fix wrong data index when dynamically adding columns

Mon, 08 Dec 2025 11:36:46 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 08 Dec 2025 11:36:46 +0100
changeset 979
471ca81a72f5
parent 978
71446363dcf9
child 980
39cb60b6a81b

fix wrong data index when dynamically adding columns

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/list.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Dec 08 10:53:56 2025 +0100
+++ b/application/main.c	Mon Dec 08 11:36:46 2025 +0100
@@ -475,7 +475,6 @@
 UiMenuBuilder *sourcelist_menu;
 
 void* table_getvalue(void *row, int col) {
-    return "test";
     switch(col) {
         case 0: return ui_foldericon(16);
         case 1: return row;
@@ -488,7 +487,7 @@
         case 8: return "edit me too";
         case 9: return (void*)(intptr_t)1;
     }
-    return NULL;
+    return "test";
 }
 
 UiBool table_getstyle(UiList *list, void *elm, int row, int col, void *userdata, UiTextStyle *style) {
@@ -577,7 +576,7 @@
     MyDocument *doc = event->document;
     
     char *colname = ui_get(doc->list_new_col);
-    ui_model_add_column(doc->model, UI_STRING, colname, 200);
+    ui_model_add_column(doc->model, UI_STRING_EDITABLE, colname, 200);
 }
 
 static void action_list_selection(UiEvent *event, void *userdata) {
@@ -707,8 +706,7 @@
             }
         }
         ui_tab(obj, "Tab 1") {
-            //UiModel *model = ui_model(obj->ctx, UI_ICON_TEXT, "col1", UI_INTEGER, "col2", UI_ICON, "col3", UI_ICON_TEXT, "col4", UI_INTEGER, "col5", UI_STRING_EDITABLE, "edit6", UI_STRING_EDITABLE, "edit7", UI_BOOL_EDITABLE, "Check", -1);
-            UiModel *model = ui_model(obj->ctx, UI_STRING, "Col", -1);
+            UiModel *model = ui_model(obj->ctx, UI_ICON_TEXT, "col1", UI_INTEGER, "col2", UI_ICON, "col3", UI_ICON_TEXT, "col4", UI_INTEGER, "col5", UI_STRING_EDITABLE, "edit6", UI_STRING_EDITABLE, "edit7", UI_BOOL_EDITABLE, "Check", -1);
             model->columnsize[0] = -1;
             doc->model = model;
             ui_table(obj, .model = model, .list = doc->list2, .colspan = 2, .fill = TRUE, .contextmenu = menubuilder, .multiselection = TRUE, .fill = TRUE,
--- a/ui/gtk/list.c	Mon Dec 08 10:53:56 2025 +0100
+++ b/ui/gtk/list.c	Mon Dec 08 11:36:46 2025 +0100
@@ -412,7 +412,7 @@
     }
 }
 
-static void column_factory_unbind(GtkSignalListItemFactory *self, GtkListItem *item, UiColData *col) {
+static void column_factory_unbind(GtkSignalListItemFactory *self, GtkListItem *item, UiColData *col) {  
     ObjWrapper *obj = gtk_list_item_get_item(item);
     UiListView *listview = col->listview;
     CxHashKey row_key = cx_hash_key(&obj->i, sizeof(int));
@@ -473,6 +473,7 @@
     GtkListItemFactory *factory = gtk_signal_list_item_factory_new();
     g_signal_connect(factory, "setup", G_CALLBACK(column_factory_setup), &listview->coldata);
     g_signal_connect(factory, "bind", G_CALLBACK(column_factory_bind), &listview->coldata);
+    g_signal_connect(factory, "unbind", G_CALLBACK(column_factory_unbind), &listview->coldata);
     
     GtkSelectionModel *selection_model = create_selection_model(listview, ls, args->multiselection);
     GtkWidget *view = gtk_list_view_new(GTK_SELECTION_MODEL(selection_model), factory);
@@ -757,11 +758,27 @@
     }
     
     gtk_column_view_set_model(GTK_COLUMN_VIEW(listview->widget), NULL);
+    cxMapClear(listview->bound_rows);
     
     if(insert_index) {
-        listview->columns[insert_index] = insert_index;
+        int prev = 0;
+        if(insert_index > 0) {
+            prev = listview->columns[insert_index-1];
+        }
+        listview->columns[insert_index] = prev+1;
         add_column(listview, insert_index);
-        // TODO: adjust data_column if insert_index < numcolumns
+        
+        if(insert_index+1 < listview->numcolumns) {
+            // the data index of trailing columns must be adjusted
+            UiModelType type = model->types[insert_index];
+            int add = 1;
+            if(type == UI_ICON_TEXT || type == UI_ICON_TEXT_FREE) {
+                add++;
+            }
+            for(int i=insert_index+1;i<listview->numcolumns;i++) {
+                listview->columns[i] += add;
+            }
+        }
     } // TODO: delete_index
     
     GListStore *ls = g_list_store_new(G_TYPE_OBJECT);
@@ -904,6 +921,7 @@
     UiListView *view = list->obj;
     view->current_row = -1;
     if(i < 0) {
+        cxMapClear(view->bound_rows);
         ui_update_liststore(view->liststore, list);
     } else {
         void *value = list->get(list, i);

mercurial