ui/common/context.c

changeset 110
c00e968d018b
parent 108
77254bd6dccb
child 112
c3f2f16fa4b8
--- a/ui/common/context.c	Sun Aug 24 15:24:16 2025 +0200
+++ b/ui/common/context.c	Sat Oct 04 14:52:59 2025 +0200
@@ -38,6 +38,7 @@
 
 #include "context.h"
 #include "../ui/window.h"
+#include "../ui/widget.h"
 #include "document.h"
 #include "types.h"
 
@@ -102,21 +103,19 @@
     UiContext *doc_ctx = ui_document_context(document);
     doc_ctx->parent = ctx;
     
-    // check if any parent context has an unbound variable with the same name
-    // as any document variable
+    // if a document variable has the same name as a parent variable,
+    // move the bindings to the document
     UiContext *var_ctx = ctx;
     while(var_ctx) {
-        if(var_ctx->vars_unbound &&  cxMapSize(var_ctx->vars_unbound) > 0) {
-            CxMapIterator i = cxMapIterator(var_ctx->vars_unbound);
-            cx_foreach(CxMapEntry*, entry, i) {
-                printf("attach %s\n", entry->key->data);
-                UiVar *var = entry->value;
-                UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key);
-                if(docvar) {
-                    // bind var to document var
-                    uic_copy_binding(var, docvar, TRUE);
-                    cxIteratorFlagRemoval(i);
-                }
+        CxMapIterator i = cxMapIterator(var_ctx->vars);
+        cx_foreach(CxMapEntry*, entry, i) {
+            printf("attach %.*s\n", (int)entry->key->len, entry->key->data);
+            UiVar *var = entry->value;
+            UiVar *docvar = cxMapGet(doc_ctx->vars, *entry->key);
+            if(docvar) {
+                // bind var to document var
+                uic_copy_binding(var, docvar, TRUE);
+                cxIteratorFlagRemoval(i);
             }
         }
         
@@ -129,10 +128,10 @@
     cx_foreach(CxMapEntry*, entry, mi) {
         UiVar *var = entry->value;
         // var->from && var->from_ctx && var->from_ctx != ctx
+        uic_save_var(var);
         if(var->from) {
-            uic_save_var2(var);
             uic_copy_binding(var, var->from, FALSE);
-            cxMapPut(var->from->from_ctx->vars_unbound, *entry->key, var->from);
+            cxMapPut(var->from->from_ctx->vars, *entry->key, var->from);
             var->from = NULL;
         }
     }
@@ -199,13 +198,6 @@
 }
 
 UiVar* uic_create_var(UiContext *ctx, const char *name, UiVarType type) {
-    if(ctx->vars_unbound) {
-        UiVar *unbound = cxMapGet(ctx->vars_unbound, name);
-        if(unbound) {
-            return unbound;
-        }
-    }
-    
     UiVar *var = uic_get_var(ctx, name);
     if(var) {
         if(var->type == type) {
@@ -224,10 +216,7 @@
 
     cxMempoolSetDestructor(var, (cx_destructor_func)uic_unbind_var);
 
-    if(!ctx->vars_unbound) {
-        ctx->vars_unbound = cxHashMapCreate(ctx->allocator, CX_STORE_POINTERS, 16);
-    }
-    cxMapPut(ctx->vars_unbound, name, var);
+    cxMapPut(ctx->vars, name, var);
     
     return var;
 }
@@ -278,7 +267,7 @@
 }
 
 
-UiVar* uic_widget_var(UiContext* toplevel, UiContext* current, void* value, const char* varname, UiVarType type) {
+UiVar* uic_widget_var(UiContext *toplevel, UiContext *current, void *value, const char *varname, UiVarType type) {
     if (value) {
         return uic_create_value_var(current, value);
     }
@@ -383,7 +372,7 @@
     ui_setop_enable(FALSE);
 }
 
-void uic_save_var2(UiVar *var) {
+void uic_save_var(UiVar *var) {
     switch(var->type) {
         case UI_VAR_SPECIAL: break;
         case UI_VAR_INTEGER: uic_int_save(var->value); break;
@@ -542,6 +531,9 @@
 }
 
 void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
+    if(enable == NULL) {
+        enable = (ui_enablefunc)ui_set_enabled;
+    }
     // get groups
     CxList *groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
     va_list ap;
@@ -557,6 +549,22 @@
     cxListFree(groups);
 }
 
+void ui_widget_set_groups2(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, int *groups, int ngroups) {
+    if(enable == NULL) {
+        enable = (ui_enablefunc)ui_set_enabled;
+    }
+    CxList *ls = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), ngroups);
+    for(int i=0;i<ngroups;i++) {
+        cxListAdd(ls, groups+i);
+    }
+    uic_add_group_widget(ctx, widget, enable, ls);
+    cxListFree(ls);
+}
+
+void ui_widget_set_visibility_states(UiContext *ctx, UIWIDGET widget, int *states, int nstates) {
+    ui_widget_set_groups2(ctx, widget, (ui_enablefunc)ui_set_visible, states, nstates);
+}
+
 size_t uic_group_array_size(const int *groups) {
     int i;
     for(i=0;groups[i] >= 0;i++) { }

mercurial