From f7f96f4d8bb3855c5d6ee81b319abdcd9ece3d8a Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 18 Nov 2015 16:16:29 -0500 Subject: [PATCH] Added an implementation of uiBox. --- haiku/GNUmakeinc.mk | 1 + haiku/box.cpp | 145 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 haiku/box.cpp diff --git a/haiku/GNUmakeinc.mk b/haiku/GNUmakeinc.mk index a07580aa..5e57bce5 100644 --- a/haiku/GNUmakeinc.mk +++ b/haiku/GNUmakeinc.mk @@ -2,6 +2,7 @@ CXXFILES += \ haiku/alloc.cpp \ + haiku/box.cpp \ haiku/control.cpp \ haiku/main.cpp \ haiku/text.cpp \ diff --git a/haiku/box.cpp b/haiku/box.cpp new file mode 100644 index 00000000..0b57f9c6 --- /dev/null +++ b/haiku/box.cpp @@ -0,0 +1,145 @@ +// 18 november 2015 +#include +#include "uipriv_haiku.hpp" +using namespace std; + +struct boxchild { + uiControl *c; + BAlignment oldalign; + bool stretchy; + BLayoutItem *item; +}; + +struct uiBox { + uiHaikuControl c; + BGroupLayout *layout; + vector *controls; + int vertical; + int padded; +}; + +static void onDestroy(uiBox *b); + +uiHaikuDefineControlWithOnDestroy( + uiBox, // type name + uiBoxType, // type function + layout, // handle + onDestroy(hthis); // on destroy +) + +static void onDestroy(uiBox *b) +{ + struct boxchild bc; + + while (b->controls->size() != 0) { + bc = b->controls->back(); + uiControlSetParent(bc.c, NULL); + uiControlDestroy(bc.c); + b->controls->pop_back(); + } + delete b->controls; +} + +static void boxContainerUpdateState(uiControl *c) +{ + uiBox *b = uiBox(c); + struct boxchild bc; + uintmax_t i; + + for (i = 0; i < b->controls->size(); i++) { + bc = b->controls->at(i); + controlUpdateState(bc.c); + } +} + +#define isStretchy(bc) bc.stretchy + +void uiBoxAppend(uiBox *b, uiControl *c, int stretchy) +{ + struct boxchild bc; + BView *view; + BAlignment alignment; + float weight; + + bc.c = c; + view = (BView *) uiControlHandle(bc.c); + bc.oldalign = view->ExplicitAlignment(); + bc.stretchy = stretchy != 0; + + alignment.horizontal = B_ALIGN_USE_FULL_WIDTH; + alignment.vertical = B_ALIGN_USE_FULL_HEIGHT; + weight = 0.0; + if (isStretchy(bc)) + weight = 1.0; + else { + if (b->vertical) + alignment.vertical = B_ALIGN_TOP; + else + alignment.horizontal = B_ALIGN_LEFT; + } + + uiControlSetParent(bc.c, uiControl(b)); + view->SetExplicitAlignment(alignment); + bc.item = b->layout->AddView(view, weight); + + b->controls->push_back(bc); +} + +void uiBoxDelete(uiBox *b, uintmax_t index) +{ + struct boxchild bc; + BView *view; + + bc = b->controls->back(); + b->controls->pop_back(); + + b->layout->RemoveItem(bc.item); + delete bc.item; + + view = (BView *) uiControlHandle(bc.c); + view->SetExplicitAlignment(bc.oldalign); + + uiControlSetParent(bc.c, NULL); +} + +int uiBoxPadded(uiBox *b) +{ + return b->padded; +} + +void uiBoxSetPadded(uiBox *b, int padded) +{ + b->padded = padded; + if (b->padded) + b->layout->SetSpacing(B_USE_DEFAULT_SPACING); + else + b->layout->SetSpacing(0); +} + +static uiBox *finishNewBox(orientation o) +{ + uiBox *b; + + b = (uiBox *) uiNewControl(uiBoxType()); + + b->layout = new BGroupLayout(o, 0); + + b->vertical = o == B_VERTICAL; + + b->controls = new vector(); + + uiHaikuFinishNewControl(b, uiBox); + uiControl(b)->ContainerUpdateState = boxContainerUpdateState; + + return b; +} + +uiBox *uiNewHorizontalBox(void) +{ + return finishNewBox(B_HORIZONTAL); +} + +uiBox *uiNewVerticalBox(void) +{ + return finishNewBox(B_VERTICAL); +}