save list selections when unbinding

Thu, 04 Jun 2026 19:59:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 04 Jun 2026 19:59:58 +0200
changeset 1168
2f9d8af6a499
parent 1167
f0e901f7d1b7
child 1169
c69f2941d536

save list selections when unbinding

ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/common/types.h file | annotate | diff | comparison | revisions
ui/common/wrapper.c file | annotate | diff | comparison | revisions
ui/common/wrapper.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/context.c	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/common/context.c	Thu Jun 04 19:59:58 2026 +0200
@@ -466,6 +466,11 @@
             UiList *t = to;
             uic_list_copy(f, t);
             ui_list_update(t);
+            if(t->setselection && t->saved_selection) {
+                t->setselection(t, *t->saved_selection);
+            }
+            ui_list_selection_free(t->saved_selection);
+            t->saved_selection = NULL;
             break;
         }
         case UI_VAR_RANGE: {
--- a/ui/common/types.c	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/common/types.c	Thu Jun 04 19:59:58 2026 +0200
@@ -121,6 +121,7 @@
     UiList *list = ui_malloc(ctx, sizeof(UiList));
     memset(list, 0, sizeof(UiList));
     listinit(ctx, list, userdata);
+    list->save_selection = TRUE;
     
     if(name && ctx) {
         uic_reg_var(ctx, name, UI_VAR_LIST, list);
@@ -137,6 +138,7 @@
     } else {
         default_list_destroy(ctx, list, default_list_destroy_userdata);
     }
+    ui_list_selection_free(list->saved_selection);
     ui_free(ctx, list);
 }
 
@@ -219,6 +221,11 @@
     ui_notify(list->observers, list);
 }
 
+void ui_list_clear_saved_selection(UiList *list) {
+    ui_list_selection_free(list->saved_selection);
+    list->saved_selection = NULL;
+}
+
 
 typedef struct {
     int  type;
@@ -806,6 +813,15 @@
 }
 
 void uic_list_unbind(UiList *l) {
+    // save selection
+    ui_list_selection_free(l->saved_selection);
+    if(l->getselection && l->save_selection) {
+        l->saved_selection = ui_list_get_selection_allocated(l);
+    } else {
+        l->saved_selection = NULL;
+    }
+    
+    // unbind
     l->update = NULL;
     l->getselection = NULL;
     l->setselection = NULL;
@@ -820,6 +836,23 @@
 }
 
 
+
+UiListSelection* ui_list_get_selection_allocated(UiList *list) {
+    UiListSelection *sel = malloc(sizeof(UiListSelection));
+    *sel = ui_list_get_selection(list);
+    return sel;
+    
+}
+
+void ui_list_selection_free(UiListSelection *sel) {
+    if(!sel) {
+        return;
+    }
+    ui_listselection_free(*sel);
+    free(sel);
+}
+
+
 UIEXPORT int ui_list_getselection(UiList *list) {
     int selection = -1;
     if (list->getselection) {
--- a/ui/common/types.h	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/common/types.h	Thu Jun 04 19:59:58 2026 +0200
@@ -61,6 +61,9 @@
 void uic_list_unbind(UiList *l);
 void uic_generic_unbind(UiGeneric *g);
 
+UIEXPORT UiListSelection* ui_list_get_selection_allocated(UiList *list);
+UIEXPORT void ui_list_selection_free(UiListSelection *sel);
+
 void uic_list_register_observer_destructor(UiContext *ctx, UiList *list, UiObserver *observer);
   
 #ifdef	__cplusplus
--- a/ui/common/wrapper.c	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/common/wrapper.c	Thu Jun 04 19:59:58 2026 +0200
@@ -98,6 +98,13 @@
     list->iter = iter;
 }
 
+void ui_list_set_save_selection(UiList *list, UiBool value) {
+    list->save_selection = value;
+}
+
+UiBool ui_list_get_save_selection(UiList *list) {
+    return list->save_selection;
+}
 
 /* ------------------------------ UiSublist ------------------------------ */
 
@@ -275,13 +282,6 @@
 
 /* ---------------------------- UiListSelection ---------------------------- */
 
-UiListSelection* ui_list_get_selection_allocated(UiList *list) {
-    UiListSelection *sel = malloc(sizeof(UiListSelection));
-    *sel = ui_list_get_selection(list);
-    return sel;
-    
-}
-
 int ui_list_selection_get_count(UiListSelection *sel) {
     return sel->count;
 }
@@ -299,10 +299,6 @@
     }
 }
 
