implement sourcelist get/set selection list methods (gtk)

Wed, 26 Nov 2025 19:39:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Nov 2025 19:39:37 +0100
changeset 937
06e03c7e39db
parent 936
d40a72210be8
child 938
be4c88ded783

implement sourcelist get/set selection list methods (gtk)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/list.c file | annotate | diff | comparison | revisions
ui/gtk/list.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Tue Nov 25 12:58:28 2025 +0100
+++ b/application/main.c	Wed Nov 26 19:39:37 2025 +0100
@@ -337,6 +337,7 @@
     ui_list_append(doc->srclist1, "test3");
     ui_list_append(doc->srclist2, "x1");
     ui_list_append(doc->srclist2, "x2");
+    ui_list_append(doc->srclist2, "select 0");
     
     doc->items = ui_list_new(docctx, "items");
     ui_list_append(doc->items, "Item 1");
@@ -446,6 +447,12 @@
     if(eventdata->row_index >= 0) {
         ui_list_update_row(eventdata->list, eventdata->row_index);
     }
+    UiListSelection sel = ui_list_getselection(eventdata->list);
+    printf("sel: %d: %d\n", sel.count, sel.count > 0 ? sel.rows[0] : -1);
+    
+    if(eventdata->sublist_index == 1 && eventdata->row_index == 2) {
+        ui_list_setselection(eventdata->list, 0);
+    }
 }
 
 void action_table_activate(UiEvent *event, void *userdata) {
--- a/ui/gtk/list.c	Tue Nov 25 12:58:28 2025 +0100
+++ b/ui/gtk/list.c	Wed Nov 26 19:39:37 2025 +0100
@@ -2160,6 +2160,8 @@
         UiList *list = uisublist.var->value;
         list->obj = sublist_ptr;
         list->update = ui_listbox_list_update;
+        list->getselection = ui_listbox_list_getselection;
+        list->setselection = ui_listbox_list_setselection;
     }
 }
 
@@ -2223,6 +2225,8 @@
             UiList *list = var->value;
             list->obj = uilistbox;
             list->update = ui_listbox_dynamic_update;
+            list->getselection = ui_listbox_dynamic_getselection;
+            list->setselection = ui_listbox_dynamic_setselection;
             
             ui_listbox_dynamic_update(list, -1);
         }
@@ -2294,6 +2298,30 @@
     ui_listbox_update(uilistbox, 0, cxListSize(uilistbox->sublists));
 }
 
+void ui_listbox_dynamic_setselection(UiList *list, UiListSelection sel) {
+    UiListBox *uilistbox = list->obj;
+    if(sel.count > 0) {
+        GtkListBoxRow *row = gtk_list_box_get_row_at_index(uilistbox->listbox, sel.rows[0]);
+        if(row) {
+            gtk_list_box_select_row(uilistbox->listbox, row);
+        }
+    } else {
+        gtk_list_box_unselect_all(uilistbox->listbox);
+    }
+}
+
+UiListSelection ui_listbox_dynamic_getselection(UiList *list) {
+    UiListSelection sel = { 0, NULL };
+    UiListBox *uilistbox = list->obj;
+    GtkListBoxRow *row = gtk_list_box_get_selected_row(uilistbox->listbox);
+    if(row) {
+        sel.count = 1;
+        sel.rows = malloc(sizeof(int));
+        sel.rows[0] = gtk_list_box_row_get_index(row);
+    }
+    return sel;
+}
+
 void ui_listbox_update(UiListBox *listbox, int from, int to) {
     CxIterator i = cxListIterator(listbox->sublists);
     size_t pos = 0;
@@ -2659,6 +2687,40 @@
     ui_sourcelist_update_finished();
 }
 
+void ui_listbox_list_setselection(UiList *list, UiListSelection sel) {
+    UiListBoxSubList *sublist = list->obj;
+    UiListBox *uilistbox = sublist->listbox;
+    if(sel.count > 0) {
+        int index = sel.rows[0];
+        if(index < sublist->numitems) {
+            int global_index = sublist->startpos + index;
+            GtkListBoxRow *row = gtk_list_box_get_row_at_index(uilistbox->listbox, global_index);
+            if(row) {
+                gtk_list_box_select_row(uilistbox->listbox, row);
+            }
+        }
+    } else {
+        gtk_list_box_unselect_all(uilistbox->listbox);
+    }
+}
+
+UiListSelection ui_listbox_list_getselection(UiList *list) {
+    UiListSelection sel = { 0, NULL };
+    UiListBoxSubList *sublist = list->obj;
+    UiListBox *uilistbox = sublist->listbox;
+    GtkListBoxRow *row = gtk_list_box_get_selected_row(uilistbox->listbox);
+    if(row) {
+        int index = gtk_list_box_row_get_index(row);
+        size_t startpos = sublist->startpos;
+        if(index >= startpos && index < startpos+sublist->numitems) {
+            sel.count = 1;
+            sel.rows = malloc(sizeof(int));
+            sel.rows[0] = index - startpos;
+        }
+    }
+    return sel;
+}
+
 void ui_listbox_row_activate(GtkListBox *self, GtkListBoxRow *row, gpointer user_data) {
     UiEventDataExt *data = g_object_get_data(G_OBJECT(row), "ui-listbox-row-eventdata");
     if(!data) {
--- a/ui/gtk/list.h	Tue Nov 25 12:58:28 2025 +0100
+++ b/ui/gtk/list.h	Wed Nov 26 19:39:37 2025 +0100
@@ -190,9 +190,14 @@
 void ui_combobox_setselection(UiList *list, UiListSelection selection);
 
 void ui_listbox_dynamic_update(UiList *list, int i);
+void ui_listbox_dynamic_setselection(UiList *list, UiListSelection sel);
+UiListSelection ui_listbox_dynamic_getselection(UiList *list);
+
 void ui_listbox_update(UiListBox *listbox, int from, int to);
 void ui_listbox_update_sublist(UiListBox *listbox, UiListBoxSubList *sublist, size_t listbox_insert_index);
 void ui_listbox_list_update(UiList *list, int i);
+void ui_listbox_list_setselection(UiList *list, UiListSelection sel);
+UiListSelection ui_listbox_list_getselection(UiList *list);
 
 void ui_listbox_row_activate(GtkListBox *self, GtkListBoxRow *row, gpointer user_data);
         

mercurial