Nuked uiParent/uiOSContainer, replacing it with the older uintptr_t/Update() method. I'm not sure if I'm happy with *this* design either... I really have no idea what's clean or elegant here ^^' Fixed initial title/size setting not taking effect on the GTK+ Window bckend.

This commit is contained in:
Pietro Gagliardi 2015-04-25 13:00:11 -04:00
parent d0e213fb04
commit 9e18ee03e6
5 changed files with 109 additions and 116 deletions

View File

@ -13,7 +13,7 @@ struct box {
uintmax_t cap; uintmax_t cap;
int vertical; int vertical;
int hasParent; int hasParent;
uiOSContainer *osContainer; uintptr_t osContainer;
int padded; int padded;
int userHid; int userHid;
int containerHid; int containerHid;
@ -37,7 +37,7 @@ static void boxDestroy(uiControl *c)
complain("attempt to destroy a uiControl at %p while it still has a parent", c); complain("attempt to destroy a uiControl at %p while it still has a parent", c);
for (i = 0; i < b->len; i++) { for (i = 0; i < b->len; i++) {
uiControlSetHasParent(b->controls[i].c, 0); uiControlSetHasParent(b->controls[i].c, 0);
uiControlSetOSContainer(b->controls[i].c, NULL); uiControlSetOSContainer(b->controls[i].c, 0);
uiControlDestroy(b->controls[i].c); uiControlDestroy(b->controls[i].c);
} }
uiFree(b->controls); uiFree(b->controls);
@ -56,20 +56,20 @@ static void boxSetHasParent(uiControl *c, int hasParent)
b->hasParent = hasParent; b->hasParent = hasParent;
} }
static void boxSetOSContainer(uiControl *c, uiOSContainer *osContainer) static void boxSetOSContainer(uiControl *c, uintptr_t osContainer)
{ {
box *b = (box *) c; box *b = (box *) c;
uintmax_t i; uintmax_t i;
uiOSContainer *oldcontainer; uintptr_t oldcontainer;
oldcontainer = b->osContainer; oldcontainer = b->osContainer;
b->osContainer = osContainer; b->osContainer = osContainer;
for (i = 0; i < b->len; i++) for (i = 0; i < b->len; i++)
uiControlSetOSContainer(b->controls[i].c, b->osContainer); uiControlSetOSContainer(b->controls[i].c, b->osContainer);
if (oldcontainer != NULL) if (oldcontainer != 0)
uiOSContainerUpdate(oldcontainer); uiUpdateOSContainer(oldcontainer);
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
static void boxPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height) static void boxPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
@ -231,8 +231,8 @@ static void boxShow(uiControl *c)
if (!b->containerHid) { if (!b->containerHid) {
for (i = 0; i < b->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerShow(b->controls[i].c); uiControlContainerShow(b->controls[i].c);
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
} }
@ -244,8 +244,8 @@ static void boxHide(uiControl *c)
b->userHid = 1; b->userHid = 1;
for (i = 0; i < b->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerHide(b->controls[i].c); uiControlContainerHide(b->controls[i].c);
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
static void boxContainerShow(uiControl *c) static void boxContainerShow(uiControl *c)
@ -257,8 +257,8 @@ static void boxContainerShow(uiControl *c)
if (!b->userHid) { if (!b->userHid) {
for (i = 0; i < b->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerShow(b->controls[i].c); uiControlContainerShow(b->controls[i].c);
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
} }
@ -270,8 +270,8 @@ static void boxContainerHide(uiControl *c)
b->containerHid = 1; b->containerHid = 1;
for (i = 0; i < b->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerHide(b->controls[i].c); uiControlContainerHide(b->controls[i].c);
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
static void boxEnable(uiControl *c) static void boxEnable(uiControl *c)
@ -330,9 +330,9 @@ static void boxAppend(uiBox *ss, uiControl *c, int stretchy)
b->controls[b->len].c = c; b->controls[b->len].c = c;
b->controls[b->len].stretchy = stretchy; b->controls[b->len].stretchy = stretchy;
b->len++; // must be here for OS container updates to work b->len++; // must be here for OS container updates to work
if (b->osContainer != NULL) { if (b->osContainer != 0) {
uiControlSetOSContainer(b->controls[b->len - 1].c, b->osContainer); uiControlSetOSContainer(b->controls[b->len - 1].c, b->osContainer);
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
} }
@ -349,9 +349,9 @@ static void boxDelete(uiBox *ss, uintmax_t index)
// TODO memset the last one to NULL // TODO memset the last one to NULL
b->len--; b->len--;
uiControlSetHasParent(removed, 0); uiControlSetHasParent(removed, 0);
if (b->osContainer != NULL) { if (b->osContainer != 0) {
uiControlSetOSContainer(removed, NULL); uiControlSetOSContainer(removed, 0);
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
} }
@ -367,8 +367,8 @@ static void boxSetPadded(uiBox *ss, int padded)
box *b = (box *) ss; box *b = (box *) ss;
b->padded = padded; b->padded = padded;
if (b->osContainer != NULL) if (b->osContainer != 0)
uiOSContainerUpdate(b->osContainer); uiUpdateOSContainer(b->osContainer);
} }
uiBox *uiNewHorizontalBox(void) uiBox *uiNewHorizontalBox(void)

View File

@ -47,7 +47,7 @@ interface Control {
func Destroy(void); func Destroy(void);
func Handle(void) uintptr_t; func Handle(void) uintptr_t;
func SetHasParent(hasParent int); func SetHasParent(hasParent int);
func SetOSContainer(c *OSContainer); func SetOSContainer(handle uintptr_t);
func PreferredSize(d *Sizing, width *intmax_t, height *intmax_t); func PreferredSize(d *Sizing, width *intmax_t, height *intmax_t);
func Resize(x intmax_t, y intmax_t, width intmax_t, height intmax_t, d *Sizing); func Resize(x intmax_t, y intmax_t, width intmax_t, height intmax_t, d *Sizing);
func Visible(void) int; func Visible(void) int;
@ -61,15 +61,7 @@ interface Control {
func ContainerDisable(void); func ContainerDisable(void);
}; };
interface OSContainer { func UpdateOSContainer(handle uintptr_t);
field Internal *void;
func Destroy(void);
func Handle(void) uintptr_t;
func SetMainControl(c *Control);
func SetMargins(left intmax_t, top intmax_t, right intmax_t, bottom intmax_t);
func Update(void);
};
func NewOSContainer(osParent uintptr_t) *OSContainer;
interface Window { interface Window {
func Destroy(void); func Destroy(void);

View File

@ -1,30 +1,16 @@
// 13 august 2014 // 13 august 2014
#include "uipriv_unix.h" #include "uipriv_unix.h"
#define uipOSContainerType (uipOSContainer_get_type()) // In GTK+, many containers (GtkWindow, GtkNotebook, GtkFrame) can only have one child.
#define uipOSContainer(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), uipOSContainerType, uipOSContainer)) // (In the case of GtkNotebook, each child widget is a single page.)
#define uipIsOSContainer(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), uipOSContainerType)) // GtkFrame and GtkLayout, the official "anything goes" containers, are buggy and ineffective.
#define uipOSContainerClass(class) (G_TYPE_CHECK_CLASS_CAST((class), uipOSContainerType, uipOSContainerClass)) // This custom container does what we need just fine.
#define uipIsOSContainerClass(class) (G_TYPE_CHECK_CLASS_TYPE((class), uipOSContainer)) // uiWindow, uiTab, and uiGroup will keep private instances of this special container, and it will be the OS container given to each uiControl that becomes a child of those three controls.
#define uipGetParentClass(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), uipOSContainerType, uipOSContainerClass))
typedef struct uipOSContainer uipOSContainer; // This container maintains a "main control", which is the uiControl that is resized alongside the container.
typedef struct uipOSContainerClass uipOSContainerClass; // It also keeps track of all the GtkWidgets that are in the uiControl for the purposes of GTK+ internals.
// Finally, it also handles margining.
struct uipOSContainer { // In other words, it does everything uiWindow, uiTab, and uiGroup need to do to keep track of controls.
GtkContainer parent_instance;
uiControl *mainControl;
GPtrArray *children; // for forall()
intmax_t marginLeft;
intmax_t marginTop;
intmax_t marginRight;
intmax_t marginBottom;
gboolean canDestroy;
};
struct uipOSContainerClass {
GtkContainerClass parent_class;
};
G_DEFINE_TYPE(uipOSContainer, uipOSContainer, GTK_TYPE_CONTAINER) G_DEFINE_TYPE(uipOSContainer, uipOSContainer, GTK_TYPE_CONTAINER)
@ -42,12 +28,12 @@ static void uipOSContainer_dispose(GObject *obj)
{ {
uipOSContainer *c = uipOSContainer(obj); uipOSContainer *c = uipOSContainer(obj);
// don't free mainControl here; that should have been done by uiOSContainerDestroy() // don't free mainControl here; that should have been done by osContainerDestroy()
if (!c->canDestroy) if (!c->canDestroy)
complain("attempt to dispose uiOSContainer with uipOSContainer at %p before uiOSContainerDestroy()", c); complain("attempt to dispose uipOSContainer at %p before osContainerDestroy()", c);
if (c->children != NULL) { if (c->children != NULL) {
if (c->children->len != 0) if (c->children->len != 0)
complain("disposing uiOSContainer with uipOSContainer at %p while there are still children", c); complain("disposing uipOSContainer at %p while there are still children", c);
g_ptr_array_unref(c->children); g_ptr_array_unref(c->children);
c->children = NULL; c->children = NULL;
} }
@ -59,7 +45,7 @@ static void uipOSContainer_finalize(GObject *obj)
uipOSContainer *c = uipOSContainer(obj); uipOSContainer *c = uipOSContainer(obj);
if (!c->canDestroy) if (!c->canDestroy)
complain("attempt to finalize uiOSContainer with uipOSContainer at %p before uiOSContainerDestroy()", c); complain("attempt to finalize uipOSContainer at %p before osContainerDestroy()", c);
G_OBJECT_CLASS(uipOSContainer_parent_class)->finalize(obj); G_OBJECT_CLASS(uipOSContainer_parent_class)->finalize(obj);
if (options.debugLogAllocations) if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", obj); fprintf(stderr, "%p free\n", obj);
@ -138,82 +124,59 @@ static void uipOSContainer_class_init(uipOSContainerClass *class)
GTK_CONTAINER_CLASS(class)->forall = uipOSContainer_forall; GTK_CONTAINER_CLASS(class)->forall = uipOSContainer_forall;
} }
// TODO convert other methods of other backends to pp arg p instance variable GtkWidget *newOSContainer(void)
static void parentDestroy(uiOSContainer *cc)
{ {
uipOSContainer *c = uipOSContainer(cc->Internal); GtkWidget *c;
c = GTK_WIDGET(g_object_new(uipOSContainerType, NULL));
// make it visible by default
gtk_widget_show_all(c);
// hold a reference to ourselves to keep ourselves alive when we're removed from whatever container we wind up in
g_object_ref_sink(c);
return c;
}
void osContainerDestroy(uipOSContainer *c)
{
// first, destroy the main control // first, destroy the main control
if (c->mainControl != NULL) { if (c->mainControl != NULL) {
// we have to do this before we can destroy controls // we have to do this before we can destroy controls
// TODO clean this up a bit
uiControlSetHasParent(c->mainControl, 0); uiControlSetHasParent(c->mainControl, 0);
uiControlSetOSContainer(c->mainControl, NULL); uiControlSetOSContainer(c->mainControl, 0);
uiControlDestroy(c->mainControl); uiControlDestroy(c->mainControl);
c->mainControl = NULL; c->mainControl = NULL;
} }
// now we can mark the parent as ready to be destroyed // now we can mark the parent as ready to be destroyed
c->canDestroy = TRUE; c->canDestroy = TRUE;
// finally, destroy the parent // finally, actually go ahead and destroy ourselves
g_object_unref(G_OBJECT(c)); g_object_unref(c);
// and free ourselves
uiFree(cc);
} }
static uintptr_t parentHandle(uiOSContainer *cc) void osContainerSetMainControl(uipOSContainer *c, uiControl *mainControl)
{ {
uipOSContainer *c = uipOSContainer(cc->Internal);
return (uintptr_t) c;
}
static void parentSetMainControl(uiOSContainer *cc, uiControl *mainControl)
{
uipOSContainer *c = uipOSContainer(cc->Internal);
if (c->mainControl != NULL) { if (c->mainControl != NULL) {
uiControlSetHasParent(c->mainControl, 0); uiControlSetHasParent(c->mainControl, 0);
uiControlSetOSContainer(c->mainControl, NULL); uiControlSetOSContainer(c->mainControl, 0);
} }
c->mainControl = mainControl; c->mainControl = mainControl;
if (c->mainControl != NULL) { if (c->mainControl != NULL) {
uiControlSetHasParent(c->mainControl, 1); uiControlSetHasParent(c->mainControl, 1);
uiControlSetOSContainer(c->mainControl, cc); uiControlSetOSContainer(c->mainControl, (uintptr_t) c);
} }
uiUpdateOSContainer((uintptr_t) c);
} }
static void parentSetMargins(uiOSContainer *cc, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom) void osContainerSetMargins(uipOSContainer *c, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom)
{ {
uipOSContainer *c = uipOSContainer(cc->Internal);
c->marginLeft = left; c->marginLeft = left;
c->marginTop = top; c->marginTop = top;
c->marginRight = right; c->marginRight = right;
c->marginBottom = bottom; c->marginBottom = bottom;
uiUpdateOSContainer((uintptr_t) c);
} }
static void parentUpdate(uiOSContainer *cc) void uiUpdateOSContainer(uintptr_t c)
{ {
uipOSContainer *c = uipOSContainer(cc->Internal);
gtk_widget_queue_resize(GTK_WIDGET(c)); gtk_widget_queue_resize(GTK_WIDGET(c));
} }
uiOSContainer *uiNewOSContainer(uintptr_t osParent)
{
uiOSContainer *c;
c = uiNew(uiOSContainer);
c->Internal = g_object_new(uipOSContainerType, NULL);
c->Destroy = parentDestroy;
c->Handle = parentHandle;
c->SetMainControl = parentSetMainControl;
c->SetMargins = parentSetMargins;
c->Update = parentUpdate;
gtk_container_add(GTK_CONTAINER(osParent), GTK_WIDGET(c->Internal));
// make it visible by default
gtk_widget_show_all(GTK_WIDGET(c->Internal));
// hold a reference to it to keep it alive
g_object_ref(G_OBJECT(c->Internal));
return c;
}

View File

@ -16,3 +16,32 @@ extern char *strdupText(const char *);
// menu.c // menu.c
extern GtkWidget *makeMenubar(uiWindow *); extern GtkWidget *makeMenubar(uiWindow *);
// oscontainer.c
#define uipOSContainerType (uipOSContainer_get_type())
#define uipOSContainer(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), uipOSContainerType, uipOSContainer))
#define uipIsOSContainer(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), uipOSContainerType))
#define uipOSContainerClass(class) (G_TYPE_CHECK_CLASS_CAST((class), uipOSContainerType, uipOSContainerClass))
#define uipIsOSContainerClass(class) (G_TYPE_CHECK_CLASS_TYPE((class), uipOSContainer))
#define uipGetParentClass(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), uipOSContainerType, uipOSContainerClass))
typedef struct uipOSContainer uipOSContainer;
typedef struct uipOSContainerClass uipOSContainerClass;
struct uipOSContainer {
GtkContainer parent_instance;
// TODO make these private
uiControl *mainControl;
GPtrArray *children; // for forall()
intmax_t marginLeft;
intmax_t marginTop;
intmax_t marginRight;
intmax_t marginBottom;
gboolean canDestroy;
};
struct uipOSContainerClass {
GtkContainerClass parent_class;
};
extern GType uipOSContainer_get_type(void);
extern GtkWidget *newOSContainer(void);
extern void osContainerDestroy(uipOSContainer *);
extern void osContainerSetMainControl(uipOSContainer *, uiControl *);
extern void osContainerSetMargins(uipOSContainer *, intmax_t, intmax_t, intmax_t, intmax_t);

View File

@ -15,8 +15,8 @@ struct window {
GtkBox *vbox; GtkBox *vbox;
// the OS container for the uiWindow // the OS container for the uiWindow
uiOSContainer *content;
GtkWidget *contentWidget; GtkWidget *contentWidget;
uipOSContainer *content;
// events // events
int (*onClosing)(uiWindow *, void *); int (*onClosing)(uiWindow *, void *);
@ -47,13 +47,20 @@ static void windowDestroy(uiWindow *ww)
// first, hide the window to avoid flicker // first, hide the window to avoid flicker
gtk_widget_hide(w->widget); gtk_widget_hide(w->widget);
// next, remove the uiOSContainer from the vbox // next, remove the uiOSContainer from the vbox
// this will free the GtkWindow's reference to it
gtk_container_remove(w->vboxcontainer, GTK_WIDGET(w->contentWidget)); gtk_container_remove(w->vboxcontainer, GTK_WIDGET(w->contentWidget));
// next, destroy the uiOSContainer, which will destroy its child widget // next, destroy the uiOSContainer, which will destroy its child widget
uiOSContainerDestroy(w->content); // this will release its own reference.
osContainerDestroy(w->content);
// TODO menus // TODO menus
// next, destroy the GtkWindow itself, which will destroy the vbox, menus, etc. // next, destroy the GtkWindow itself, which will destroy the vbox, menus, etc.
gtk_widget_destroy(w->widget); gtk_widget_destroy(w->widget);
// finally, free ourselves // finally, free ourselves
uiFree(w); uiFree(w);
} }
@ -106,9 +113,7 @@ static void windowSetChild(uiWindow *ww, uiControl *c)
{ {
struct window *w = (struct window *) ww; struct window *w = (struct window *) ww;
// TODO make the update implicit osContainerSetMainControl(w->content, c);
uiOSContainerSetMainControl(w->content, c);
uiOSContainerUpdate(w->content);
} }
static int windowMargined(uiWindow *ww) static int windowMargined(uiWindow *ww)
@ -125,10 +130,9 @@ static void windowSetMargined(uiWindow *ww, int margined)
// TODO make the update implicit // TODO make the update implicit
w->margined = margined; w->margined = margined;
if (w->margined) if (w->margined)
uiOSContainerSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin); osContainerSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin);
else else
uiOSContainerSetMargins(w->content, 0, 0, 0, 0); osContainerSetMargins(w->content, 0, 0, 0, 0);
uiOSContainerUpdate(w->content);
} }
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
@ -141,6 +145,10 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
w->container = GTK_CONTAINER(w->widget); w->container = GTK_CONTAINER(w->widget);
w->window = GTK_WINDOW(w->widget); w->window = GTK_WINDOW(w->widget);
gtk_window_set_title(w->window, title);
// TODO this does not take menus into account
gtk_window_resize(w->window, width, height);
w->vboxwidget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); w->vboxwidget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
w->vboxcontainer = GTK_CONTAINER(w->vboxwidget); w->vboxcontainer = GTK_CONTAINER(w->vboxwidget);
w->vbox = GTK_BOX(w->vboxwidget); w->vbox = GTK_BOX(w->vboxwidget);
@ -152,12 +160,13 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
gtk_container_add(w->vboxcontainer, makeMenubar(uiWindow(w))); gtk_container_add(w->vboxcontainer, makeMenubar(uiWindow(w)));
// and add the OS container // and add the OS container
w->content = uiNewOSContainer((uintptr_t) (w->vboxcontainer)); w->contentWidget = newOSContainer();
w->contentWidget = GTK_WIDGET(uiOSContainerHandle(w->content)); w->content = uipOSContainer(w->contentWidget);
gtk_widget_set_hexpand(w->contentWidget, TRUE); gtk_widget_set_hexpand(w->contentWidget, TRUE);
gtk_widget_set_halign(w->contentWidget, GTK_ALIGN_FILL); gtk_widget_set_halign(w->contentWidget, GTK_ALIGN_FILL);
gtk_widget_set_vexpand(w->contentWidget, TRUE); gtk_widget_set_vexpand(w->contentWidget, TRUE);
gtk_widget_set_valign(w->contentWidget, GTK_ALIGN_FILL); gtk_widget_set_valign(w->contentWidget, GTK_ALIGN_FILL);
gtk_container_add(w->vboxcontainer, w->contentWidget);
// show everything in the vbox, but not the GtkWindow itself // show everything in the vbox, but not the GtkWindow itself
gtk_widget_show_all(w->vboxwidget); gtk_widget_show_all(w->vboxwidget);