# HG changeset patch # User Olaf Wintermann # Date 1766270221 -3600 # Node ID c609ca165ae7e58c74972a4bc12eb3f25189e830 # Parent ac1af881d8d43b298d669bbe4d978683de5fbe19 implement functions for accessing values by var name diff -r ac1af881d8d4 -r c609ca165ae7 ui/common/context.c --- a/ui/common/context.c Fri Dec 19 23:18:48 2025 +0100 +++ b/ui/common/context.c Sat Dec 20 23:37:01 2025 +0100 @@ -695,6 +695,91 @@ cxMempoolSetDestructor(mem, (cx_destructor_func)destr); } +void ui_var_set_int(UiContext *ctx, const char *name, int64_t value) { + UiInteger *i = ui_get_int_var(ctx, name); + if(i) { + ui_set(i, value); + } +} + +int64_t ui_var_get_int(UiContext *ctx, const char *name) { + UiInteger *i = ui_get_int_var(ctx, name); + if(i) { + return ui_get(i); + } + return 0; +} + +void ui_var_set_double(UiContext *ctx, const char *name, double value) { + UiDouble *d = ui_get_double_var(ctx, name); + if(d) { + ui_set(d, value); + } +} + +double ui_var_get_double(UiContext *ctx, const char *name) { + UiDouble *d = ui_get_double_var(ctx, name); + if(d) { + return ui_get(d); + } + return 0; +} + +void ui_var_set_string(UiContext *ctx, const char *name, char *value) { + UiString *s = ui_get_string_var(ctx, name); + if(s) { + ui_set(s, value); + } +} + +char* ui_var_get_string(UiContext *ctx, const char *name) { + UiString *s = ui_get_string_var(ctx, name); + if(s) { + return ui_get(s); + } + return NULL; +} + +UIEXPORT void ui_var_add_observer(UiContext *ctx, const char *varname, ui_callback f, void *data) { + UiVar *var = uic_get_var(ctx, varname); + if(!var) { + return; + } + + switch(var->type) { + case UI_VAR_INTEGER: { + UiInteger *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + case UI_VAR_DOUBLE: { + UiDouble *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + case UI_VAR_RANGE: { + UiRange *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + case UI_VAR_STRING: { + UiString *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + case UI_VAR_TEXT: { + UiText *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + case UI_VAR_LIST: { + UiList *v = var->value; + v->observers = ui_add_observer(v->observers, f, data); + break; + } + } +} + UiInteger* ui_get_int_var(UiContext *ctx, const char *name) { UiVar *var = uic_get_var_t(ctx, name, UI_VAR_INTEGER); return var ? var->value : NULL; diff -r ac1af881d8d4 -r c609ca165ae7 ui/ui/toolkit.h --- a/ui/ui/toolkit.h Fri Dec 19 23:18:48 2025 +0100 +++ b/ui/ui/toolkit.h Sat Dec 20 23:37:01 2025 +0100 @@ -644,6 +644,8 @@ UIEXPORT UiRange* ui_get_range_var(UiContext *ctx, const char *name); UIEXPORT UiGeneric* ui_get_generic_var(UiContext *ctx, const char *name); +UIEXPORT void ui_var_add_observer(UiContext *ctx, const char *varname, ui_callback f, void *data); + UIEXPORT UiObserver* ui_observer_new(ui_callback f, void *data); UIEXPORT UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer); UIEXPORT UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data);