update spinner entry widget api (GTK) newapi

Fri, 16 Feb 2024 19:00:53 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 16 Feb 2024 19:00:53 +0100
branch
newapi
changeset 264
24d9a92fd048
parent 263
a0ea718ab1d8
child 265
3756725aeaf4

update spinner entry widget api (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/entry.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/entry.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Fri Feb 16 18:33:34 2024 +0100
+++ b/application/main.c	Fri Feb 16 19:00:53 2024 +0100
@@ -80,6 +80,10 @@
         
         ui_label(obj, .label = "Label Col 1", .align = UI_ALIGN_LEFT);
         ui_label(obj, .label = "Label Col 2", .align = UI_ALIGN_RIGHT);
+        ui_newline(obj);
+        
+        ui_spinner(obj, .step = 5);
+        
     }
     
     ui_show(obj);
--- a/ui/gtk/button.c	Fri Feb 16 18:33:34 2024 +0100
+++ b/ui/gtk/button.c	Fri Feb 16 19:00:53 2024 +0100
@@ -142,6 +142,8 @@
         event->obj = obj;
         event->var = var;
         event->observers = NULL;
+        event->callback = NULL;
+        event->userdata = NULL;
 
         g_signal_connect(
                 widget,
@@ -214,6 +216,8 @@
         event->obj = obj;
         event->var = var;
         event->observers = NULL;
+        event->callback = NULL;
+        event->userdata = NULL;
         
         g_signal_connect(
                 rbutton,
--- a/ui/gtk/entry.c	Fri Feb 16 18:33:34 2024 +0100
+++ b/ui/gtk/entry.c	Fri Feb 16 19:00:53 2024 +0100
@@ -35,65 +35,46 @@
 #include "entry.h"
 
 
-UIWIDGET ui_spinner(UiObject *obj, int step, UiInteger *i) {
-    UiVar *var = malloc(sizeof(UiVar));
-    var->value = i;
-    var->type = UI_VAR_SPECIAL;
-    return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
-}
-
-UIWIDGET ui_spinnerf(UiObject *obj, double step, int digits, UiDouble *d) {
-    UiVar *var = malloc(sizeof(UiVar));
-    var->value = d;
-    var->type = UI_VAR_SPECIAL;
-    return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
-}
-
-UIWIDGET ui_spinnerr(UiObject *obj, UiRange *r) {
-    UiVar *var = malloc(sizeof(UiVar));
-    var->value = r;
-    var->type = UI_VAR_SPECIAL;
-    return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
-}
-
-UIWIDGET ui_spinner_nv(UiObject *obj, int step, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
-    return ui_spinner_var(obj, step, 0, var, UI_VAR_INTEGER);
-}
-
-UIWIDGET ui_spinnerf_nv(UiObject *obj, double step, int digits, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_DOUBLE);
-    return ui_spinner_var(obj, step, digits, var, UI_VAR_DOUBLE);
-}
-
-UIWIDGET ui_spinnerr_nv(UiObject *obj, char *varname) {
-    UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_RANGE);
-    UiRange *r = var->value;
-    return ui_spinner_var(obj, r->extent, 1, var, UI_VAR_RANGE);
-}
-
-UIWIDGET ui_spinner_var(UiObject *obj, double step, int digits, UiVar *var, UiVarType type) {
+UIWIDGET ui_spinner_create(UiObject *obj, UiSpinnerArgs args) {
     double min = 0;
     double max = 1000;
-    if(type == UI_VAR_RANGE) {
+    
+    UiObject* current = uic_current_obj(obj);
+    
+    UiVar *var = NULL;
+    if(args.varname) {
+        var = uic_get_var(obj->ctx, args.varname);
+    }
+    
+    if(!var) {
+        if(args.intvalue) {
+            var = uic_widget_var(obj->ctx, current->ctx, args.intvalue, NULL, UI_VAR_INTEGER);
+        } else if(args.doublevalue) {
+            var = uic_widget_var(obj->ctx, current->ctx, args.doublevalue, NULL, UI_VAR_DOUBLE);
+        } else if(args.rangevalue) {
+            var = uic_widget_var(obj->ctx, current->ctx, args.rangevalue, NULL, UI_VAR_RANGE);
+        }
+    }
+    
+    if(var && var->type == UI_VAR_RANGE) {
         UiRange *r = var->value;
         min = r->min;
         max = r->max;
     }
-    if(step == 0) {
-        step = 1;
+    if(args.step == 0) {
+        args.step = 1;
     }
 #ifdef UI_GTK2LEGACY
     if(min == max) {
         max = min + 1;
     }
 #endif
-    GtkWidget *spin = gtk_spin_button_new_with_range(min, max, step);
-    gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
+    GtkWidget *spin = gtk_spin_button_new_with_range(min, max, args.step);
+    gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), args.digits);
+    UiObserver **obs = NULL;
     if(var) {
         double value = 0;
-        UiObserver **obs = NULL;
-        switch(type) {
+        switch(var->type) {
             default: break;
             case UI_VAR_INTEGER: {
                 UiInteger *i = var->value;
@@ -126,26 +107,28 @@
             }
         }
         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
-        
-        UiVarEventData *event = malloc(sizeof(UiVarEventData));
-        event->obj = obj;
-        event->var = var;
-        event->observers = obs;
-        
-        g_signal_connect(
-                spin,
-                "value-changed",
-                G_CALLBACK(ui_spinner_changed),
-                event);
-        g_signal_connect(
-                spin,
-                "destroy",
-                G_CALLBACK(ui_destroy_vardata),
-                event);
     }
     
-    UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, spin, FALSE);
+    UiVarEventData *event = malloc(sizeof(UiVarEventData));
+    event->obj = obj;
+    event->var = var;
+    event->observers = obs;
+    event->callback = args.onchange;
+    event->userdata = args.onchangedata;
+
+    g_signal_connect(
+            spin,
+            "value-changed",
+            G_CALLBACK(ui_spinner_changed),
+            event);
+    g_signal_connect(
+            spin,
+            "destroy",
+            G_CALLBACK(ui_destroy_vardata),
+            event);
+    
+    UI_APPLY_LAYOUT1(current, args);
+    current->container->add(current->container, spin, FALSE);
     
     return spin;
 }
@@ -160,15 +143,22 @@
 
 
 void ui_spinner_changed(GtkSpinButton *spinner, UiVarEventData *event) {
+    gdouble value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinner));
     UiEvent e;
     e.obj = event->obj;
     e.window = event->obj->window;
     e.document = event->obj->ctx->document;