-void ui_list_selection_free(UiListSelection *sel) {
-    ui_listselection_free(*sel);
-    free(sel);
-}
 
 /* -------------------------- UiTextChangedEvent -------------------------- */
 
--- a/ui/common/wrapper.h	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/common/wrapper.h	Thu Jun 04 19:59:58 2026 +0200
@@ -49,6 +49,8 @@
 UIEXPORT void* ui_list_get_data(UiList *list);
 UIEXPORT void* ui_list_get_iter(UiList *list);
 UIEXPORT void ui_list_set_iter(UiList *list, void *iter);
+UIEXPORT void ui_list_set_save_selection(UiList *list, UiBool value);
+UIEXPORT UiBool ui_list_get_save_selection(UiList *list);
 
 UIEXPORT UiSubList* ui_sublist_new(void);
 UIEXPORT void ui_sublist_set_value(UiSubList *sublist, UiList *value);
@@ -82,11 +84,9 @@
 UIEXPORT int ui_event_get_int(UiEvent *event);
 UIEXPORT int ui_event_get_set(UiEvent *event);
 
-UIEXPORT UiListSelection* ui_list_get_selection_allocated(UiList *list);
 UIEXPORT int ui_list_selection_get_count(UiListSelection *sel);
 UIEXPORT int* ui_list_selection_get_rows(UiListSelection *sel);
 UIEXPORT void ui_list_set_selected_indices(UiList *list, int *indices, int num);
-UIEXPORT void ui_list_selection_free(UiListSelection *sel);
 
 UIEXPORT int ui_text_change_event_get_type(UiTextChangeEventData *event);
 UIEXPORT int ui_text_change_event_get_begin(UiTextChangeEventData *event);
--- a/ui/ui/toolkit.h	Thu Jun 04 19:22:54 2026 +0200
+++ b/ui/ui/toolkit.h	Thu Jun 04 19:59:58 2026 +0200
@@ -419,29 +419,33 @@
  * abstract list
  */
 struct UiList {
-    /* destructor */
+    // destructor
     ui_list_destroy_func destroy;
-    /* get the first element */
+    // get the first element
     void*(*first)(UiList *list);
-    /* get the next element */
+    // get the next element
     void*(*next)(UiList *list);
-    /* get the nth element */
+    // get the nth element
     void*(*get)(UiList *list, int i);
-    /* get the number of elements */
+    // get the number of elements 
     int(*count)(UiList *list);
-    /* iterator changes after first() next() and get() */
+    // iterator changes after first() next() and get() 
     void *iter;
-    /* private - implementation dependent */
+    // private - implementation dependent
     void *data;
     
-    /* binding functions */
+    // binding functions 
     void (*update)(UiList *list, int i);
     UiListSelection (*getselection)(UiList *list);
     void (*setselection)(UiList *list, UiListSelection selection);
-    /* binding object */
+    // binding object
     void *obj;
+    // saved selection after unbind
+    UiListSelection *saved_selection;
+    // enable selection saving
+    UiBool save_selection;
     
-    /* list of observers */
+    // list of observers
     UiObserver *observers;
 };
 
@@ -695,6 +699,7 @@
 UIEXPORT void ui_list_set_selection(UiList *list, UiListSelection sel);
 UIEXPORT void ui_list_addobsv(UiList *list, ui_callback f, void *data);
 UIEXPORT void ui_list_notify(UiList *list);
+UIEXPORT void ui_list_clear_saved_selection(UiList *list);
 
 UIEXPORT int ui_list_getselection(UiList *list);
 UIEXPORT void ui_list_setselection(UiList *list, int index);

mercurial