gtk4/
entry_buffer.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{translate::*, GString};
4use libc::{c_int, c_uint};
5
6use crate::{ffi, prelude::*, EntryBuffer};
7
8impl EntryBuffer {
9    /// Create a new [`EntryBuffer`][crate::EntryBuffer] object.
10    ///
11    /// Optionally, specify initial text to set in the buffer.
12    /// ## `initial_chars`
13    /// initial buffer text
14    /// ## `n_initial_chars`
15    /// number of characters in @initial_chars, or -1
16    ///
17    /// # Returns
18    ///
19    /// A new [`EntryBuffer`][crate::EntryBuffer] object.
20    #[doc(alias = "gtk_entry_buffer_new")]
21    pub fn new(initial_chars: impl IntoOptionalGStr) -> Self {
22        assert_initialized_main_thread!();
23        unsafe {
24            initial_chars.run_with_gstr(|initial_chars| {
25                from_glib_full(ffi::gtk_entry_buffer_new(
26                    initial_chars.to_glib_none().0,
27                    -1,
28                ))
29            })
30        }
31    }
32}
33
34macro_rules! to_u16 {
35    ($e:expr) => (
36        {
37            let x = $e;
38            assert!(x as usize <= u16::MAX as usize,
39                "Unexpected value exceeding `u16` range");
40            x as u16
41        }
42    )
43}
44
45// rustdoc-stripper-ignore-next
46/// Trait containing manually implemented methods of
47/// [`EntryBuffer`](crate::EntryBuffer).
48pub trait EntryBufferExtManual: IsA<EntryBuffer> + 'static {
49    #[doc(alias = "gtk_entry_buffer_delete_text")]
50    fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
51        unsafe {
52            to_u16!(ffi::gtk_entry_buffer_delete_text(
53                self.as_ref().to_glib_none().0,
54                position as c_uint,
55                n_chars.map(|n| n as c_int).unwrap_or(-1)
56            ))
57        }
58    }
59
60    #[doc(alias = "gtk_entry_buffer_get_bytes")]
61    #[doc(alias = "get_bytes")]
62    fn bytes(&self) -> usize {
63        unsafe { ffi::gtk_entry_buffer_get_bytes(self.as_ref().to_glib_none().0) as _ }
64    }
65
66    #[doc(alias = "gtk_entry_buffer_get_length")]
67    #[doc(alias = "get_length")]
68    fn length(&self) -> u16 {
69        unsafe {
70            to_u16!(ffi::gtk_entry_buffer_get_length(
71                self.as_ref().to_glib_none().0
72            ))
73        }
74    }
75
76    #[doc(alias = "gtk_entry_buffer_get_max_length")]
77    #[doc(alias = "get_max_length")]
78    fn max_length(&self) -> Option<u16> {
79        unsafe {
80            match ffi::gtk_entry_buffer_get_max_length(self.as_ref().to_glib_none().0) {
81                0 => None,
82                x => Some(to_u16!(x)),
83            }
84        }
85    }
86
87    #[doc(alias = "gtk_entry_buffer_get_text")]
88    #[doc(alias = "get_text")]
89    fn text(&self) -> GString {
90        unsafe {
91            from_glib_none(ffi::gtk_entry_buffer_get_text(
92                self.as_ref().to_glib_none().0,
93            ))
94        }
95    }
96
97    #[doc(alias = "gtk_entry_buffer_insert_text")]
98    fn insert_text(&self, position: u16, chars: impl IntoGStr) -> u16 {
99        unsafe {
100            chars.run_with_gstr(|chars| {
101                to_u16!(ffi::gtk_entry_buffer_insert_text(
102                    self.as_ref().to_glib_none().0,
103                    position as c_uint,
104                    chars.as_ptr(),
105                    -1
106                ))
107            })
108        }
109    }
110
111    #[doc(alias = "gtk_entry_buffer_set_max_length")]
112    fn set_max_length(&self, max_length: Option<u16>) {
113        unsafe {
114            assert_ne!(max_length, Some(0), "Zero maximum length not supported");
115            ffi::gtk_entry_buffer_set_max_length(
116                self.as_ref().to_glib_none().0,
117                max_length.unwrap_or(0) as c_int,
118            );
119        }
120    }
121
122    #[doc(alias = "gtk_entry_buffer_set_text")]
123    fn set_text(&self, chars: impl IntoGStr) {
124        unsafe {
125            chars.run_with_gstr(|chars| {
126                ffi::gtk_entry_buffer_set_text(self.as_ref().to_glib_none().0, chars.as_ptr(), -1);
127            })
128        }
129    }
130}
131
132impl<O: IsA<EntryBuffer>> EntryBufferExtManual for O {}
133
134impl Default for EntryBuffer {
135    fn default() -> Self {
136        glib::Object::new()
137    }
138}
139
140impl std::fmt::Write for EntryBuffer {
141    fn write_str(&mut self, s: &str) -> std::fmt::Result {
142        let pos = self.length();
143        self.insert_text(pos, s);
144        Ok(())
145    }
146}