Split out the code for single-child handling so that uiTab can use it too; started switching uiTab to its proper implementation.

This commit is contained in:
Pietro Gagliardi 2015-11-19 12:41:58 -05:00
parent 9acc6a2f30
commit df4156033b
5 changed files with 113 additions and 35 deletions

View File

@ -18,6 +18,7 @@ CXXFILES += \
haiku/progressbar.cpp \ haiku/progressbar.cpp \
haiku/radiobuttons.cpp \ haiku/radiobuttons.cpp \
haiku/separator.cpp \ haiku/separator.cpp \
haiku/singlechild.cpp \
haiku/slider.cpp \ haiku/slider.cpp \
haiku/spinbox.cpp \ haiku/spinbox.cpp \
haiku/stddialogs.cpp \ haiku/stddialogs.cpp \

79
haiku/singlechild.cpp Normal file
View File

@ -0,0 +1,79 @@
// 19 november 2015
#include "uipriv_haiku.hpp"
// singlechild.cpp is like child.c/child.m in the other ports, except it only handles single children with an optional margin.
struct singleChild {
uiControl *c;
BView *view;
BGroupLayout *box;
BLayoutItem *item;
BAlignment oldalign;
};
struct singleChild *newSingleChild(uiControl *c, uiControl *parent, void (*attach)(void *, BLayoutItem *), void *attachTo)
{
struct singleChild *s;
if (c == NULL)
return NULL;
s = uiNew(struct singleChild);
s->c = c;
s->view = (BView *) uiControlHandle(s->c);
s->oldalign = s->view->ExplicitAlignment();
uiControlSetParent(s->c, parent);
s->box = new BGroupLayout(B_HORIZONTAL, 0);
// TODO TODO TODO TODO
// currently BLayout won't let you add a view to a layout that isn't attached to a view
// that is, if you want to add a view to a layout, there must already be a parent view
// request this behavior to be changed
(*attach)(attachTo, s->box);
s->view->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT));
s->item = s->box->AddView(s->view, 1.0);
// and set it on the box as well
// this way it fills the entire space
s->box->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT));
return s;
}
void singleChildRemove(struct singleChild *s)
{
s->box->RemoveItem(s->item);
delete s->item;
delete s->box;
uiControlSetParent(s->c, NULL);
s->view->SetExplicitAlignment(s->oldalign);
uiFree(s);
}
void singleChildDestroy(struct singleChild *s)
{
uiControl *child;
child = s->c;
singleChildRemove(s);
uiControlDestroy(child);
}
// this is of the box itself, not of the child
// it is used to add the child to the parent layout
BLayoutItem *singleChildLayoutItem(struct singleChild *s)
{
return s->box;
}
void singleChildUpdateState(struct singleChild *s)
{
controlUpdateState(s->c);
}
void singleChildSetMargined(struct singleChild *s, float inset)
{
s->box->SetInsets(inset, inset, inset, inset);
}

View File

@ -3,13 +3,13 @@
struct uiTab { struct uiTab {
uiHaikuControl c; uiHaikuControl c;
BStringView *dummy; BTabView *tabview;
}; };
uiHaikuDefineControl( uiHaikuDefineControl(
uiTab, // type name uiTab, // type name
uiTabType, // type function uiTabType, // type function
dummy // handle tabview // handle
) )
void uiTabAppend(uiTab *t, const char *name, uiControl *c) void uiTabAppend(uiTab *t, const char *name, uiControl *c)
@ -50,8 +50,7 @@ uiTab *uiNewTab(void)
t = (uiTab *) uiNewControl(uiTabType()); t = (uiTab *) uiNewControl(uiTabType());
t->dummy = new BStringView(BRect(0, 0, 1, 1), NULL, t->tabview = new BTabView(BRect(0, 0, 1, 1), NULL);
"TODO uiTab not implemented");
uiHaikuFinishNewControl(t, uiTab); uiHaikuFinishNewControl(t, uiTab);

View File

@ -7,6 +7,14 @@
#include "../ui_haiku.hpp" #include "../ui_haiku.hpp"
#include "../common/uipriv.h" #include "../common/uipriv.h"
// alloc.c // alloc.cpp
extern void initAlloc(void); extern void initAlloc(void);
extern void uninitAlloc(void); extern void uninitAlloc(void);
// singlechild.cpp
extern struct singleChild *newSingleChild(uiControl *c, uiControl *parent, void (*attach)(void *, BLayoutItem *), void *attachTo);
extern void singleChildRemove(struct singleChild *s);
extern void singleChildDestroy(struct singleChild *s);
extern BLayoutItem *singleChildLayoutItem(struct singleChild *s);
extern void singleChildUpdateState(struct singleChild *s);
extern void singleChildSetMargined(struct singleChild *s, float inset);

