Added the GTK+ implementation of uiTab, for the most part I think.

This commit is contained in:
Pietro Gagliardi 2015-04-29 00:57:51 -04:00
parent ca1103e347
commit 390899f803
2 changed files with 27 additions and 16 deletions

View File

@ -7,6 +7,7 @@ osCFILES = \
unix/main.c \
unix/menu.c \
unix/newcontrol.c \
unix/tab.c \
unix/text.c \
unix/util.c \
unix/window.c

View File

@ -6,7 +6,8 @@ struct tab {
GtkWidget *widget;
GtkContainer *container;
GtkNotebook *notebook;
uiParent **pages;
// TODO switch to GArray or GPtrArray
uiContainer **pages;
uintmax_t len;
uintmax_t cap;
};
@ -16,52 +17,61 @@ static void onDestroy(void *data)
struct tab *t = (struct tab *) data;
uintmax_t i;
// first hide ourselves to avoid flicker
gtk_widget_hide(t->widget);
// the pages do not have a libui parent, so we can simply destroy them
// TODO verify if this is sufficient
for (i = 0; i < t->len; i++)
uiParentDestroy(t->pages[i]);
uiControlDestroy(uiControl(t->pages[i]));
// then free ourselves
uiFree(t->pages);
uiFree(t);
}
#define tabCapGrow 32
static void tabAddPage(uiTab *tt, const char *name, uiControl *child)
static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
{
struct tab *t = (struct tab *) tt;
uiParent *content;
uiContainer *page;
GtkWidget *pageWidget;
if (t->len >= t->cap) {
t->cap += tabCapGrow;
t->pages = (uiParent **) uiRealloc(t->pages, t->cap * sizeof (uiParent *), "uiParent *[]");
t->pages = (uiContainer **) uiRealloc(t->pages, t->cap * sizeof (uiContainer *), "uiContainer *[]");
}
content = uiNewParent((uintptr_t) (t->container));
uiParentSetMainControl(content, child);
uiParentUpdate(content);
gtk_notebook_set_tab_label_text(t->notebook, GTK_WIDGET(uiParentHandle(content)), name);
page = newBin();
binSetMainControl(page, child);
// and add it as a tab page
binSetParent(page, (uintptr_t) (t->container));
pageWidget = GTK_WIDGET(uiControlHandle(uiControl(page)));
gtk_notebook_set_tab_label_text(t->notebook, pageWidget, name);
t->pages[t->len] = content;
t->pages[t->len] = page;
t->len++;
}
static void tabDeletePage(uiTab *tt, uintmax_t n)
{
struct tab *t = (struct tab *) tt;
uiParent *p;
uiContainer *page;
uintmax_t i;
p = t->pages[n];
page = t->pages[n];
for (i = n; i < t->len - 1; i++)
t->pages[i] = t->pages[i + 1];
t->pages[i] = NULL;
t->len--;
// make sure the page's control isn't destroyed
uiParentSetMainControl(p, NULL);
binSetMainControl(page, NULL);
// now destroy the page
// this will also remove the tab
// why? simple: both gtk_notebook_remove_tab() and gtk_widget_destroy() call gtk_container_remove()
uiParentDestroy(p);
// TODO verify this
uiControlDestroy(uiControl(page));
}
uiTab *uiNewTab(void)
@ -74,11 +84,11 @@ uiTab *uiNewTab(void)
FALSE, FALSE, onDestroy, t,
NULL);
t->widget = WIDGET(t);
t->widget = GTK_WIDGET(uiControlHandle(uiControl(t)));
t->container = GTK_CONTAINER(t->widget);
t->notebook = GTK_NOTEBOOK(t->widget);
uiTab(t)->AddPage = tabAddPage;
uiTab(t)->AppendPage = tabAppendPage;
uiTab(t)->DeletePage = tabDeletePage;
return uiTab(t);