[go: up one dir, main page]

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