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