Skip to main content

gtk/
entry_buffer.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::ffi;
4use glib::translate::*;
5use libc::{c_int, c_uint};
6
7glib::wrapper! {
8    /// The [`EntryBuffer`][crate::EntryBuffer] class contains the actual text displayed in a
9    /// [`Entry`][crate::Entry] widget.
10    ///
11    /// A single [`EntryBuffer`][crate::EntryBuffer] object can be shared by multiple [`Entry`][crate::Entry]
12    /// widgets which will then share the same text content, but not the cursor
13    /// position, visibility attributes, icon etc.
14    ///
15    /// [`EntryBuffer`][crate::EntryBuffer] may be derived from. Such a derived class might allow
16    /// text to be stored in an alternate location, such as non-pageable memory,
17    /// useful in the case of important passwords. Or a derived class could
18    /// integrate with an application’s concept of undo/redo.
19    ///
20    /// ## Properties
21    ///
22    ///
23    /// #### `length`
24    ///  The length (in characters) of the text in buffer.
25    ///
26    /// Readable
27    ///
28    ///
29    /// #### `max-length`
30    ///  The maximum length (in characters) of the text in the buffer.
31    ///
32    /// Readable | Writable
33    ///
34    ///
35    /// #### `text`
36    ///  The contents of the buffer.
37    ///
38    /// Readable | Writable
39    ///
40    /// ## Signals
41    ///
42    ///
43    /// #### `deleted-text`
44    ///  This signal is emitted after text is deleted from the buffer.
45    ///
46    ///
47    ///
48    ///
49    /// #### `inserted-text`
50    ///  This signal is emitted after text is inserted into the buffer.
51    ///
52    ///
53    ///
54    /// # Implements
55    ///
56    /// [`EntryBufferExt`][trait@crate::prelude::EntryBufferExt], [`trait@glib::ObjectExt`]
57    #[doc(alias = "GtkEntryBuffer")]
58    pub struct EntryBuffer(Object<ffi::GtkEntryBuffer, ffi::GtkEntryBufferClass>);
59
60    match fn {
61        type_ => || ffi::gtk_entry_buffer_get_type(),
62    }
63}
64
65macro_rules! to_u16 {
66    ($e:expr_2021) => (
67        {
68            let x = $e;
69            assert!(x as usize <= u16::MAX as usize,
70                "Unexpected value exceeding `u16` range");
71            x as u16
72        }
73    )
74}
75
76#[allow(clippy::cast_lossless)]
77impl EntryBuffer {
78    #[doc(alias = "gtk_entry_buffer_new")]
79    pub fn new(initial_chars: Option<&str>) -> EntryBuffer {
80        assert_initialized_main_thread!();
81        unsafe {
82            from_glib_full(ffi::gtk_entry_buffer_new(
83                initial_chars.to_glib_none().0,
84                -1,
85            ))
86        }
87    }
88
89    #[doc(alias = "gtk_entry_buffer_delete_text")]
90    pub fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
91        unsafe {
92            to_u16!(ffi::gtk_entry_buffer_delete_text(
93                self.to_glib_none().0,
94                position as c_uint,
95                n_chars.map(|n| n as c_int).unwrap_or(-1)
96            ))
97        }
98    }
99
100    #[doc(alias = "gtk_entry_buffer_get_bytes")]
101    #[doc(alias = "get_bytes")]
102    pub fn bytes(&self) -> u32 {
103        unsafe { ffi::gtk_entry_buffer_get_bytes(self.to_glib_none().0) as u32 }
104    }
105
106    #[doc(alias = "gtk_entry_buffer_get_length")]
107    #[doc(alias = "get_length")]
108    pub fn length(&self) -> u16 {
109        unsafe { to_u16!(ffi::gtk_entry_buffer_get_length(self.to_glib_none().0)) }
110    }
111
112    #[doc(alias = "gtk_entry_buffer_get_max_length")]
113    #[doc(alias = "get_max_length")]
114    pub fn max_length(&self) -> Option<u16> {
115        unsafe {
116            match ffi::gtk_entry_buffer_get_max_length(self.to_glib_none().0) {
117                0 => None,
118                x => Some(to_u16!(x)),
119            }
120        }
121    }
122
123    #[doc(alias = "gtk_entry_buffer_get_text")]
124    #[doc(alias = "get_text")]
125    pub fn text(&self) -> String {
126        unsafe { from_glib_none(ffi::gtk_entry_buffer_get_text(self.to_glib_none().0)) }
127    }
128
129    #[doc(alias = "gtk_entry_buffer_insert_text")]
130    pub fn insert_text(&self, position: u16, chars: &str) -> u16 {
131        unsafe {
132            to_u16!(ffi::gtk_entry_buffer_insert_text(
133                self.to_glib_none().0,
134                position as c_uint,
135                chars.to_glib_none().0,
136                -1
137            ))
138        }
139    }
140
141    #[doc(alias = "gtk_entry_buffer_set_max_length")]
142    pub fn set_max_length(&self, max_length: Option<u16>) {
143        unsafe {
144            assert_ne!(max_length, Some(0), "Zero maximum length not supported");
145            ffi::gtk_entry_buffer_set_max_length(
146                self.to_glib_none().0,
147                max_length.unwrap_or(0) as c_int,
148            );
149        }
150    }
151
152    #[doc(alias = "gtk_entry_buffer_set_text")]
153    pub fn set_text(&self, chars: &str) {
154        unsafe {
155            ffi::gtk_entry_buffer_set_text(self.to_glib_none().0, chars.to_glib_none().0, -1);
156        }
157    }
158}