]> uap-core.de Git - note.git/commitdiff
add missing UiText methods
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 27 Apr 2026 19:53:18 +0000 (21:53 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 27 Apr 2026 19:53:18 +0000 (21:53 +0200)
ui-rs/src/ui/toolkit.rs
ui/common/types.c
ui/ui/toolkit.h

index 9176d46796b1521695bf3038133e7c6ebb3b1eed..88917d1897d5d645d324500d612b526019eae0dc 100644 (file)
@@ -295,6 +295,74 @@ impl UiText {
             ui_text_set(self.ptr, cstr.as_ptr());
         }
     }
+
+    pub fn substring(&self, start: usize, end: usize) -> String {
+        unsafe {
+            let cstr = ui_text_getsubstr(self.ptr, start as c_int, end as c_int);
+            CStr::from_ptr(cstr)
+                .to_string_lossy()
+                .into_owned()
+        }
+    }
+
+    pub fn insert(&mut self, index: usize, value: &str) {
+        unsafe {
+            let cstr = CString::new(value).unwrap();
+            ui_text_insert(self.ptr, index as c_int, cstr.as_ptr());
+        }
+    }
+
+    pub fn replace(&mut self, begin: usize, end: usize, value: &str) {
+        unsafe {
+            let cstr = CString::new(value).unwrap();
+            ui_text_replace(self.ptr, begin as c_int, end as c_int, cstr.as_ptr());
+        }
+    }
+
+    pub fn position(&self) -> usize {
+        unsafe {
+            ui_text_position(self.ptr) as usize
+        }
+    }
+
+    pub fn set_position(&mut self, position: usize) {
+        unsafe {
+            ui_text_setposition(self.ptr, position as c_int);
+        }
+    }
+
+    pub fn show_position(&mut self, pos: usize) {
+        unsafe {
+            ui_text_showposition(self.ptr, pos as c_int);
+        }
+    }
+
+    pub fn selection(&self) -> (usize, usize) {
+        unsafe {
+            let mut begin = 0;
+            let mut end = 0;
+            ui_text_selection(self.ptr, &mut begin, &mut end);
+            (begin as usize, end as usize)
+        }
+    }
+
+    pub fn set_selection(&mut self, begin: usize, end: usize) {
+        unsafe {
+            ui_text_setselection(self.ptr, begin as c_int, end as c_int);
+        }
+    }
+
+    pub fn length(&self) -> usize {
+        unsafe {
+            ui_text_length(self.ptr) as usize
+        }
+    }
+
+    pub fn remove(&mut self, begin: usize, end: usize) {
+        unsafe {
+            ui_text_remove(self.ptr, begin as c_int, end as c_int);
+        }
+    }
 }
 
 impl UiString {
@@ -642,4 +710,15 @@ extern "C" {
     fn ui_list_getselection(list: *const ffi::UiList) -> c_int;
     fn ui_list_setselection(list: *mut ffi::UiList, selection: c_int);
     fn ui_list_setselection2(list: *mut ffi::UiList, selection: c_int, event: c_int);
+
+    fn ui_text_getsubstr(text: *const ffi::UiText, begin: c_int, end: c_int) -> *const c_char;
+    fn ui_text_insert(text: *mut ffi::UiText, pos: c_int, str: *const c_char);
+    fn ui_text_replace(text: *mut ffi::UiText, begin: c_int, end: c_int, str: *const c_char);
+    fn ui_text_setposition(text: *mut ffi::UiText, pos: c_int);
+    fn ui_text_position(text: *const ffi::UiText) -> c_int;
+    fn ui_text_showposition(text: *mut ffi::UiText, pos: c_int);
+    fn ui_text_setselection(text: *mut ffi::UiText, begin: c_int, end: c_int);
+    fn ui_text_selection(text: *const ffi::UiText, begin: *mut c_int, end: *mut c_int);
+    fn ui_text_length(text: *const ffi::UiText) -> c_int;
+    fn ui_text_remove(text: *mut ffi::UiText, begin: c_int, end: c_int);
 }
index aeb8586c7309035064297976ab0fc70198f811a8..df03542be4ceda56d78e983142b50671ec43206d 100644 (file)
@@ -973,3 +973,77 @@ void ui_list_class_set_data(UiList *list, void *data) {
 void ui_list_class_set_iter(UiList *list, void *iter) {
     list->iter = iter;
 }
+
+/* ---------------- Text functions ---------------- */
+
+char* ui_text_getsubstr(UiText *text, int begin, int end) {
+    if(text->getsubstr) {
+        text->getsubstr(text, begin, end);
+    } else {
+        return NULL;
+    }
+}
+
+void  ui_text_insert(UiText *text, int pos, const char *str) {
+    if(text->insert) {
+        text->insert(text, pos, str);
+    }
+}
+
+void  ui_text_replace(UiText *text, int begin, int end, const char *str) {
+    if(text->replace) {
+        text->replace(text, begin, end, str);
+    }
+}
+
+void  ui_text_setposition(UiText *text, int pos) {
+    if(text->setposition) {
+        text->setposition(text, pos);
+    }
+}
+
+int   ui_text_position(UiText *text) {
+    if(text->position) {
+        return text->position(text);
+    } else {
+        return 0;
+    }
+}
+
+void  ui_text_showposition(UiText *text, int pos) {
+    if(text->showposition) {
+        text->showposition(text, pos);
+    }
+}
+
+void  ui_text_setselection(UiText *text, int begin, int end) {
+    if(text->setselection) {
+        text->setselection(text, begin, end);
+    }
+}
+
+void  ui_text_selection(UiText *text, int *begin, int *end) {
+    if(text->selection) {
+        text->selection(text, begin, end);
+    } else {
+        if(begin) {
+            *begin = 0;
+        }
+        if(end) {
+            *end = 0;
+        }
+    }
+}
+
+int   ui_text_length(UiText *text) {
+    if(text->length) {
+        return text->length(text);
+    } else {
+        return 0;
+    }
+}
+void  ui_text_remove(UiText *text, int begin, int end) {
+    if(text->remove) {
+        text->remove(text, begin, end);
+    }
+}
index d76f7a55b1af7a0f6f120153a28fbb4f795fd93f..995c8c29c4f61e748660433f41f5cec5b57421ce 100644 (file)
@@ -718,7 +718,16 @@ UIEXPORT char* ui_clipboard_get();
 
 UIEXPORT void ui_add_image(char *imgname, char *filename); // TODO: remove?
 
-
+UIEXPORT char* ui_text_getsubstr(UiText *text, int begin, int end);
+UIEXPORT void  ui_text_insert(UiText *text, int pos, const char *str);
+UIEXPORT void  ui_text_replace(UiText *text, int begin, int end, const char *str);
+UIEXPORT void  ui_text_setposition(UiText *text, int pos);
+UIEXPORT int   ui_text_position(UiText *text);
+UIEXPORT void  ui_text_showposition(UiText *text, int pos);
+UIEXPORT void  ui_text_setselection(UiText *text, int begin, int end);
+UIEXPORT void  ui_text_selection(UiText *text, int *begin, int *end);
+UIEXPORT int   ui_text_length(UiText *text);
+UIEXPORT void  ui_text_remove(UiText *text, int begin, int end);
 
 UIEXPORT UiStr ui_str(char *cstr);
 UIEXPORT UiStr ui_str_free(char *str, void (*free)(void *v));