]> uap-core.de Git - note.git/commitdiff
implement ListSelection event data type main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 7 May 2026 20:16:52 +0000 (22:16 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 7 May 2026 20:16:52 +0000 (22:16 +0200)
ui-rs/src/ui/event.rs
ui-rs/src/ui/toolkit.rs

index 0a1ff3c2d05615d904b5a33b03800ea747df3eae..80659f8fd1d5abc56bfb0bf3a1adc757217a0cec 100644 (file)
@@ -29,7 +29,7 @@
 #![allow(dead_code)]
 
 use std::ffi::{c_char, c_void, CStr, CString};
-use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange};
+use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange, ListSelection};
 use crate::ui::ffi::{UiEvent};
 
 pub struct Event<'a, T> {
@@ -54,7 +54,7 @@ pub enum EventType<'a> {
     TextValue(&'a mut UiText),
     DoubleValue(&'a mut UiDouble),
     RangeValue(&'a mut UiRange),
-    ListSelection,
+    ListSelection(&'a ListSelection),
     ListElement,
     Dnd,
     SubList,
@@ -70,7 +70,7 @@ enum EventTypeData {
     TextValue(UiText),
     DoubleValue(UiDouble),
     RangeValue(UiRange),
-    ListSelection,
+    ListSelection(ListSelection),
     ListElement,
     Dnd,
     SubList,
@@ -101,6 +101,9 @@ fn get_event_data(e: *const ffi::UiEvent) -> EventTypeData {
             let d: *mut ffi::UiRange = ptr.cast();
             EventTypeData::RangeValue( UiRange { ptr: d } )
         }
+        8 => {
+            EventTypeData::ListSelection( ListSelection::from_ptr(ptr.cast()) )
+        }
         _ => EventTypeData::Null
     }
 }
@@ -115,7 +118,7 @@ fn get_event_type<'a>(data: &'a mut EventTypeData) -> EventType<'a> {
         EventTypeData::TextValue(t) => EventType::TextValue( t ),
         EventTypeData::DoubleValue(d) => EventType::DoubleValue( d ),
         EventTypeData::RangeValue(r) => EventType::RangeValue( r ),
-        EventTypeData::ListSelection => EventType::ListSelection,
+        EventTypeData::ListSelection(s) => EventType::ListSelection( s),
         EventTypeData::ListElement => EventType::ListElement,
         EventTypeData::Dnd => EventType::Dnd,
         EventTypeData::SubList => EventType::SubList,
index d80c76f184ec91ffeb78ea5fc1cb584002fd119f..3330b39a7bf4da8714717f40eea447fa45225b4b 100644 (file)
@@ -454,20 +454,13 @@ impl<T> UiList<T> {
     }
 
     pub fn selection(&mut self) -> Vec<i32> {
-        let mut sel = Vec::new();
         unsafe {
-            let selection = ui_list_get_selection_allocated(self.ptr);
-            let count = ui_list_selection_get_count(selection) as usize;
-            if count > 0 {
-                let indices = ui_list_selection_get_rows(selection);
-                sel.set_len(count as usize);
-                for i in 0..count {
-                    sel.push(*indices.add(i) as i32);
-                }
-            }
-            ui_list_selection_free(selection);
+            let selection = ListSelection {
+                ptr: ui_list_get_selection_allocated(self.ptr),
+                free: true
+            };
+            selection.selection()
         }
-        sel
     }
 
     pub fn set_selection(&mut self, sel: &Vec<i32>) {
@@ -499,6 +492,36 @@ impl<T> Drop for UiList<T> {
     }
 }
 
+pub struct ListSelection {
+    pub ptr: *mut ffi::UiListSelection,
+    free: bool
+}
+
+impl ListSelection {
+    pub fn from_ptr(ptr: *mut ffi::UiListSelection) -> ListSelection {
+        ListSelection { ptr: ptr, free: false }
+    }
+
+    pub fn from_allocated(ptr: *mut ffi::UiListSelection) -> ListSelection {
+        ListSelection { ptr: ptr, free: true }
+    }
+    
+    pub fn selection(&self) -> Vec<i32> {
+        let mut sel = Vec::new();
+        unsafe {
+            let count = ui_list_selection_get_count(self.ptr) as usize;
+            if count > 0 {
+                let indices = ui_list_selection_get_rows(self.ptr);
+                sel.set_len(count as usize);
+                for i in 0..count {
+                    sel.push(*indices.add(i) as i32);
+                }
+            }
+        }
+        sel
+    }
+}
+
 
 /* -------------------------------- SourceList -------------------------------- */
 
@@ -739,8 +762,8 @@ extern "C" {
     fn ui_list_update_row(list: *mut ffi::UiList, row: c_int);
     fn ui_list_get_selection_allocated(list: *mut ffi::UiList) -> *mut ffi::UiListSelection;
     fn ui_list_set_selected_indices(list: *mut ffi::UiList, indices: *const c_int, len: c_int);
-    fn ui_list_selection_get_count(list: *mut ffi::UiListSelection) -> c_int;
-    fn ui_list_selection_get_rows(list: *mut ffi::UiListSelection) -> *const c_int;
+    fn ui_list_selection_get_count(list: *const ffi::UiListSelection) -> c_int;
+    fn ui_list_selection_get_rows(list: *const ffi::UiListSelection) -> *const c_int;
     fn ui_list_selection_free(list: *mut ffi::UiListSelection);
 
     pub fn ui_list_append(list: *mut ffi::UiList, data: *mut c_void);