From 3c707ffabe469b530b0dbfaa197a532ec897d4c5 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Mon, 27 Apr 2026 21:53:18 +0200 Subject: [PATCH] add missing UiText methods --- ui-rs/src/ui/toolkit.rs | 79 +++++++++++++++++++++++++++++++++++++++++ ui/common/types.c | 74 ++++++++++++++++++++++++++++++++++++++ ui/ui/toolkit.h | 11 +++++- 3 files changed, 163 insertions(+), 1 deletion(-) diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index 9176d46..88917d1 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -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); } diff --git a/ui/common/types.c b/ui/common/types.c index aeb8586..df03542 100644 --- a/ui/common/types.c +++ b/ui/common/types.c @@ -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); + } +} diff --git a/ui/ui/toolkit.h b/ui/ui/toolkit.h index d76f7a5..995c8c2 100644 --- a/ui/ui/toolkit.h +++ b/ui/ui/toolkit.h @@ -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)); -- 2.47.3