Skip to main content

gtk/auto/
editable.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::ffi;
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// The [`Editable`][crate::Editable] interface is an interface which should be implemented by
10    /// text editing widgets, such as [`Entry`][crate::Entry] and [`SpinButton`][crate::SpinButton]. It contains functions
11    /// for generically manipulating an editable widget, a large number of action
12    /// signals used for key bindings, and several signals that an application can
13    /// connect to to modify the behavior of a widget.
14    ///
15    /// As an example of the latter usage, by connecting
16    /// the following handler to [`insert-text`][struct@crate::Editable#insert-text], an application
17    /// can convert all entry into a widget into uppercase.
18    ///
19    /// ## Forcing entry to uppercase.
20    ///
21    ///
22    ///
23    /// **⚠️ The following code is in C ⚠️**
24    ///
25    /// ```C
26    /// #include <ctype.h>;
27    ///
28    /// void
29    /// insert_text_handler (GtkEditable *editable,
30    ///                      const gchar *text,
31    ///                      gint         length,
32    ///                      gint        *position,
33    ///                      gpointer     data)
34    /// {
35    ///   gchar *result = g_utf8_strup (text, length);
36    ///
37    ///   g_signal_handlers_block_by_func (editable,
38    ///                                (gpointer) insert_text_handler, data);
39    ///   gtk_editable_insert_text (editable, result, length, position);
40    ///   g_signal_handlers_unblock_by_func (editable,
41    ///                                      (gpointer) insert_text_handler, data);
42    ///
43    ///   g_signal_stop_emission_by_name (editable, "insert_text");
44    ///
45    ///   g_free (result);
46    /// }
47    /// ```
48    ///
49    /// ## Signals
50    ///
51    ///
52    /// #### `changed`
53    ///  The ::changed signal is emitted at the end of a single
54    /// user-visible operation on the contents of the [`Editable`][crate::Editable].
55    ///
56    /// E.g., a paste operation that replaces the contents of the
57    /// selection will cause only one signal emission (even though it
58    /// is implemented by first deleting the selection, then inserting
59    /// the new content, and may cause multiple ::notify::text signals
60    /// to be emitted).
61    ///
62    ///
63    ///
64    ///
65    /// #### `delete-text`
66    ///  This signal is emitted when text is deleted from
67    /// the widget by the user. The default handler for
68    /// this signal will normally be responsible for deleting
69    /// the text, so by connecting to this signal and then
70    /// stopping the signal with `g_signal_stop_emission()`, it
71    /// is possible to modify the range of deleted text, or
72    /// prevent it from being deleted entirely. The `start_pos`
73    /// and `end_pos` parameters are interpreted as for
74    /// [`EditableExt::delete_text()`][crate::prelude::EditableExt::delete_text()].
75    ///
76    ///
77    ///
78    ///
79    /// #### `insert-text`
80    ///  This signal is emitted when text is inserted into
81    /// the widget by the user. The default handler for
82    /// this signal will normally be responsible for inserting
83    /// the text, so by connecting to this signal and then
84    /// stopping the signal with `g_signal_stop_emission()`, it
85    /// is possible to modify the inserted text, or prevent
86    /// it from being inserted entirely.
87    ///
88    ///
89    ///
90    /// # Implements
91    ///
92    /// [`EditableExt`][trait@crate::prelude::EditableExt], [`EditableSignals`][trait@crate::prelude::EditableSignals]
93    #[doc(alias = "GtkEditable")]
94    pub struct Editable(Interface<ffi::GtkEditable, ffi::GtkEditableInterface>);
95
96    match fn {
97        type_ => || ffi::gtk_editable_get_type(),
98    }
99}
100
101impl Editable {
102    pub const NONE: Option<&'static Editable> = None;
103}
104
105/// Trait containing all [`struct@Editable`] methods.
106///
107/// # Implementors
108///
109/// [`Editable`][struct@crate::Editable], [`Entry`][struct@crate::Entry], [`SearchEntry`][struct@crate::SearchEntry], [`SpinButton`][struct@crate::SpinButton]
110pub trait EditableExt: IsA<Editable> + 'static {
111    /// Copies the contents of the currently selected content in the editable and
112    /// puts it on the clipboard.
113    #[doc(alias = "gtk_editable_copy_clipboard")]
114    fn copy_clipboard(&self) {
115        unsafe {
116            ffi::gtk_editable_copy_clipboard(self.as_ref().to_glib_none().0);
117        }
118    }
119
120    /// Removes the contents of the currently selected content in the editable and
121    /// puts it on the clipboard.
122    #[doc(alias = "gtk_editable_cut_clipboard")]
123    fn cut_clipboard(&self) {
124        unsafe {
125            ffi::gtk_editable_cut_clipboard(self.as_ref().to_glib_none().0);
126        }
127    }
128
129    /// Deletes the currently selected text of the editable.
130    /// This call doesn’t do anything if there is no selected text.
131    #[doc(alias = "gtk_editable_delete_selection")]
132    fn delete_selection(&self) {
133        unsafe {
134            ffi::gtk_editable_delete_selection(self.as_ref().to_glib_none().0);
135        }
136    }
137
138    /// Deletes a sequence of characters. The characters that are deleted are
139    /// those characters at positions from `start_pos` up to, but not including
140    /// `end_pos`. If `end_pos` is negative, then the characters deleted
141    /// are those from `start_pos` to the end of the text.
142    ///
143    /// Note that the positions are specified in characters, not bytes.
144    /// ## `start_pos`
145    /// start position
146    /// ## `end_pos`
147    /// end position
148    #[doc(alias = "gtk_editable_delete_text")]
149    fn delete_text(&self, start_pos: i32, end_pos: i32) {
150        unsafe {
151            ffi::gtk_editable_delete_text(self.as_ref().to_glib_none().0, start_pos, end_pos);
152        }
153    }
154
155    /// Retrieves a sequence of characters. The characters that are retrieved
156    /// are those characters at positions from `start_pos` up to, but not
157    /// including `end_pos`. If `end_pos` is negative, then the characters
158    /// retrieved are those characters from `start_pos` to the end of the text.
159    ///
160    /// Note that positions are specified in characters, not bytes.
161    /// ## `start_pos`
162    /// start of text
163    /// ## `end_pos`
164    /// end of text
165    ///
166    /// # Returns
167    ///
168    /// a pointer to the contents of the widget as a
169    ///  string. This string is allocated by the [`Editable`][crate::Editable]
170    ///  implementation and should be freed by the caller.
171    #[doc(alias = "gtk_editable_get_chars")]
172    #[doc(alias = "get_chars")]
173    fn chars(&self, start_pos: i32, end_pos: i32) -> Option<glib::GString> {
174        unsafe {
175            from_glib_full(ffi::gtk_editable_get_chars(
176                self.as_ref().to_glib_none().0,
177                start_pos,
178                end_pos,
179            ))
180        }
181    }
182
183    /// Retrieves whether `self` is editable. See
184    /// [`set_editable()`][Self::set_editable()].
185    ///
186    /// # Returns
187    ///
188    /// [`true`] if `self` is editable.
189    #[doc(alias = "gtk_editable_get_editable")]
190    #[doc(alias = "get_editable")]
191    fn is_editable(&self) -> bool {
192        unsafe {
193            from_glib(ffi::gtk_editable_get_editable(
194                self.as_ref().to_glib_none().0,
195            ))
196        }
197    }
198
199    /// Retrieves the current position of the cursor relative to the start
200    /// of the content of the editable.
201    ///
202    /// Note that this position is in characters, not in bytes.
203    ///
204    /// # Returns
205    ///
206    /// the cursor position
207    #[doc(alias = "gtk_editable_get_position")]
208    #[doc(alias = "get_position")]
209    fn position(&self) -> i32 {
210        unsafe { ffi::gtk_editable_get_position(self.as_ref().to_glib_none().0) }
211    }
212
213    /// Retrieves the selection bound of the editable. start_pos will be filled
214    /// with the start of the selection and `end_pos` with end. If no text was
215    /// selected both will be identical and [`false`] will be returned.
216    ///
217    /// Note that positions are specified in characters, not bytes.
218    ///
219    /// # Returns
220    ///
221    /// [`true`] if an area is selected, [`false`] otherwise
222    ///
223    /// ## `start_pos`
224    /// location to store the starting position, or [`None`]
225    ///
226    /// ## `end_pos`
227    /// location to store the end position, or [`None`]
228    #[doc(alias = "gtk_editable_get_selection_bounds")]
229    #[doc(alias = "get_selection_bounds")]
230    fn selection_bounds(&self) -> Option<(i32, i32)> {
231        unsafe {
232            let mut start_pos = std::mem::MaybeUninit::uninit();
233            let mut end_pos = std::mem::MaybeUninit::uninit();
234            let ret = from_glib(ffi::gtk_editable_get_selection_bounds(
235                self.as_ref().to_glib_none().0,
236                start_pos.as_mut_ptr(),
237                end_pos.as_mut_ptr(),
238            ));
239            if ret {
240                Some((start_pos.assume_init(), end_pos.assume_init()))
241            } else {
242                None
243            }
244        }
245    }
246
247    /// Inserts `new_text_length` bytes of `new_text` into the contents of the
248    /// widget, at position `position`.
249    ///
250    /// Note that the position is in characters, not in bytes.
251    /// The function updates `position` to point after the newly inserted text.
252    /// ## `new_text`
253    /// the text to append
254    /// ## `new_text_length`
255    /// the length of the text in bytes, or -1
256    ///
257    /// # Returns
258    ///
259    ///
260    /// ## `position`
261    /// location of the position text will be inserted at
262    #[doc(alias = "gtk_editable_insert_text")]
263    fn insert_text(&self, new_text: &str, position: &mut i32) {
264        let new_text_length = new_text.len() as _;
265        unsafe {
266            ffi::gtk_editable_insert_text(
267                self.as_ref().to_glib_none().0,
268                new_text.to_glib_none().0,
269                new_text_length,
270                position,
271            );
272        }
273    }
274
275    /// Pastes the content of the clipboard to the current position of the
276    /// cursor in the editable.
277    #[doc(alias = "gtk_editable_paste_clipboard")]
278    fn paste_clipboard(&self) {
279        unsafe {
280            ffi::gtk_editable_paste_clipboard(self.as_ref().to_glib_none().0);
281        }
282    }
283
284    /// Selects a region of text. The characters that are selected are
285    /// those characters at positions from `start_pos` up to, but not
286    /// including `end_pos`. If `end_pos` is negative, then the
287    /// characters selected are those characters from `start_pos` to
288    /// the end of the text.
289    ///
290    /// Note that positions are specified in characters, not bytes.
291    /// ## `start_pos`
292    /// start of region
293    /// ## `end_pos`
294    /// end of region
295    #[doc(alias = "gtk_editable_select_region")]
296    fn select_region(&self, start_pos: i32, end_pos: i32) {
297        unsafe {
298            ffi::gtk_editable_select_region(self.as_ref().to_glib_none().0, start_pos, end_pos);
299        }
300    }
301
302    /// Determines if the user can edit the text in the editable
303    /// widget or not.
304    /// ## `is_editable`
305    /// [`true`] if the user is allowed to edit the text
306    ///  in the widget
307    #[doc(alias = "gtk_editable_set_editable")]
308    fn set_editable(&self, is_editable: bool) {
309        unsafe {
310            ffi::gtk_editable_set_editable(self.as_ref().to_glib_none().0, is_editable.into_glib());
311        }
312    }
313
314    /// Sets the cursor position in the editable to the given value.
315    ///
316    /// The cursor is displayed before the character with the given (base 0)
317    /// index in the contents of the editable. The value must be less than or
318    /// equal to the number of characters in the editable. A value of -1
319    /// indicates that the position should be set after the last character
320    /// of the editable. Note that `position` is in characters, not in bytes.
321    /// ## `position`
322    /// the position of the cursor
323    #[doc(alias = "gtk_editable_set_position")]
324    fn set_position(&self, position: i32) {
325        unsafe {
326            ffi::gtk_editable_set_position(self.as_ref().to_glib_none().0, position);
327        }
328    }
329}
330
331impl<O: IsA<Editable>> EditableExt for O {}