add option to automatically enable states with toggle buttons (GTK) newapi

Sun, 27 Oct 2024 11:34:25 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 27 Oct 2024 11:34:25 +0100
branch
newapi
changeset 359
c51e58359db8
parent 358
98b26a19cbeb
child 360
681b930abe84

add option to automatically enable states with toggle buttons (GTK)

ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/button.h file | annotate | diff | comparison | revisions
ui/gtk/headerbar.c file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/button.c	Sun Oct 27 10:33:24 2024 +0100
+++ b/ui/gtk/button.c	Sun Oct 27 11:34:25 2024 +0100
@@ -151,6 +151,14 @@
     event->callback(&e, event->userdata);    
 }
 
+static void ui_togglebutton_enable_state_callback(GtkToggleButton *widget, UiEventData *event) {
+    if(gtk_toggle_button_get_active(widget)) {
+        ui_set_group(event->obj->ctx, event->value);
+    } else {
+        ui_unset_group(event->obj->ctx, event->value);
+    }
+}
+
 void ui_setup_togglebutton(
         UiObject *obj,
         GtkWidget *togglebutton,
@@ -159,7 +167,8 @@
         const char *varname,
         UiInteger *value,
         ui_callback onchange,
-        void *onchangedata)
+        void *onchangedata,
+        int enable_state)
 {
     if(label) {
         gtk_button_set_label(GTK_BUTTON(togglebutton), label);
@@ -175,7 +184,10 @@
             value,
             (ui_toggled_func)ui_toggled_callback,
             onchange,
-            onchangedata);
+            onchangedata,
+            (ui_toggled_func)ui_togglebutton_enable_state_callback,
+            enable_state
+            );
 }
 
 void ui_bind_togglebutton(
@@ -187,7 +199,9 @@
         UiInteger *value,
         void (*toggled_callback)(void*, void*),
         ui_callback onchange,
-        void *onchangedata)
+        void *onchangedata,
+        void (*enable_state_func)(void*, void*),
+        int enable_state)
 {
     UiObject* current = uic_current_obj(obj);
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, value, varname, UI_VAR_INTEGER);
@@ -235,12 +249,41 @@
                 G_CALLBACK(ui_destroy_userdata),
                 event);
     }
+    
+    if(enable_state > 0) {
+        UiEventData *event = malloc(sizeof(UiEventData));
+        event->obj = obj;
+        event->userdata = NULL;
+        event->callback = NULL;
+        event->value = enable_state;
+        event->customdata = NULL;
+        
+        g_signal_connect(
+                widget,
+                "toggled",
+                G_CALLBACK(enable_state_func),
+                event);
+        g_signal_connect(
+                widget,
+                "destroy",
+                G_CALLBACK(ui_destroy_userdata),
+                event);
+    }
 }
 
 static UIWIDGET togglebutton_create(UiObject *obj, GtkWidget *widget, UiToggleArgs args) {
     UiObject* current = uic_current_obj(obj);
     
-    ui_setup_togglebutton(current, widget, args.label, args.icon, args.varname, args.value, args.onchange, args.onchangedata);
+    ui_setup_togglebutton(
+            current,
+            widget,
+            args.label,
+            args.icon,
+            args.varname,
+            args.value,
+            args.onchange,
+            args.onchangedata,
+            args.enable_group);
     ui_set_name_and_style(widget, args.name, args.style_class);
     ui_set_widget_groups(obj->ctx, widget, args.groups);
     
@@ -278,6 +321,14 @@
     event->callback(&e, event->userdata);    
 }
 
+static void ui_checkbox_enable_state(GtkCheckButton *widget, UiEventData *event) {
+    if(gtk_check_button_get_active(widget)) {
+        ui_set_group(event->obj->ctx, event->value);
+    } else {
+        ui_unset_group(event->obj->ctx, event->value);
+    }
+}
+
 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
     UiObject* current = uic_current_obj(obj);
     
@@ -291,7 +342,9 @@
             args.value,
             (ui_toggled_func)ui_checkbox_callback,
             args.onchange,
-            args.onchangedata);
+            args.onchangedata,
+            (ui_toggled_func)ui_checkbox_enable_state,
+            args.enable_group);
     
     ui_set_name_and_style(widget, args.name, args.style_class);
     ui_set_widget_groups(obj->ctx, widget, args.groups);
--- a/ui/gtk/button.h	Sun Oct 27 10:33:24 2024 +0100
+++ b/ui/gtk/button.h	Sun Oct 27 11:34:25 2024 +0100
@@ -58,7 +58,8 @@
         const char *varname,
         UiInteger *value,
         ui_callback onchange,
-        void *onchangedata);
+        void *onchangedata,
+        int enable_state);
 
 void ui_bind_togglebutton(
         UiObject *obj,
@@ -69,7 +70,9 @@
         UiInteger *value,
         void (*toggled_callback)(void*, void*),
         ui_callback onchange,
-        void *onchangedata);
+        void *onchangedata,
+        void (*enable_state_func)(void*, void*),
+        int enable_state);
     
 // event wrapper
 void ui_button_clicked(GtkWidget *widget, UiEventData *event);
--- a/ui/gtk/headerbar.c	Sun Oct 27 10:33:24 2024 +0100
+++ b/ui/gtk/headerbar.c	Sun Oct 27 11:34:25 2024 +0100
@@ -128,7 +128,7 @@
 {
     GtkWidget *button = gtk_toggle_button_new();
     WIDGET_ADD_CSS_CLASS(button, "flat");
-    ui_setup_togglebutton(obj, button, item->args.label, item->args.icon, item->args.varname, NULL, item->args.onchange, item->args.onchangedata);
+    ui_setup_togglebutton(obj, button, item->args.label, item->args.icon, item->args.varname, NULL, item->args.onchange, item->args.onchangedata, 0);
     headerbar_add(headerbar, box, button, pos);
 }
 
--- a/ui/ui/button.h	Sun Oct 27 10:33:24 2024 +0100
+++ b/ui/ui/button.h	Sun Oct 27 11:34:25 2024 +0100
@@ -71,6 +71,7 @@
     const char* varname;
     ui_callback onchange;
     void* onchangedata;
+    int enable_group;
     
     const int* groups;
 } UiToggleArgs;

mercurial