11//! User input mechanisms: numbers, colors, and text in various forms.
2+ //!
3+ //! All text buffers accept and return `\n` line endings; if on Windows, the appropriate
4+ //! `\r\n` for display are added and removed by the controls.
25
36use super :: Control ;
47use std:: ffi:: { CStr , CString } ;
58use std:: i32;
69use std:: mem;
710use std:: os:: raw:: c_void;
8- use ui:: UI ;
911use ui_sys:: {
1012 self , uiCheckbox, uiCombobox, uiControl, uiEntry, uiMultilineEntry, uiRadioButtons, uiSlider,
1113 uiSpinbox,
1214} ;
15+ use ui:: UI ;
16+ use str_tools:: { from_toolkit_string, to_toolkit_string} ;
1317
1418pub trait NumericEntry {
1519 fn value ( & self , ctx : & UI ) -> i32 ;
@@ -150,14 +154,11 @@ impl MultilineEntry {
150154
151155impl TextEntry for Entry {
152156 fn value ( & self , _ctx : & UI ) -> String {
153- unsafe {
154- CStr :: from_ptr ( ui_sys:: uiEntryText ( self . uiEntry ) )
155- . to_string_lossy ( )
156- . into_owned ( )
157- }
157+ unsafe { from_toolkit_string ( ui_sys:: uiEntryText ( self . uiEntry ) ) }
158158 }
159+
159160 fn set_value ( & mut self , _ctx : & UI , value : & str ) {
160- let cstring = CString :: new ( value. as_bytes ( ) . to_vec ( ) ) . unwrap ( ) ;
161+ let cstring = to_toolkit_string ( value) ;
161162 unsafe { ui_sys:: uiEntrySetText ( self . uiEntry , cstring. as_ptr ( ) ) }
162163 }
163164
@@ -210,9 +211,7 @@ impl TextEntry for PasswordEntry {
210211
211212 extern "C" fn c_callback ( entry : * mut uiEntry , data : * mut c_void ) {
212213 unsafe {
213- let string = CStr :: from_ptr ( ui_sys:: uiEntryText ( entry) )
214- . to_string_lossy ( )
215- . into_owned ( ) ;
214+ let string = from_toolkit_string ( ui_sys:: uiEntryText ( entry) ) ;
216215 mem:: transmute :: < * mut c_void , & mut Box < dyn FnMut ( String ) > > ( data) ( string) ;
217216 mem:: forget ( entry) ;
218217 }
@@ -223,13 +222,12 @@ impl TextEntry for PasswordEntry {
223222impl TextEntry for MultilineEntry {
224223 fn value ( & self , _ctx : & UI ) -> String {
225224 unsafe {
226- CStr :: from_ptr ( ui_sys:: uiMultilineEntryText ( self . uiMultilineEntry ) )
227- . to_string_lossy ( )
228- . into_owned ( )
225+ from_toolkit_string ( ui_sys:: uiMultilineEntryText ( self . uiMultilineEntry ) )
229226 }
230227 }
228+
231229 fn set_value ( & mut self , _ctx : & UI , value : & str ) {
232- let cstring = CString :: new ( value. as_bytes ( ) . to_vec ( ) ) . unwrap ( ) ;
230+ let cstring = to_toolkit_string ( value) ;
233231 unsafe { ui_sys:: uiMultilineEntrySetText ( self . uiMultilineEntry , cstring. as_ptr ( ) ) }
234232 }
235233
@@ -246,9 +244,7 @@ impl TextEntry for MultilineEntry {
246244
247245 extern "C" fn c_callback ( entry : * mut uiMultilineEntry , data : * mut c_void ) {
248246 unsafe {
249- let string = CStr :: from_ptr ( ui_sys:: uiMultilineEntryText ( entry) )
250- . to_string_lossy ( )
251- . into_owned ( ) ;
247+ let string = from_toolkit_string ( ui_sys:: uiMultilineEntryText ( entry) ) ;
252248 mem:: transmute :: < * mut c_void , & mut Box < dyn FnMut ( String ) > > ( data) ( string) ;
253249 mem:: forget ( entry) ;
254250 }
@@ -271,7 +267,7 @@ impl Combobox {
271267 /// Adds a new option to the combination box.
272268 pub fn append ( & self , _ctx : & UI , name : & str ) {
273269 unsafe {
274- let c_string = CString :: new ( name. as_bytes ( ) . to_vec ( ) ) . unwrap ( ) ;
270+ let c_string = to_toolkit_string ( name) ;
275271 ui_sys:: uiComboboxAppend ( self . uiCombobox , c_string. as_ptr ( ) )
276272 }
277273 }
@@ -314,7 +310,7 @@ define_control! {
314310impl Checkbox {
315311 // Create a new Checkbox which can produce values from `min` to `max`.
316312 pub fn new ( _ctx : & UI , text : & str ) -> Self {
317- let c_string = CString :: new ( text. as_bytes ( ) . to_vec ( ) ) . unwrap ( ) ;
313+ let c_string = to_toolkit_string ( text) ;
318314 unsafe { Checkbox :: from_raw ( ui_sys:: uiNewCheckbox ( c_string. as_ptr ( ) ) ) }
319315 }
320316
0 commit comments