implement progressbar min/max args (GTK) newapi

Sun, 29 Sep 2024 20:24:46 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 20:24:46 +0200
branch
newapi
changeset 319
050ceb8ecf44
parent 318
8d8a31d55dd5
child 320
be224cf1f50e

implement progressbar min/max args (GTK)

ui/gtk/display.c file | annotate | diff | comparison | revisions
--- a/ui/gtk/display.c	Sun Sep 29 19:06:29 2024 +0200
+++ b/ui/gtk/display.c	Sun Sep 29 20:24:46 2024 +0200
@@ -119,10 +119,27 @@
 
 /* ------------------------- progress bar ------------------------- */
 
+typedef struct UiProgressBarRange {
+    double min;
+    double max;
+} UiProgressBarRange;
+
 UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) {
     UiObject* current = uic_current_obj(obj);
     
     GtkWidget *progressbar = gtk_progress_bar_new();
+    if(args.max > args.min) {
+        UiProgressBarRange *range = malloc(sizeof(UiProgressBarRange));
+        range->min = args.min;
+        range->max = args.max;
+        g_signal_connect(
+                progressbar,
+                "destroy",
+                G_CALLBACK(ui_destroy_userdata),
+                range);
+        g_object_set_data(G_OBJECT(progressbar), "ui_range", range);
+    }
+    
     
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE);
     if(var && var->value) {
@@ -130,7 +147,7 @@
         value->get = ui_progressbar_get;
         value->set = ui_progressbar_set;
         value->obj = progressbar;
-        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 0.5);
+        ui_progressbar_set(value, value->value);
     }
     
     UI_APPLY_LAYOUT1(current, args);
@@ -140,13 +157,22 @@
 }
 
 double ui_progressbar_get(UiDouble *d) {
-    d->value = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(d->obj));
+    UiProgressBarRange *range = g_object_get_data(d->obj, "ui_range");
+    double fraction = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(d->obj));
+    if(range) {
+        fraction = range->min + (range->max - range->min) * fraction;
+    }
+    d->value = fraction;
     return d->value;
 }
 
 void ui_progressbar_set(UiDouble *d, double value) {
+    d->value = value;
+    UiProgressBarRange *range = g_object_get_data(d->obj, "ui_range");
+    if(range) {
+        value = (value - range->min) / (range->max - range->min);
+    }
     gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(d->obj), value);
-    d->value = value;
 }
 
 

mercurial