View File

@ -17,9 +17,7 @@ struct uiWindow {
BGroupLayout *vbox; BGroupLayout *vbox;
uiControl *child; struct singleChild *child;
BGroupLayout *childBox;
BLayoutItem *childItem;
int margined; int margined;
int (*onClosing)(uiWindow *, void *); int (*onClosing)(uiWindow *, void *);
@ -55,12 +53,8 @@ static void windowCommitDestroy(uiControl *c)
// first hide ourselves // first hide ourselves
w->window->Hide(); w->window->Hide();
// now destroy the child // now destroy the child
if (w->child != NULL) { if (w->child != NULL)
w->childBox->RemoveItem(w->childItem); singleChildDestroy(w->child);
delete w->childItem;
uiControlSetParent(w->child, NULL);
uiControlDestroy(w->child);
}
// and finally destroy ourselves // and finally destroy ourselves
// this is why we don't use the libui-provided CommitDestroy() implementation // this is why we don't use the libui-provided CommitDestroy() implementation
// TODO check this for errors? // TODO check this for errors?
@ -87,12 +81,10 @@ static void windowCommitHide(uiControl *c)
static void windowContainerUpdateState(uiControl *c) static void windowContainerUpdateState(uiControl *c)
{ {
/*TODO
uiWindow *w = uiWindow(c); uiWindow *w = uiWindow(c);
if (w->child != NULL) if (w->child != NULL)
childUpdateState(w->child); singleChildUpdateState(w->child);
*/
} }
char *uiWindowTitle(uiWindow *w) char *uiWindowTitle(uiWindow *w)
@ -112,17 +104,23 @@ void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
w->onClosingData = data; w->onClosingData = data;
} }
// TODO save and restore old alignment // see singlechild.cpp
static void attach(void *attachTo, BLayoutItem *what)
{
BGroupLayout *vbox = (BGroupLayout *) attachTo;
vbox->AddItem(what);
}
void uiWindowSetChild(uiWindow *w, uiControl *child) void uiWindowSetChild(uiWindow *w, uiControl *child)
{ {
if (w->child != NULL) { if (w->child != NULL) {
w->childBox->RemoveItem(w->childItem); w->vbox->RemoveItem(singleChildLayoutItem(w->child));
delete w->childItem; singleChildRemove(w->child);
uiControlSetParent(w->child, NULL);
} }
w->child = child; w->child = newSingleChild(child, uiControl(w), attach, w->vbox);
if (w->child != NULL) if (w->child != NULL)
w->childItem = w->childBox->AddView((BView *) uiControlHandle(w->child)); uiWindowSetMargined(w, w->margined);
} }
int uiWindowMargined(uiWindow *w) int uiWindowMargined(uiWindow *w)
@ -133,13 +131,11 @@ int uiWindowMargined(uiWindow *w)
void uiWindowSetMargined(uiWindow *w, int margined) void uiWindowSetMargined(uiWindow *w, int margined)
{ {
w->margined = margined; w->margined = margined;
if (w->child != NULL)
if (w->margined) if (w->margined)
w->childBox->SetInsets(B_USE_WINDOW_SPACING, singleChildSetMargined(w->child, B_USE_WINDOW_SPACING);
B_USE_WINDOW_SPACING,
B_USE_WINDOW_SPACING,
B_USE_WINDOW_SPACING);
else else
w->childBox->SetInsets(0, 0, 0, 0); singleChildSetMargined(w->child, 0);
} }
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
@ -162,11 +158,6 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
// Haiku itself does this, with a TODO // Haiku itself does this, with a TODO
w->vbox->Owner()->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); w->vbox->Owner()->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
w->childBox = new BGroupLayout(B_HORIZONTAL, 0);
w->childBox->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT));
w->childBox->SetInsets(0, 0, 0, 0);
w->vbox->AddItem(w->childBox, 1.0);
uiWindowOnClosing(w, defaultOnClosing, NULL); uiWindowOnClosing(w, defaultOnClosing, NULL);
uiHaikuFinishNewControl(w, uiWindow); uiHaikuFinishNewControl(w, uiWindow);