implement ui_listview_destroy (Motif)

Sun, 07 Dec 2025 12:24:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 12:24:12 +0100
changeset 964
c563220d9aea
parent 963
90b349cdd47f
child 965
5d4419042d9b

implement ui_listview_destroy (Motif)

ui/common/types.c file | annotate | diff | comparison | revisions
ui/motif/list.c file | annotate | diff | comparison | revisions
ui/ui/tree.h file | annotate | diff | comparison | revisions
--- a/ui/common/types.c	Sun Dec 07 12:12:02 2025 +0100
+++ b/ui/common/types.c	Sun Dec 07 12:24:12 2025 +0100
@@ -212,6 +212,7 @@
 
 UiModel* ui_model(UiContext *ctx, ...) {
     UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
+    info->ctx = ctx;
     
     va_list ap;
     va_start(ap, ctx);
@@ -253,6 +254,7 @@
 
 UiModel* ui_model_new(UiContext *ctx) {
     UiModel *info = ui_calloc(ctx, 1, sizeof(UiModel));
+    info->ctx = ctx;
     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*));
@@ -277,6 +279,7 @@
     const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
 
     UiModel* newmodel = cxMalloc(a, sizeof(UiModel));
+    newmodel->ctx = ctx;
     *newmodel = *model;
 
     newmodel->types = cxCalloc(a, model->columns, sizeof(UiModelType));
@@ -292,8 +295,19 @@
     return newmodel;
 }
 
-void ui_model_free(UiContext *ctx, UiModel *mi) {
-    const CxAllocator* a = ctx ? ctx->allocator : cxDefaultAllocator;
+void ui_model_ref(UiModel *model) {
+    model->ref++;
+}
+
+void ui_model_unref(UiModel *model) {
+    if(--model->ref == 0) {
+        ui_model_free(model);
+    }
+}
+
+void ui_model_free(UiModel *mi) {
+    UiContext *ctx = mi->ctx;
+    const CxAllocator* a = ctx->allocator;
     for(int i=0;i<mi->columns;i++) {
         ui_free(ctx, mi->titles[i]);
     }
@@ -735,9 +749,7 @@
 }
 
 UIEXPORT void ui_listselection_free(UiListSelection selection) {
-    if (selection.rows) {
-        free(selection.rows);
-    }
+    free(selection.rows);
 }
 
 UIEXPORT UiStr ui_str(char *cstr) {
--- a/ui/motif/list.c	Sun Dec 07 12:12:02 2025 +0100
+++ b/ui/motif/list.c	Sun Dec 07 12:24:12 2025 +0100
@@ -130,7 +130,11 @@
 }
 
 void ui_listview_destroy(Widget w, UiListView *listview, XtPointer d) {
-    // TODO
+    ui_listselection_free(listview->current_selection);
+    if(listview->model) {
+        ui_model_unref(listview->model);
+    }
+    free(listview);
 }
 
 static void list_callback(UiObject *obj, UiListSelection sel, ui_callback callback, void *userdata) {
--- a/ui/ui/tree.h	Sun Dec 07 12:12:02 2025 +0100
+++ b/ui/ui/tree.h	Sun Dec 07 12:24:12 2025 +0100
@@ -68,6 +68,8 @@
 typedef UiBool (*ui_list_savefunc)(UiList *list, int row, int col, UiCellValue *value, void *userdata);
 
 struct UiModel {
+    UiContext *ctx;
+    
     /*
      * number of columns
      */
@@ -94,6 +96,11 @@
      * array of column size hints
      */
     int *columnsize;
+    
+    /*
+     * reference counter
+     */
+    int ref;
 };
 
 struct UiListCallbacks {
@@ -292,7 +299,9 @@
 UIEXPORT UiModel* ui_model_new(UiContext *ctx);
 UIEXPORT void ui_model_add_column(UiContext *ctx, UiModel *model, UiModelType type, const char *title, int width);
 UIEXPORT UiModel* ui_model_copy(UiContext *ctx, UiModel* model);
-UIEXPORT void ui_model_free(UiContext *ctx, UiModel *mi);
+UIEXPORT void ui_model_ref(UiModel *model);
+UIEXPORT void ui_model_unref(UiModel *model);
+UIEXPORT void ui_model_free(UiModel *mi);
 
 #define ui_listview(obj, ...) ui_listview_create(obj, &(UiListArgs) { __VA_ARGS__ } )
 #define ui_table(obj, ...) ui_table_create(obj, &(UiListArgs) { __VA_ARGS__ } )

mercurial