Reworked the GTK+ uiTab code to use a GArray of struct tabPages to store the tab page information.

This commit is contained in:
Pietro Gagliardi 2015-04-29 01:20:09 -04:00
parent ee998a973b
commit ae66137d01
1 changed files with 28 additions and 31 deletions

View File

@ -6,25 +6,30 @@ struct tab {
GtkWidget *widget; GtkWidget *widget;
GtkContainer *container; GtkContainer *container;
GtkNotebook *notebook; GtkNotebook *notebook;
// TODO switch to GArray or GPtrArray GArray *pages;
uiContainer **pages; };
uintmax_t len;
uintmax_t cap; struct tabPage {
uiContainer *bin;
GtkWidget *binWidget;
}; };
static void onDestroy(void *data) static void onDestroy(void *data)
{ {
struct tab *t = (struct tab *) data; struct tab *t = (struct tab *) data;
uintmax_t i; guint i;
struct tabPage *p;
// first hide ourselves to avoid flicker // first hide ourselves to avoid flicker
gtk_widget_hide(t->widget); gtk_widget_hide(t->widget);
// the pages do not have a libui parent, so we can simply destroy them // the pages do not have a libui parent, so we can simply destroy them
// TODO verify if this is sufficient // TODO verify if this is sufficient
for (i = 0; i < t->len; i++) for (i = 0; i < t->pages->len; i++) {
uiControlDestroy(uiControl(t->pages[i])); p = &g_array_index(t->pages, struct tabPage, i);
uiControlDestroy(uiControl(p->bin));
}
// then free ourselves // then free ourselves
uiFree(t->pages); g_array_free(t->pages, TRUE);
uiFree(t); uiFree(t);
} }
@ -33,45 +38,35 @@ static void onDestroy(void *data)
static void tabAppendPage(uiTab *tt, const char *name, uiControl *child) static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
{ {
struct tab *t = (struct tab *) tt; struct tab *t = (struct tab *) tt;
uiContainer *page; struct tabPage p;
GtkWidget *pageWidget;
if (t->len >= t->cap) { p.bin = newBin();
t->cap += tabCapGrow; binSetMainControl(p.bin, child);
t->pages = (uiContainer **) uiRealloc(t->pages, t->cap * sizeof (uiContainer *), "uiContainer *[]");
}
page = newBin();
binSetMainControl(page, child);
// and add it as a tab page // and add it as a tab page
binSetParent(page, (uintptr_t) (t->container)); binSetParent(p.bin, (uintptr_t) (t->container));
pageWidget = GTK_WIDGET(uiControlHandle(uiControl(page))); p.binWidget = GTK_WIDGET(uiControlHandle(uiControl(p.bin)));
gtk_notebook_set_tab_label_text(t->notebook, pageWidget, name); gtk_notebook_set_tab_label_text(t->notebook, p.binWidget, name);
t->pages[t->len] = page; g_array_append_val(t->pages, p);
t->len++;
} }
static void tabDeletePage(uiTab *tt, uintmax_t n) static void tabDeletePage(uiTab *tt, uintmax_t n)
{ {
struct tab *t = (struct tab *) tt; struct tab *t = (struct tab *) tt;
uiContainer *page; struct tabPage *p;
uintmax_t i;
page = t->pages[n]; p = &g_array_index(t->pages, struct tabPage, 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 // make sure the page's control isn't destroyed
binSetMainControl(page, NULL); binSetMainControl(p->bin, NULL);
// now destroy the page // now destroy the page
// this will also remove the tab // this will also remove the tab
// why? simple: both gtk_notebook_remove_tab() and gtk_widget_destroy() call gtk_container_remove() // why? simple: both gtk_notebook_remove_tab() and gtk_widget_destroy() call gtk_container_remove()
// TODO verify this // TODO verify this
uiControlDestroy(uiControl(page)); uiControlDestroy(uiControl(p->bin));
g_array_remove_index(t->pages, n);
} }
uiTab *uiNewTab(void) uiTab *uiNewTab(void)
@ -84,6 +79,8 @@ uiTab *uiNewTab(void)
FALSE, FALSE, onDestroy, t, FALSE, FALSE, onDestroy, t,
NULL); NULL);
t->pages = g_array_new(FALSE, TRUE, sizeof (struct tabPage));
t->widget = GTK_WIDGET(uiControlHandle(uiControl(t))); t->widget = GTK_WIDGET(uiControlHandle(uiControl(t)));
t->container = GTK_CONTAINER(t->widget); t->container = GTK_CONTAINER(t->widget);
t->notebook = GTK_NOTEBOOK(t->widget); t->notebook = GTK_NOTEBOOK(t->widget);