--- a/ui/gtk/entry.c Mon Feb 12 21:13:23 2024 +0100 +++ b/ui/gtk/entry.c Sun Jun 09 15:43:08 2024 +0200 @@ -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); + } }