ui/common/types.c

changeset 110
c00e968d018b
parent 109
c3dfcb8f0be7
child 113
dde28a806552
--- a/ui/common/types.c	Sun Aug 24 15:24:16 2025 +0200
+++ b/ui/common/types.c	Sat Oct 04 14:52:59 2025 +0200
@@ -220,6 +220,7 @@
     va_end(ap);
     
     size_t len = cxListSize(cols);
+    info->alloc = len;
     info->columns = len;
     info->types = ui_calloc(ctx, len, sizeof(UiModelType));
     info->titles = ui_calloc(ctx, len, sizeof(char*));
@@ -229,7 +230,7 @@
     CxIterator iter = cxListIterator(cols);
     cx_foreach(UiColumn*, c, iter) {
         info->types[i] = c->type;
-        info->titles[i] = c->name;
+        info->titles[i] = ui_strdup(ctx, c->name);
         i++;
     }
     cxListFree(cols);
@@ -237,6 +238,30 @@
     return info;
 }
 
+#define UI_MODEL_DEFAULT_ALLOC_SIZE 16
+
+UiModel* ui_model_new(UiContext *ctx) {
+    UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
+    info->alloc = UI_MODEL_DEFAULT_ALLOC_SIZE;
+    info->types = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(UiModelType));
+    info->titles = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(char*));
+    info->columnsize = ui_calloc(ctx, UI_MODEL_DEFAULT_ALLOC_SIZE, sizeof(int));
+    return info;
+}
+
+void ui_model_add_column(UiContext *ctx, UiModel *model, UiModelType type, const char *title, int width) {
+    if(model->columns >= model->alloc) {
+        model->alloc += UI_MODEL_DEFAULT_ALLOC_SIZE;
+        model->types = ui_realloc(ctx, model->types, model->alloc * sizeof(UiModelType));
+        model->titles = ui_realloc(ctx, model->titles, model->alloc * sizeof(char*));
+        model->columnsize = ui_realloc(ctx, model->columnsize, model->alloc * sizeof(int));
+    }
+    model->types[model->columns] = type;
+    model->titles[model->columns] = ui_strdup(ctx, title);
+    model->columnsize[model->columns] = width;
+    model->columns++;
+}
+
 UiModel* ui_model_copy(UiContext *ctx, UiModel* model) {
     const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
 
@@ -258,6 +283,9 @@
 
 void ui_model_free(UiContext *ctx, UiModel *mi) {
     const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
+    for(int i=0;i<mi->columns;i++) {
+        ui_free(ctx, mi->titles[i]);
+    }
     cxFree(a, mi->types);
     cxFree(a, mi->titles);
     cxFree(a, mi->columnsize);
@@ -389,7 +417,7 @@
         return s->get ? s->get(s) : s->value.ptr;
     }
     else {
-        return 0;
+        return NULL;
     }
 }
 
@@ -419,6 +447,69 @@
         return s->get ? s->get(s) : s->value.ptr;
     }
     else {
+        return NULL;
+    }
+}
+
+void ui_range_set(UiRange *r, double value) {
+    if (r) {
+        if (r->set) {
+            r->set(r, value);
+        } else {
+            r->value = value;
+        }
+    }
+}
+
+void ui_range_set_range(UiRange *r, double min, double max) {
+    if (r) {
+        if (r->setrange) {
+            r->setrange(r, min, max);
+        } else {
+            r->min = min;
+            r->max = max;
+        }
+    }
+}
+
+void ui_range_set_extent(UiRange *r, double extent) {
+    if (r) {
+        if (r->setextent) {
+            r->setextent(r, extent);
+        } else {
+            r->extent = extent;
+        }
+    }
+}
+
+double ui_range_get(UiRange *r) {
+    if (r) {
+        return r->get ? r->get(r) : r->value;
+    } else {
+        return 0;
+    }
+}
+
+double ui_range_get_min(UiRange *r) {
+    if (r) {
+        return r->min;
+    } else {
+        return 0;
+    }
+}
+
+double ui_range_get_max(UiRange *r) {
+    if (r) {
+        return r->max;
+    } else {
+        return 0;
+    }
+}
+
+double ui_range_get_extent(UiRange *r) {
+    if (r) {
+        return r->extent;
+    } else {
         return 0;
     }
 }
@@ -580,6 +671,8 @@
 
 void uic_list_unbind(UiList *l) {
     l->update = NULL;
+    l->getselection = NULL;
+    l->setselection = NULL;
     l->obj = NULL;
 }
 
@@ -599,10 +692,12 @@
 }
 
 UIEXPORT void ui_list_setselection(UiList *list, int index) {
-    if (list->setselection && index >= 0) {
-        UiListSelection sel;
-        sel.count = 1;
-        sel.rows = &index;
+    if (list->setselection) {
+        UiListSelection sel = { 0, NULL };
+        if(index >= 0) {
+            sel.count = 1;
+            sel.rows = &index;
+        }
         list->setselection(list, sel);
     }
 }

mercurial