Sun, 13 Oct 2024 11:05:38 +0200
implement gtk3 stack tabview
application/main.c | file | annotate | diff | comparison | revisions | |
ui/gtk/container.c | file | annotate | diff | comparison | revisions | |
ui/gtk/toolkit.h | file | annotate | diff | comparison | revisions |
--- a/application/main.c Wed Oct 09 21:43:11 2024 +0200 +++ b/application/main.c Sun Oct 13 11:05:38 2024 +0200 @@ -47,6 +47,7 @@ MyDocument *doc1; MyDocument *doc2; +UIWIDGET tabview; void action_menu(UiEvent *event, void *userdata) { @@ -191,6 +192,18 @@ ui_tab(obj, "Tab 2") { ui_button(obj, .label = "Button", .onclick=action_tab2_button); } + ui_tab(obj, "Tab 3 x") { + UiTabViewArgs args = {0}; + tabview = ui_tabview_create(obj, args); + + UiObject *tab1 = ui_tabview_add(tabview, "Sub 1", -1); + ui_button(tab1, .label = "Button 1"); + + + UiObject *tab2 = ui_tabview_add(tabview, "Sub 2", -1); + ui_button(tab2, .label = "Button 2"); + + } } /*
--- a/ui/gtk/container.c Wed Oct 09 21:43:11 2024 +0200 +++ b/ui/gtk/container.c Sun Oct 13 11:05:38 2024 +0200 @@ -361,6 +361,7 @@ } +#if GTK_MAJOR_VERSION >= 4 static int stack_set_page(GtkWidget *stack, int index) { GtkSelectionModel *pages = gtk_stack_get_pages(GTK_STACK(stack)); GListModel *list = G_LIST_MODEL(pages); @@ -416,6 +417,65 @@ i->value = value; } } +#elif GTK_MAJOR_VERSION >= 3 +static GtkWidget* stack_get_child(GtkWidget *stack, int index) { + GList *children = gtk_container_get_children(GTK_CONTAINER(stack)); + if(children) { + return g_list_nth_data(children, index); + } + return NULL; +} + +void ui_stack_tab_select(UIWIDGET tabview, int tab) { + GtkWidget *child = stack_get_child(tabview, tab); + if(child) { + gtk_stack_set_visible_child(GTK_STACK(tabview), child); + } +} + +void ui_stack_tab_remove(UIWIDGET tabview, int tab) { + GtkWidget *child = stack_get_child(tabview, tab); + if(child) { + gtk_container_remove(GTK_CONTAINER(tabview), child); + } +} + +void ui_stack_tab_add(UIWIDGET widget, int index, const char *name, UIWIDGET child) { + gtk_stack_add_titled(GTK_STACK(widget), child, name, name); +} + +int64_t ui_stack_get(UiInteger *i) { + GtkWidget *visible = gtk_stack_get_visible_child(GTK_STACK(i->obj)); + GList *children = gtk_container_get_children(GTK_CONTAINER(i->obj)); + GList *elm = children; + int n = 0; + int64_t v = -1; + while(elm) { + GtkWidget *child = elm->data; + if(child == visible) { + v = n; + break; + } + + elm = elm->next; + n++; + } + g_list_free(children); + i->value = v; + return v; +} + +void ui_stack_set(UiInteger *i, int64_t value) { + GtkWidget *child = stack_get_child(i->obj, value); + if(child) { + gtk_stack_set_visible_child(GTK_STACK(i->obj), child); + i->value = value; + } +} + +#endif + + UiGtkTabView* ui_widget_get_tabview_data(UIWIDGET tabview) { @@ -464,6 +524,7 @@ } case UI_TABVIEW_DEFAULT: /* fall through */ case UI_TABVIEW_NAVIGATION_TOP: /* fall through */ + case UI_TABVIEW_INVISIBLE: /* fall through */ case UI_TABVIEW_NAVIGATION_TOP2: { widget = gtk_notebook_new(); data_widget = widget; @@ -472,11 +533,10 @@ data->add_tab = ui_notebook_tab_add; getfunc = ui_notebook_get; setfunc = ui_notebook_set; - /* fall through */ - } - case UI_TABVIEW_INVISIBLE: { - gtk_notebook_set_show_tabs(GTK_NOTEBOOK(widget), FALSE); - gtk_notebook_set_show_border(GTK_NOTEBOOK(widget), FALSE); + if(args.tabview == UI_TABVIEW_INVISIBLE) { + gtk_notebook_set_show_tabs(GTK_NOTEBOOK(widget), FALSE); + gtk_notebook_set_show_border(GTK_NOTEBOOK(widget), FALSE); + } break; } }
--- a/ui/gtk/toolkit.h Wed Oct 09 21:43:11 2024 +0200 +++ b/ui/gtk/toolkit.h Sun Oct 13 11:05:38 2024 +0200 @@ -72,7 +72,7 @@ #define WINDOW_SHOW(window) gtk_widget_show_all(window) #define WINDOW_DESTROY(window) gtk_widget_destroy(window) #define WINDOW_SET_CONTENT(window, child) gtk_container_add(GTK_CONTAINER(window), child) -#define BOX_ADD(box, child) gtk_box_pack_end(GTK_BOX(box), child, TRUE, TRUE, 0) +#define BOX_ADD(box, child) gtk_container_add(GTK_CONTAINER(box), child) #define BOX_ADD_EXPAND(box, child) gtk_box_pack_end(GTK_BOX(box), child, TRUE, TRUE, 0) #define BOX_ADD_NO_EXPAND(box, child) gtk_box_pack_end(GTK_BOX(box), child, TRUE, FALSE, 0) #define ENTRY_SET_TEXT(entry, text) gtk_entry_set_text(GTK_ENTRY(entry), text)