1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::EntryBuffer;
use glib::object::IsA;
use glib::translate::*;
use libc::{c_int, c_uint};

impl EntryBuffer {
    /// Create a new [`EntryBuffer`][crate::EntryBuffer] object.
    ///
    /// Optionally, specify initial text to set in the buffer.
    /// ## `initial_chars`
    /// initial buffer text
    /// ## `n_initial_chars`
    /// number of characters in @initial_chars, or -1
    ///
    /// # Returns
    ///
    /// A new [`EntryBuffer`][crate::EntryBuffer] object.
    #[doc(alias = "gtk_entry_buffer_new")]
    pub fn new(initial_chars: Option<&str>) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_entry_buffer_new(
                initial_chars.to_glib_none().0,
                -1,
            ))
        }
    }
}

// rustdoc-stripper-ignore-next
/// Trait containing manually implemented methods of [`EntryBuffer`](crate::EntryBuffer).
pub trait EntryBufferExtManual: 'static {
    /// Deletes a sequence of characters from the buffer.
    ///
    /// @n_chars characters are deleted starting at @position.
    /// If @n_chars is negative, then all characters until the
    /// end of the text are deleted.
    ///
    /// If @position or @n_chars are out of bounds, then they
    /// are coerced to sane values.
    ///
    /// Note that the positions are specified in characters,
    /// not bytes.
    /// ## `position`
    /// position at which to delete text
    /// ## `n_chars`
    /// number of characters to delete
    ///
    /// # Returns
    ///
    /// The number of characters deleted.
    #[doc(alias = "gtk_entry_buffer_delete_text")]
    fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16;

    /// Retrieves the length in bytes of the buffer.
    ///
    /// See [`EntryBufferExtManual::length()`][crate::prelude::EntryBufferExtManual::length()].
    ///
    /// # Returns
    ///
    /// The byte length of the buffer.
    #[doc(alias = "gtk_entry_buffer_get_bytes")]
    #[doc(alias = "get_bytes")]
    fn bytes(&self) -> usize;

    /// Retrieves the length in characters of the buffer.
    ///
    /// # Returns
    ///
    /// The number of characters in the buffer.
    #[doc(alias = "gtk_entry_buffer_get_length")]
    #[doc(alias = "get_length")]
    fn length(&self) -> u16;

    /// Retrieves the maximum allowed length of the text in @self.
    ///
    /// # Returns
    ///
    /// the maximum allowed number of characters
    ///   in [`EntryBuffer`][crate::EntryBuffer], or 0 if there is no maximum.
    #[doc(alias = "gtk_entry_buffer_get_max_length")]
    #[doc(alias = "get_max_length")]
    fn max_length(&self) -> Option<u16>;

    /// Retrieves the contents of the buffer.
    ///
    /// The memory pointer returned by this call will not change
    /// unless this object emits a signal, or is finalized.
    ///
    /// # Returns
    ///
    /// a pointer to the contents of the widget as a
    ///   string. This string points to internally allocated storage
    ///   in the buffer and must not be freed, modified or stored.
    #[doc(alias = "gtk_entry_buffer_get_text")]
    #[doc(alias = "get_text")]
    fn text(&self) -> String;

    /// Inserts @n_chars characters of @chars into the contents of the
    /// buffer, at position @position.
    ///
    /// If @n_chars is negative, then characters from chars will be inserted
    /// until a null-terminator is found. If @position or @n_chars are out of
    /// bounds, or the maximum buffer text length is exceeded, then they are
    /// coerced to sane values.
    ///
    /// Note that the position and length are in characters, not in bytes.
    /// ## `position`
    /// the position at which to insert text.
    /// ## `chars`
    /// the text to insert into the buffer.
    /// ## `n_chars`
    /// the length of the text in characters, or -1
    ///
    /// # Returns
    ///
    /// The number of characters actually inserted.
    #[doc(alias = "gtk_entry_buffer_insert_text")]
    fn insert_text(&self, position: u16, chars: &str) -> u16;

    /// Sets the maximum allowed length of the contents of the buffer.
    ///
    /// If the current contents are longer than the given length, then
    /// they will be truncated to fit.
    /// ## `max_length`
    /// the maximum length of the entry buffer, or 0 for no maximum.
    ///   (other than the maximum length of entries.) The value passed in will
    ///   be clamped to the range 0-65536.
    #[doc(alias = "gtk_entry_buffer_set_max_length")]
    fn set_max_length(&self, max_length: Option<u16>);

    /// Sets the text in the buffer.
    ///
    /// This is roughly equivalent to calling
    /// [`EntryBufferExtManual::delete_text()`][crate::prelude::EntryBufferExtManual::delete_text()] and
    /// [`EntryBufferExtManual::insert_text()`][crate::prelude::EntryBufferExtManual::insert_text()].
    ///
    /// Note that @n_chars is in characters, not in bytes.
    /// ## `chars`
    /// the new text
    /// ## `n_chars`
    /// the number of characters in @text, or -1
    #[doc(alias = "gtk_entry_buffer_set_text")]
    fn set_text(&self, chars: &str);
}

macro_rules! to_u16 {
    ($e:expr) => (
        {
            let x = $e;
            assert!(x as usize <= u16::max_value() as usize,
                "Unexpected value exceeding `u16` range");
            x as u16
        }
    )
}

impl<O: IsA<EntryBuffer>> EntryBufferExtManual for O {
    fn delete_text(&self, position: u16, n_chars: Option<u16>) -> u16 {
        unsafe {
            to_u16!(ffi::gtk_entry_buffer_delete_text(
                self.as_ref().to_glib_none().0,
                position as c_uint,
                n_chars.map(|n| n as c_int).unwrap_or(-1)
            ))
        }
    }

    fn bytes(&self) -> usize {
        unsafe { ffi::gtk_entry_buffer_get_bytes(self.as_ref().to_glib_none().0) as _ }
    }

    fn length(&self) -> u16 {
        unsafe {
            to_u16!(ffi::gtk_entry_buffer_get_length(
                self.as_ref().to_glib_none().0
            ))
        }
    }

    fn max_length(&self) -> Option<u16> {
        unsafe {
            match ffi::gtk_entry_buffer_get_max_length(self.as_ref().to_glib_none().0) {
                0 => None,
                x => Some(to_u16!(x)),
            }
        }
    }

    fn text(&self) -> String {
        unsafe {
            from_glib_none(ffi::gtk_entry_buffer_get_text(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn insert_text(&self, position: u16, chars: &str) -> u16 {
        unsafe {
            to_u16!(ffi::gtk_entry_buffer_insert_text(
                self.as_ref().to_glib_none().0,
                position as c_uint,
                chars.to_glib_none().0,
                -1
            ))
        }
    }

    fn set_max_length(&self, max_length: Option<u16>) {
        unsafe {
            assert_ne!(max_length, Some(0), "Zero maximum length not supported");
            ffi::gtk_entry_buffer_set_max_length(
                self.as_ref().to_glib_none().0,
                max_length.unwrap_or(0) as c_int,
            );
        }
    }

    fn set_text(&self, chars: &str) {
        unsafe {
            ffi::gtk_entry_buffer_set_text(
                self.as_ref().to_glib_none().0,
                chars.to_glib_none().0,
                -1,
            );
        }
    }
}

impl Default for EntryBuffer {
    fn default() -> Self {
        glib::Object::new(&[])
    }
}