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 glib::{prelude::*, translate::*};
6use std::{fmt, mem};
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
105mod sealed {
106 pub trait Sealed {}
107 impl<T: super::IsA<super::Editable>> Sealed for T {}
108}
109
110/// Trait containing all [`struct@Editable`] methods.
111///
112/// # Implementors
113///
114/// [`Editable`][struct@crate::Editable], [`Entry`][struct@crate::Entry], [`SearchEntry`][struct@crate::SearchEntry], [`SpinButton`][struct@crate::SpinButton]
115pub trait EditableExt: IsA<Editable> + sealed::Sealed + 'static {
116 /// Copies the contents of the currently selected content in the editable and
117 /// puts it on the clipboard.
118 #[doc(alias = "gtk_editable_copy_clipboard")]
119 fn copy_clipboard(&self) {
120 unsafe {
121 ffi::gtk_editable_copy_clipboard(self.as_ref().to_glib_none().0);
122 }
123 }
124
125 /// Removes the contents of the currently selected content in the editable and
126 /// puts it on the clipboard.
127 #[doc(alias = "gtk_editable_cut_clipboard")]
128 fn cut_clipboard(&self) {
129 unsafe {
130 ffi::gtk_editable_cut_clipboard(self.as_ref().to_glib_none().0);
131 }
132 }
133
134 /// Deletes the currently selected text of the editable.
135 /// This call doesn’t do anything if there is no selected text.
136 #[doc(alias = "gtk_editable_delete_selection")]
137 fn delete_selection(&self) {
138 unsafe {
139 ffi::gtk_editable_delete_selection(self.as_ref().to_glib_none().0);
140 }
141 }
142
143 /// Deletes a sequence of characters. The characters that are deleted are
144 /// those characters at positions from `start_pos` up to, but not including
145 /// `end_pos`. If `end_pos` is negative, then the characters deleted
146 /// are those from `start_pos` to the end of the text.
147 ///
148 /// Note that the positions are specified in characters, not bytes.
149 /// ## `start_pos`
150 /// start position
151 /// ## `end_pos`
152 /// end position
153 #[doc(alias = "gtk_editable_delete_text")]
154 fn delete_text(&self, start_pos: i32, end_pos: i32) {
155 unsafe {
156 ffi::gtk_editable_delete_text(self.as_ref().to_glib_none().0, start_pos, end_pos);
157 }
158 }
159
160 /// Retrieves a sequence of characters. The characters that are retrieved
161 /// are those characters at positions from `start_pos` up to, but not
162 /// including `end_pos`. If `end_pos` is negative, then the characters
163 /// retrieved are those characters from `start_pos` to the end of the text.
164 ///
165 /// Note that positions are specified in characters, not bytes.
166 /// ## `start_pos`
167 /// start of text
168 /// ## `end_pos`
169 /// end of text
170 ///
171 /// # Returns
172 ///
173 /// a pointer to the contents of the widget as a
174 /// string. This string is allocated by the [`Editable`][crate::Editable]
175 /// implementation and should be freed by the caller.
176 #[doc(alias = "gtk_editable_get_chars")]
177 #[doc(alias = "get_chars")]
178 fn chars(&self, start_pos: i32, end_pos: i32) -> Option<glib::GString> {
179 unsafe {
180 from_glib_full(ffi::gtk_editable_get_chars(
181 self.as_ref().to_glib_none().0,
182 start_pos,
183 end_pos,
184 ))
185 }
186 }
187
188 /// Retrieves whether `self` is editable. See
189 /// [`set_editable()`][Self::set_editable()].
190 ///
191 /// # Returns
192 ///
193 /// [`true`] if `self` is editable.
194 #[doc(alias = "gtk_editable_get_editable")]
195 #[doc(alias = "get_editable")]
196 fn is_editable(&self) -> bool {
197 unsafe {
198 from_glib(ffi::gtk_editable_get_editable(
199 self.as_ref().to_glib_none().0,
200 ))
201 }
202 }
203
204 /// Retrieves the current position of the cursor relative to the start
205 /// of the content of the editable.
206 ///
207 /// Note that this position is in characters, not in bytes.
208 ///
209 /// # Returns
210 ///
211 /// the cursor position
212 #[doc(alias = "gtk_editable_get_position")]
213 #[doc(alias = "get_position")]
214 fn position(&self) -> i32 {
215 unsafe { ffi::gtk_editable_get_position(self.as_ref().to_glib_none().0) }
216 }
217
218 /// Retrieves the selection bound of the editable. start_pos will be filled
219 /// with the start of the selection and `end_pos` with end. If no text was
220 /// selected both will be identical and [`false`] will be returned.
221 ///
222 /// Note that positions are specified in characters, not bytes.
223 ///
224 /// # Returns
225 ///
226 /// [`true`] if an area is selected, [`false`] otherwise
227 ///
228 /// ## `start_pos`
229 /// location to store the starting position, or [`None`]
230 ///
231 /// ## `end_pos`
232 /// location to store the end position, or [`None`]
233 #[doc(alias = "gtk_editable_get_selection_bounds")]
234 #[doc(alias = "get_selection_bounds")]
235 fn selection_bounds(&self) -> Option<(i32, i32)> {
236 unsafe {
237 let mut start_pos = mem::MaybeUninit::uninit();
238 let mut end_pos = mem::MaybeUninit::uninit();
239 let ret = from_glib(ffi::gtk_editable_get_selection_bounds(
240 self.as_ref().to_glib_none().0,
241 start_pos.as_mut_ptr(),
242 end_pos.as_mut_ptr(),
243 ));
244 if ret {
245 Some((start_pos.assume_init(), end_pos.assume_init()))
246 } else {
247 None
248 }
249 }
250 }
251
252 /// Inserts `new_text_length` bytes of `new_text` into the contents of the
253 /// widget, at position `position`.
254 ///
255 /// Note that the position is in characters, not in bytes.
256 /// The function updates `position` to point after the newly inserted text.
257 /// ## `new_text`
258 /// the text to append
259 /// ## `new_text_length`
260 /// the length of the text in bytes, or -1
261 /// ## `position`
262 /// location of the position text will be inserted at
263 #[doc(alias = "gtk_editable_insert_text")]
264 fn insert_text(&self, new_text: &str, position: &mut i32) {
265 let new_text_length = new_text.len() as _;
266 unsafe {
267 ffi::gtk_editable_insert_text(
268 self.as_ref().to_glib_none().0,
269 new_text.to_glib_none().0,
270 new_text_length,
271 position,
272 );
273 }
274 }
275
276 /// Pastes the content of the clipboard to the current position of the
277 /// cursor in the editable.
278 #[doc(alias = "gtk_editable_paste_clipboard")]
279 fn paste_clipboard(&self) {
280 unsafe {
281 ffi::gtk_editable_paste_clipboard(self.as_ref().to_glib_none().0);
282 }
283 }
284
285 /// Selects a region of text. The characters that are selected are
286 /// those characters at positions from `start_pos` up to, but not
287 /// including `end_pos`. If `end_pos` is negative, then the
288 /// characters selected are those characters from `start_pos` to
289 /// the end of the text.
290 ///
291 /// Note that positions are specified in characters, not bytes.
292 /// ## `start_pos`
293 /// start of region
294 /// ## `end_pos`
295 /// end of region
296 #[doc(alias = "gtk_editable_select_region")]
297 fn select_region(&self, start_pos: i32, end_pos: i32) {
298 unsafe {
299 ffi::gtk_editable_select_region(self.as_ref().to_glib_none().0, start_pos, end_pos);
300 }
301 }
302
303 /// Determines if the user can edit the text in the editable
304 /// widget or not.
305 /// ## `is_editable`
306 /// [`true`] if the user is allowed to edit the text
307 /// in the widget
308 #[doc(alias = "gtk_editable_set_editable")]
309 fn set_editable(&self, is_editable: bool) {
310 unsafe {
311 ffi::gtk_editable_set_editable(self.as_ref().to_glib_none().0, is_editable.into_glib());
312 }
313 }
314
315 /// Sets the cursor position in the editable to the given value.
316 ///
317 /// The cursor is displayed before the character with the given (base 0)
318 /// index in the contents of the editable. The value must be less than or
319 /// equal to the number of characters in the editable. A value of -1
320 /// indicates that the position should be set after the last character
321 /// of the editable. Note that `position` is in characters, not in bytes.
322 /// ## `position`
323 /// the position of the cursor
324 #[doc(alias = "gtk_editable_set_position")]
325 fn set_position(&self, position: i32) {
326 unsafe {
327 ffi::gtk_editable_set_position(self.as_ref().to_glib_none().0, position);
328 }
329 }
330}
331
332impl<O: IsA<Editable>> EditableExt for O {}
333
334impl fmt::Display for Editable {
335 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
336 f.write_str("Editable")
337 }
338}