add initial support for headerbars (GTK) newapi

Sun, 31 Mar 2024 18:29:06 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 31 Mar 2024 18:29:06 +0200
branch
newapi
changeset 276
376921880a7f
parent 275
132c7bcc6997
child 277
5099a34747c4

add initial support for headerbars (GTK)

ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/button.h file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/gtk/toolbar.h file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
--- a/ui/gtk/button.c	Sun Mar 31 16:19:01 2024 +0200
+++ b/ui/gtk/button.c	Sun Mar 31 18:29:06 2024 +0200
@@ -35,13 +35,13 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
-static void button_set_icon(GtkWidget *button, const char *icon) {
+void ui_button_set_icon_name(GtkWidget *button, const char *icon) {
     if(!icon) {
         return;
     }
     
 #ifdef UI_GTK4
-    gtk_button_set_icon_name(GTK_BUTTON(button), args.icon);
+    gtk_button_set_icon_name(GTK_BUTTON(button), icon);
 #else
 #if GTK_CHECK_VERSION(2, 6, 0)
     GtkWidget *image = gtk_image_new_from_icon_name(icon, GTK_ICON_SIZE_BUTTON);
@@ -60,7 +60,7 @@
     if(args.label) {
         gtk_button_set_label(GTK_BUTTON(button), args.label);
     }
-    button_set_icon(button, args.icon);
+    ui_button_set_icon_name(button, args.icon);
 
     
     if(args.onclick) {
@@ -129,7 +129,7 @@
     if(args.label) {
         gtk_button_set_label(GTK_BUTTON(widget), args.label);
     }
-    button_set_icon(widget, args.icon);
+    ui_button_set_icon_name(widget, args.icon);
     
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER);
     if (var) {
--- a/ui/gtk/button.h	Sun Mar 31 16:19:01 2024 +0200
+++ b/ui/gtk/button.h	Sun Mar 31 18:29:06 2024 +0200
@@ -37,6 +37,8 @@
 extern "C" {
 #endif
 
+void ui_button_set_icon_name(GtkWidget *button, const char *icon_name);
+    
 // event wrapper
 void ui_button_clicked(GtkWidget *widget, UiEventData *event);
 
--- a/ui/gtk/toolbar.c	Sun Mar 31 16:19:01 2024 +0200
+++ b/ui/gtk/toolbar.c	Sun Mar 31 18:29:06 2024 +0200
@@ -345,3 +345,74 @@
 }
 */
 
+
+
+#ifdef UI_GTK3
+
+GtkWidget* ui_create_headerbar(UiObject *obj) {
+    GtkWidget *headerbar = gtk_header_bar_new();
+    
+    CxMap *items = uic_get_toolbar_items();
+    CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT);
+    CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER);
+    CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT);
+    
+    ui_headerbar_add_items(obj, headerbar, items, left_defaults);
+    ui_headerbar_add_items(obj, headerbar, items, center_defaults);
+    ui_headerbar_add_items(obj, headerbar, items, right_defaults);
+    
+    return headerbar;
+}
+
+static void hb_create_item(UiObject *obj, GtkWidget *toolbar, UiToolbarItemI *i) {
+    GtkHeaderBar *tb = GTK_HEADER_BAR(toolbar);
+    switch(i->type) {
+        case UI_TOOLBAR_ITEM: {
+            add_headerbar_item_widget(tb, (UiToolbarItem*)i, obj);
+            break;
+        }
+        case UI_TOOLBAR_TOGGLEITEM: {
+            add_headerbar_item_toggle_widget(tb, (UiToolbarToggleItem*)i, obj);
+            break;
+        }
+        case UI_TOOLBAR_MENU: {
+            add_headerbar_item_menu_widget(tb, (UiToolbarMenuItem*)i, obj);
+            break;
+        }
+        default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type);
+    }
+}
+
+
+void ui_headerbar_add_items(UiObject *obj, GtkWidget *headerbar, CxMap *items, CxList *defaults) {
+    // add pre-configured items
+    CxIterator i = cxListIterator(defaults);
+    cx_foreach(char*, def, i) {
+        UiToolbarItemI* item = uic_toolbar_get_item(def);
+        if (!item) {
+            fprintf(stderr, "unknown toolbar item: %s\n", def);
+            continue;
+        }
+        hb_create_item(obj, headerbar, item);
+    }
+}
+
+void add_headerbar_item_widget(GtkHeaderBar *hb, UiToolbarItem *item, UiObject *obj) {
+    GtkWidget *button = gtk_button_new_with_label(item->args.label);
+    if(item->args.icon) {
+        ui_button_set_icon_name(button, item->args.icon);
+    }
+    
+    gtk_header_bar_pack_start(hb, button);
+    
+}
+
+void add_headerbar_item_toggle_widget(GtkHeaderBar *hb, UiToolbarToggleItem *item, UiObject *obj) {
+    
+}
+
+void add_headerbar_item_menu_widget(GtkHeaderBar *hb, UiToolbarMenuItem *item, UiObject *obj) {
+    
+}
+
+#endif
--- a/ui/gtk/toolbar.h	Sun Mar 31 16:19:01 2024 +0200
+++ b/ui/gtk/toolbar.h	Sun Mar 31 18:29:06 2024 +0200
@@ -119,6 +119,19 @@
 void add_toolitem_toggle_widget(GtkToolbar *tb, UiToolbarToggleItem *item, UiObject *obj);
 void add_toolitem_menu_widget(GtkToolbar *tb, UiToolbarMenuItem *item, UiObject *obj);
 
+void ui_tool_button_toggled(GtkToggleToolButton *widget, UiVarEventData *event);
+int64_t ui_tool_toggle_button_get(UiInteger *integer);
+void ui_tool_toggle_button_set(UiInteger *integer, int64_t value);
+
+GtkWidget* ui_create_headerbar(UiObject *obj);
+
+void ui_headerbar_add_items(UiObject *obj, GtkWidget *headerbar, CxMap *items, CxList *defaults);
+
+void add_headerbar_item_widget(GtkHeaderBar *hb, UiToolbarItem *item, UiObject *obj);
+void add_headerbar_item_toggle_widget(GtkHeaderBar *hb, UiToolbarToggleItem *item, UiObject *obj);
+void add_headerbar_item_menu_widget(GtkHeaderBar *hb, UiToolbarMenuItem *item, UiObject *obj);
+
+
 /*
 void add_toolbar_combobox(GtkToolbar *tb, UiToolbarComboBox *cb, UiObject *obj);
 void add_toolbar_combobox_nv(GtkToolbar *tb, UiToolbarComboBoxNV *cb, UiObject *obj);
@@ -126,10 +139,6 @@
 void ui_combobox_update(UiEvent *event, void *combobox);
 */
 
-void ui_tool_button_toggled(GtkToggleToolButton *widget, UiVarEventData *event);
-int64_t ui_tool_toggle_button_get(UiInteger *integer);
-void ui_tool_toggle_button_set(UiInteger *integer, int64_t value);
-
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/gtk/window.c	Sun Mar 31 16:19:01 2024 +0200
+++ b/ui/gtk/window.c	Sun Mar 31 18:29:06 2024 +0200
@@ -126,6 +126,9 @@
                 gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0);
             }
         }
+        
+        //GtkWidget *hb = ui_create_headerbar(obj);
+        //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb);
     }
     
     // window content

mercurial