-    e.eventdata = event->var->value;
-    e.intval = 0;
+    e.eventdata = &value;
+    e.intval = (int64_t)value;
     
-    UiObserver *observer = *event->observers;
-    ui_notify_evt(observer, &e);
+    if(event->callback) {
+        event->callback(&e, event->userdata);
+    }
+    
+    if(event->observers) {
+        UiObserver *observer = *event->observers;
+        ui_notify_evt(observer, &e);
+    }
 }
 
 
--- a/ui/gtk/toolkit.c	Fri Feb 16 18:33:34 2024 +0100
+++ b/ui/gtk/toolkit.c	Fri Feb 16 19:00:53 2024 +0100
@@ -247,7 +247,9 @@
 }
 
 void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data) {
-    ui_destroy_boundvar(data->obj->ctx, data->var);
+    if(data->var) {
+        ui_destroy_boundvar(data->obj->ctx, data->var);
+    }
     free(data);
 }
 
--- a/ui/gtk/toolkit.h	Fri Feb 16 18:33:34 2024 +0100
+++ b/ui/gtk/toolkit.h	Fri Feb 16 19:00:53 2024 +0100
@@ -47,9 +47,11 @@
 } UiEventData;
 
 typedef struct UiVarEventData {
-    UiObject   *obj;
-    UiVar     *var;
-    UiObserver **observers;
+    UiObject    *obj;
+    UiVar       *var;
+    UiObserver  **observers;
+    ui_callback callback;
+    void        *userdata;
 } UiVarEventData;
 
 
--- a/ui/ui/entry.h	Fri Feb 16 18:33:34 2024 +0100
+++ b/ui/ui/entry.h	Fri Feb 16 19:00:53 2024 +0100
@@ -35,13 +35,29 @@
 extern "C" {
 #endif
 
-UIWIDGET ui_spinner(UiObject *obj, int step, UiInteger *i);
-UIWIDGET ui_spinnerf(UiObject *obj, double step, int digits, UiDouble *d);
-UIWIDGET ui_spinnerr(UiObject *obj, UiRange *r);
+
+typedef struct UiSpinnerArgs {
+    UiTri fill;
+    UiBool hexpand;
+    UiBool vexpand;
+    int colspan;
+    int rowspan;
 
-UIWIDGET ui_spinner_nv(UiObject *obj, int step, char *varname);
-UIWIDGET ui_spinnerf_nv(UiObject *obj, double step, int digits, char *varname);
-UIWIDGET ui_spinnerr_nv(UiObject *obj, char *varname);
+    double step;
+    int digits;
+    UiInteger *intvalue;
+    UiDouble* doublevalue;
+    UiRange *rangevalue;
+    const char* varname;
+    ui_callback onchange;
+    void* onchangedata;
+} UiSpinnerArgs;
+
+
+    
+UIWIDGET ui_spinner_create(UiObject *obj, UiSpinnerArgs args);
+
+#define ui_spinner(obj, ...) ui_spinner_create(obj, (UiSpinnerArgs){ __VA_ARGS__ } )
 
 void ui_spinner_setrange(UIWIDGET spinner, double min, double max);
 void ui_spinner_setdigits(UIWIDGET spinner, int digits);

mercurial