diff --git a/new/box.c b/new/box.c index a1438486..6ba9cdc5 100644 --- a/new/box.c +++ b/new/box.c @@ -13,7 +13,7 @@ struct box { uintmax_t cap; int vertical; int hasParent; - uiOSContainer *osContainer; + uintptr_t osContainer; int padded; int userHid; 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); for (i = 0; i < b->len; i++) { uiControlSetHasParent(b->controls[i].c, 0); - uiControlSetOSContainer(b->controls[i].c, NULL); + uiControlSetOSContainer(b->controls[i].c, 0); uiControlDestroy(b->controls[i].c); } uiFree(b->controls); @@ -56,20 +56,20 @@ static void boxSetHasParent(uiControl *c, int hasParent) b->hasParent = hasParent; } -static void boxSetOSContainer(uiControl *c, uiOSContainer *osContainer) +static void boxSetOSContainer(uiControl *c, uintptr_t osContainer) { box *b = (box *) c; uintmax_t i; - uiOSContainer *oldcontainer; + uintptr_t oldcontainer; oldcontainer = b->osContainer; b->osContainer = osContainer; for (i = 0; i < b->len; i++) uiControlSetOSContainer(b->controls[i].c, b->osContainer); - if (oldcontainer != NULL) - uiOSContainerUpdate(oldcontainer); - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (oldcontainer != 0) + uiUpdateOSContainer(oldcontainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } 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) { for (i = 0; i < b->len; i++) uiControlContainerShow(b->controls[i].c); - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } } @@ -244,8 +244,8 @@ static void boxHide(uiControl *c) b->userHid = 1; for (i = 0; i < b->len; i++) uiControlContainerHide(b->controls[i].c); - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } static void boxContainerShow(uiControl *c) @@ -257,8 +257,8 @@ static void boxContainerShow(uiControl *c) if (!b->userHid) { for (i = 0; i < b->len; i++) uiControlContainerShow(b->controls[i].c); - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } } @@ -270,8 +270,8 @@ static void boxContainerHide(uiControl *c) b->containerHid = 1; for (i = 0; i < b->len; i++) uiControlContainerHide(b->controls[i].c); - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } 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].stretchy = stretchy; 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); - 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 b->len--; uiControlSetHasParent(removed, 0); - if (b->osContainer != NULL) { - uiControlSetOSContainer(removed, NULL); - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) { + uiControlSetOSContainer(removed, 0); + uiUpdateOSContainer(b->osContainer); } } @@ -367,8 +367,8 @@ static void boxSetPadded(uiBox *ss, int padded) box *b = (box *) ss; b->padded = padded; - if (b->osContainer != NULL) - uiOSContainerUpdate(b->osContainer); + if (b->osContainer != 0) + uiUpdateOSContainer(b->osContainer); } uiBox *uiNewHorizontalBox(void) diff --git a/new/ui.idl b/new/ui.idl index 4f6d288b..ce44b448 100644 --- a/new/ui.idl +++ b/new/ui.idl @@ -47,7 +47,7 @@ interface Control { func Destroy(void); func Handle(void) uintptr_t; func SetHasParent(hasParent int); - func SetOSContainer(c *OSContainer); + func SetOSContainer(handle uintptr_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 Visible(void) int; @@ -61,15 +61,7 @@ interface Control { func ContainerDisable(void); }; -interface OSContainer { - 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; +func UpdateOSContainer(handle uintptr_t); interface Window { func Destroy(void); diff --git a/new/unix/oscontainer.c b/new/unix/oscontainer.c index a690a63a..68713c80 100644 --- a/new/unix/oscontainer.c +++ b/new/unix/oscontainer.c @@ -1,30 +1,16 @@ // 13 august 2014 #include "uipriv_unix.h" -#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)) +// In GTK+, many containers (GtkWindow, GtkNotebook, GtkFrame) can only have one child. +// (In the case of GtkNotebook, each child widget is a single page.) +// GtkFrame and GtkLayout, the official "anything goes" containers, are buggy and ineffective. +// This custom container does what we need just fine. +// 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. -typedef struct uipOSContainer uipOSContainer; -typedef struct uipOSContainerClass uipOSContainerClass; - -struct uipOSContainer { - 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; -}; +// This container maintains a "main control", which is the uiControl that is resized alongside the container. +// It also keeps track of all the GtkWidgets that are in the uiControl for the purposes of GTK+ internals. +// Finally, it also handles margining. +// In other words, it does everything uiWindow, uiTab, and uiGroup need to do to keep track of controls. G_DEFINE_TYPE(uipOSContainer, uipOSContainer, GTK_TYPE_CONTAINER) @@ -42,12 +28,12 @@ static void uipOSContainer_dispose(GObject *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) - 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->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); c->children = NULL; } @@ -59,7 +45,7 @@ static void uipOSContainer_finalize(GObject *obj) uipOSContainer *c = uipOSContainer(obj); 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); if (options.debugLogAllocations) fprintf(stderr, "%p free\n", obj); @@ -138,82 +124,59 @@ static void uipOSContainer_class_init(uipOSContainerClass *class) GTK_CONTAINER_CLASS(class)->forall = uipOSContainer_forall; } -// TODO convert other methods of other backends to pp arg p instance variable - -static void parentDestroy(uiOSContainer *cc) +GtkWidget *newOSContainer(void) { - 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 if (c->mainControl != NULL) { // we have to do this before we can destroy controls + // TODO clean this up a bit uiControlSetHasParent(c->mainControl, 0); - uiControlSetOSContainer(c->mainControl, NULL); + uiControlSetOSContainer(c->mainControl, 0); uiControlDestroy(c->mainControl); c->mainControl = NULL; } // now we can mark the parent as ready to be destroyed c->canDestroy = TRUE; - // finally, destroy the parent - g_object_unref(G_OBJECT(c)); - // and free ourselves - uiFree(cc); + // finally, actually go ahead and destroy ourselves + g_object_unref(c); } -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) { uiControlSetHasParent(c->mainControl, 0); - uiControlSetOSContainer(c->mainControl, NULL); + uiControlSetOSContainer(c->mainControl, 0); } c->mainControl = mainControl; if (c->mainControl != NULL) { 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->marginTop = top; c->marginRight = right; 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)); } - -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; -} diff --git a/new/unix/uipriv_unix.h b/new/unix/uipriv_unix.h index b9177de8..53f6a2e3 100644 --- a/new/unix/uipriv_unix.h +++ b/new/unix/uipriv_unix.h @@ -16,3 +16,32 @@ extern char *strdupText(const char *); // menu.c 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); diff --git a/new/unix/window.c b/new/unix/window.c index 97a2477f..6a050c4f 100644 --- a/new/unix/window.c +++ b/new/unix/window.c @@ -15,8 +15,8 @@ struct window { GtkBox *vbox; // the OS container for the uiWindow - uiOSContainer *content; GtkWidget *contentWidget; + uipOSContainer *content; // events int (*onClosing)(uiWindow *, void *); @@ -47,13 +47,20 @@ static void windowDestroy(uiWindow *ww) // first, hide the window to avoid flicker gtk_widget_hide(w->widget); + // 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)); + // next, destroy the uiOSContainer, which will destroy its child widget - uiOSContainerDestroy(w->content); + // this will release its own reference. + osContainerDestroy(w->content); + // TODO menus + // next, destroy the GtkWindow itself, which will destroy the vbox, menus, etc. gtk_widget_destroy(w->widget); + // finally, free ourselves uiFree(w); } @@ -106,9 +113,7 @@ static void windowSetChild(uiWindow *ww, uiControl *c) { struct window *w = (struct window *) ww; - // TODO make the update implicit - uiOSContainerSetMainControl(w->content, c); - uiOSContainerUpdate(w->content); + osContainerSetMainControl(w->content, c); } static int windowMargined(uiWindow *ww) @@ -125,10 +130,9 @@ static void windowSetMargined(uiWindow *ww, int margined) // TODO make the update implicit w->margined = margined; if (w->margined) - uiOSContainerSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin); + osContainerSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin); else - uiOSContainerSetMargins(w->content, 0, 0, 0, 0); - uiOSContainerUpdate(w->content); + osContainerSetMargins(w->content, 0, 0, 0, 0); } 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->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->vboxcontainer = GTK_CONTAINER(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))); // and add the OS container - w->content = uiNewOSContainer((uintptr_t) (w->vboxcontainer)); - w->contentWidget = GTK_WIDGET(uiOSContainerHandle(w->content)); + w->contentWidget = newOSContainer(); + w->content = uipOSContainer(w->contentWidget); gtk_widget_set_hexpand(w->contentWidget, TRUE); gtk_widget_set_halign(w->contentWidget, GTK_ALIGN_FILL); gtk_widget_set_vexpand(w->contentWidget, TRUE); 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 gtk_widget_show_all(w->vboxwidget);