ui/gtk/button.c

changeset 32
e5f4d8af567e
parent 29
3fc287f06305
child 44
473954dc6b74
--- a/ui/gtk/button.c	Mon Feb 12 21:13:23 2024 +0100
+++ b/ui/gtk/button.c	Sun Jun 09 15:43:08 2024 +0200
@@ -35,15 +35,41 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
-UIWIDGET ui_button_deprecated(UiObject *obj, char *label, ui_callback f, void *data) {
-    GtkWidget *button = gtk_button_new_with_label(label);
+void ui_button_set_icon_name(GtkWidget *button, const char *icon) {
+    if(!icon) {
+        return;
+    }
     
-    if(f) {
+#ifdef UI_GTK4
+    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);
+    if(image) {
+        gtk_button_set_image(GTK_BUTTON(button), image);
+    }
+#else
+    // TODO
+#endif
+#endif
+}
+
+UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    GtkWidget *button = gtk_button_new();
+    if(args.label) {
+        gtk_button_set_label(GTK_BUTTON(button), args.label);
+    }
+    ui_button_set_icon_name(button, args.icon);
+
+    
+    if(args.onclick) {
         UiEventData *event = malloc(sizeof(UiEventData));
         event->obj = obj;
-        event->userdata = data;
-        event->callback = f;
+        event->userdata = args.onclickdata;
+        event->callback = args.onclick;
         event->value = 0;
+        event->customdata = NULL;
 
         g_signal_connect(
                 button,
@@ -57,8 +83,8 @@
                 event);
     }
     
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, button, FALSE);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, button, FALSE);
     
     return button;
 }
@@ -98,66 +124,90 @@
     ui_notify_evt(i->observers, &e);
 }
 
-UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) {
-    GtkWidget *button = gtk_check_button_new_with_label(label);
+static UIWIDGET togglebutton_create(UiObject *obj, GtkWidget *widget, UiToggleArgs args) {
+    UiObject* current = uic_current_obj(obj);
     
-    // bind value
-    if(var) {
-        UiInteger *value = var->value;
-        value->obj = GTK_TOGGLE_BUTTON(button);
+    if(args.label) {
+        gtk_button_set_label(GTK_BUTTON(widget), args.label);
+    }
+    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) {
+        UiInteger* value = (UiInteger*)var->value;
+        value->obj = widget;
         value->get = ui_toggle_button_get;
         value->set = ui_toggle_button_set;
-        gtk_toggle_button_set_active(value->obj, value->value);
         
         UiVarEventData *event = malloc(sizeof(UiVarEventData));
         event->obj = obj;
         event->var = var;
         event->observers = NULL;
+        event->callback = NULL;
+        event->userdata = NULL;
 
         g_signal_connect(
-                button,
+                widget,
                 "clicked",
                 G_CALLBACK(ui_toggled_obs),
                 event);
         g_signal_connect(
-                button,
+                widget,
                 "destroy",
                 G_CALLBACK(ui_destroy_vardata),
                 event);
     }
     
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, button, FALSE);
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, widget, FALSE);
     
-    return button;
+    return widget;
+}
+
+UIWIDGET ui_togglebutton_create(UiObject* obj, UiToggleArgs args) {
+    return togglebutton_create(obj, gtk_toggle_button_new(), args);
 }
 
-UIWIDGET ui_checkbox_deprecated(UiObject *obj, char *label, UiInteger *value) {
-    UiVar *var = NULL;
-    if(value) {
-        var = malloc(sizeof(UiVar));
-        var->value = value;
-        var->type = UI_VAR_SPECIAL;
-    }
-    return ui_checkbox_var(obj, label, var);
+UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
+    return togglebutton_create(obj, gtk_check_button_new(), args);
 }
 
-UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
-    return ui_checkbox_var(obj, label, var);
+UIWIDGET ui_switch_create(UiObject* obj, UiToggleArgs args) {
+#ifdef UI_GTK3
+    return NULL; // TODO
+#else
+    return ui_checkbox_create(obj, args);
+#endif
 }
 
 
-UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) {
+
+
+
+
+UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    
     GSList *rg = NULL;
     UiInteger *rgroup;
     
+    UiVar* var = NULL;
+    if (args.value) {
+        var = uic_create_value_var(current->ctx, args.value);
+    } else if (args.varname) {
+        var = uic_create_var(obj->ctx, args.varname, UI_VAR_INTEGER);
+    }
+    
+    UiBool first = FALSE;
     if(var) {
         rgroup = var->value;
         rg = rgroup->obj;
+        if(!rg) {
+            first = TRUE;
+        }
     }
     
-    GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label);
+    GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, args.label ? args.label : "");
     rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
     
     if(rgroup) {
@@ -171,17 +221,21 @@
         event->obj = obj;
         event->var = var;
         event->observers = NULL;
+        event->callback = NULL;
+        event->userdata = NULL;
         
         g_signal_connect(
                 rbutton,
                 "clicked",
                 G_CALLBACK(ui_radio_obs),
                 event);
-        g_signal_connect(
+        if(first) {
+            g_signal_connect(
                 rbutton,
                 "destroy",
                 G_CALLBACK(ui_destroy_vardata),
                 event);
+        }
     }
     
     UiContainer *ct = uic_get_current_container(obj);
@@ -190,21 +244,6 @@
     return rbutton;
 }
 
-UIWIDGET ui_radiobutton_deprecated(UiObject *obj, char *label, UiInteger *rgroup) {
-    UiVar *var = NULL;
-    if(rgroup) {
-        var = malloc(sizeof(UiVar));
-        var->value = rgroup;
-        var->type = UI_VAR_SPECIAL;
-    }
-    return ui_radiobutton_var(obj, label, var);
-}
-
-UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
-    return ui_radiobutton_var(obj, label, var);
-}
-
 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
     UiInteger *i = event->var->value;
     

mercurial