update button, togglebutton and checkbox to the new API (GTK) newapi

Mon, 12 Feb 2024 22:11:33 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Feb 2024 22:11:33 +0100
branch
newapi
changeset 257
a334f9ebc5d0
parent 256
8874a68bafb0
child 258
a252f68c665a

update button, togglebutton and checkbox to the new API (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Feb 12 17:32:47 2024 +0100
+++ b/application/main.c	Mon Feb 12 22:11:33 2024 +0100
@@ -67,7 +67,9 @@
     
     UiObject *obj = ui_window("Test", NULL);
     
-    
+    ui_button(obj, .label = "Test Button");
+    ui_togglebutton(obj, .label = "Toggle");
+    ui_checkbox(obj, .label = "Checkbox");
     
     ui_show(obj);
 }
--- a/ui/gtk/button.c	Mon Feb 12 17:32:47 2024 +0100
+++ b/ui/gtk/button.c	Mon Feb 12 22:11:33 2024 +0100
@@ -35,14 +35,15 @@
 #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);
+UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    GtkWidget *button = gtk_button_new_with_label(args.label);
     
-    if(f) {
+    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;
 
         g_signal_connect(
@@ -57,8 +58,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,16 +99,25 @@
     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);
+    
+    if(args.label) {
+        gtk_button_set_label(GTK_BUTTON(widget), args.label);
+    }
     
-    // bind value
-    if(var) {
-        UiInteger *value = var->value;
-        value->obj = GTK_TOGGLE_BUTTON(button);
+    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);
+    }
+    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;
@@ -115,39 +125,44 @@
         event->observers = 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) {
     GSList *rg = NULL;
     UiInteger *rgroup;

mercurial