ui/gtk/list.c

changeset 937
06e03c7e39db
parent 890
8d30cbd1c465
child 939
fdc6a46100a3
--- 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) {

mercurial