From 705f81d7e43aa2f51d9bebd08b7c5a3ea0999a9e Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 16 Aug 2015 22:19:15 -0400 Subject: [PATCH] Started merging back the uiControl base code. --- redo/reredo/control.c | 121 ++++++++++++++++++++++++++++++ redo/reredo/darwin/button.m | 2 +- redo/reredo/darwin/checkbox.m | 2 +- redo/reredo/darwin/group.m | 6 +- redo/reredo/darwin/label.m | 2 +- redo/reredo/darwin/radiobuttons.m | 2 +- redo/reredo/ui.h | 12 +-- redo/reredo/ui_darwin.h | 5 ++ 8 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 redo/reredo/control.c diff --git a/redo/reredo/control.c b/redo/reredo/control.c new file mode 100644 index 00000000..f91b6234 --- /dev/null +++ b/redo/reredo/control.c @@ -0,0 +1,121 @@ +// 26 may 2015 +#include "ui.h" +#include "uipriv.h" + +struct controlBase { + uiControl *parent; + int hidden; + int disabled; +}; + +static uintmax_t type_uiControl = 0; + +uintmax_t uiTypeControl(void) +{ + if (type_uiControl == 0) + type_uiControl = uiRegisterType("uiControl", 0, sizeof (uiControl)); + return type_uiControl; +} + +#define controlBase(c) ((struct controlBase *) (c->Internal)) + +void uiControlDestroy(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + if (cb->parent != NULL) + complain("attempt to destroy uiControl %p while it has a parent", c); + (*(c->CommitDestroy))(c); + uiFree(cb); + uiFree(c); +} + +static void controlUpdateState(uiControl *); + +void uiControlSetParent(uiControl *c, uiControl *parent) +{ + struct controlBase *cb = controlBase(c); + + if (parent != NULL && cb->parent != NULL) + complain("attempt to reparent uiControl %p (has parent %p, attempt to give parent %p)", c, cb->parent, parent); + if (parent == NULL && cb->parent == NULL) + complain("attempt to double unparent uiControl %p", c); + cb->parent = parent; + // for situations such as where the old parent was disabled but the new one is not, etc. + controlUpdateState(c); +} + +static int controlContainerVisible(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + if (cb->hidden) + return 0; + if (cb->parent == NULL) + return 1; + return controlContainerVisible(cb->parent); +} + +void uiControlShow(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + cb->hidden = 0; + controlUpdateState(c); +} + +void uiControlHide(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + cb->hidden = 1; + controlUpdateState(c); +} + +static int controlContainerEnabled(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + if (cb->disabled) + return 0; + if (cb->parent == NULL) + return 1; + return controlContainerEnabled(cb->parent); +} + +void uiControlEnable(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + cb->disabled = 0; + controlUpdateState(c); +} + +void uiControlDisable(uiControl *c) +{ + struct controlBase *cb = controlBase(c); + + cb->disabled = 1; + controlUpdateState(c); +} + +static void controlUpdateState(uiControl *c) +{ + if (uiControlContainerVisible(c)) + osCommitShow(c); + else + osCommitHide(c); + if (uiControlContainerEnabled(c)) + osCommitEnable(c); + else + osCommitDisable(c); + (*(c->ContainerUpdateState))(c); + // and queue a resize, just in case we showed/hid something +//TODO uiControlQueueResize(c); +} + +// TODO should this be public? +uiControl *uiNewControl(uintmax_t type) +{ + return uiControl(newTyped(type)); +} diff --git a/redo/reredo/darwin/button.m b/redo/reredo/darwin/button.m index 3981699e..5aacde32 100644 --- a/redo/reredo/darwin/button.m +++ b/redo/reredo/darwin/button.m @@ -79,7 +79,7 @@ void uiButtonSetText(uiButton *b, const char *text) { [b->button setTitle:toNSString(text)]; // this may result in the size of the button changing - uiControlQueueResize(uiControl(b)); +//TODO uiControlQueueResize(uiControl(b)); } void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data) diff --git a/redo/reredo/darwin/checkbox.m b/redo/reredo/darwin/checkbox.m index 1533d9be..aa43ec7d 100644 --- a/redo/reredo/darwin/checkbox.m +++ b/redo/reredo/darwin/checkbox.m @@ -79,7 +79,7 @@ void uiCheckboxSetText(uiCheckbox *c, const char *text) { [c->button setTitle:toNSString(text)]; // this may result in the size of the checkbox changing - uiControlQueueResize(uiControl(c)); +//TODO uiControlQueueResize(uiControl(c)); } void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data) diff --git a/redo/reredo/darwin/group.m b/redo/reredo/darwin/group.m index c37718fb..940238bd 100644 --- a/redo/reredo/darwin/group.m +++ b/redo/reredo/darwin/group.m @@ -26,7 +26,7 @@ void uiGroupSetTitle(uiGroup *g, const char *text) { // TODO // changing the text might necessitate a change in the groupbox's size - uiControlQueueResize(uiControl(g)); +//TODO uiControlQueueResize(uiControl(g)); } void uiGroupSetChild(uiGroup *g, uiControl *child) @@ -37,7 +37,7 @@ void uiGroupSetChild(uiGroup *g, uiControl *child) g->child = child; if (g->child != NULL) { uiControlSetParent(g->child, uiControl(g)); - uiControlQueueResize(g->child); +//TODO uiControlQueueResize(g->child); } */ } @@ -51,7 +51,7 @@ void uiGroupSetMargined(uiGroup *g, int margined) { g->margined = margined; // TODO - uiControlQueueResize(uiControl(g)); +//TODO uiControlQueueResize(uiControl(g)); } uiGroup *uiNewGroup(const char *title) diff --git a/redo/reredo/darwin/label.m b/redo/reredo/darwin/label.m index dc5e8302..ae0f303c 100644 --- a/redo/reredo/darwin/label.m +++ b/redo/reredo/darwin/label.m @@ -21,7 +21,7 @@ void uiLabelSetText(uiLabel *l, const char *text) { [l->textfield setStringValue:toNSString(text)]; // changing the text might necessitate a change in the label's size - uiControlQueueResize(uiControl(l)); +//TODO uiControlQueueResize(uiControl(l)); } uiLabel *uiNewLabel(const char *text) diff --git a/redo/reredo/darwin/radiobuttons.m b/redo/reredo/darwin/radiobuttons.m index 0b07bb94..3c79b40b 100644 --- a/redo/reredo/darwin/radiobuttons.m +++ b/redo/reredo/darwin/radiobuttons.m @@ -15,7 +15,7 @@ uiDarwinDefineControl( void uiRadioButtonsAppend(uiRadioButtons *r, const char *text) { // TODO - uiControlQueueResize(uiControl(r)); +//TODO uiControlQueueResize(uiControl(r)); } uiRadioButtons *uiNewRadioButtons(void) diff --git a/redo/reredo/ui.h b/redo/reredo/ui.h index ae6eaf5b..11094f98 100644 --- a/redo/reredo/ui.h +++ b/redo/reredo/ui.h @@ -53,14 +53,16 @@ struct uiControl { void *Internal; // for use by libui only void (*CommitDestroy)(uiControl *); uintptr_t (*Handle)(uiControl *); - void (*CommitShow)(uiControl *); // TODO are all of these still needed? - void (*CommitHide)(uiControl *); - void (*CommitEnable)(uiControl *); - void (*CommitDisable)(uiControl *); - void (*ContainerUpdateState)(uiControl *); // TODO still needed? + void (*ContainerUpdateState)(uiControl *); }; _UI_EXTERN uintmax_t uiControlType(void); #define uiControl(this) ((uiControl *) uiIsA((this), uiControlType(), 1)) +_UI_EXTERN void uiControlDestroy(uiControl *); +_UI_EXTERN void uiControlSetParent(uiControl *, uiControl *); +_UI_EXTERN void uiControlShow(uiControl *); +_UI_EXTERN void uiControlHide(uiControl *); +_UI_EXTERN void uiControlEnable(uiControl *); +_UI_EXTERN void uiControlDisable(uiControl *); /* TODO func NewControl(type uintmax_t) *Control; diff --git a/redo/reredo/ui_darwin.h b/redo/reredo/ui_darwin.h index 0c8f4da2..25a817d0 100644 --- a/redo/reredo/ui_darwin.h +++ b/redo/reredo/ui_darwin.h @@ -25,6 +25,10 @@ This file assumes that you have imported and "ui.h" beforehand. static uintptr_t _ ## type ## Handle(uiControl *c) \ { \ return type(c)->handlefield; \ + } \ + static void _ ## type ## ContainerUpdateState(uiControl *c) \ + { \ + /* do nothing */ \ } #define uiDarwinDefineControl(type, typefn, handlefield) \ @@ -33,6 +37,7 @@ This file assumes that you have imported and "ui.h" beforehand. #define uiDarwinFinishNewControl(variable, type) \ type(variable)->CommitDestroy = _ ## type ## CommitDestroy; \ type(variable)->Handle = _ ## type ## Handle; \ + type(variable)->ContainerUpdateState = _ ## type ## ContainerUpdateState; \ uiDarwinFinishControl(uiControl(variable)); // This is a function used to set up a control.