Skip to main content

gtk4/
text.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::mem::transmute;
4
5use glib::{
6    GString,
7    signal::{SignalHandlerId, connect_raw},
8    translate::*,
9};
10
11use crate::{DeleteType, MovementStep, Text, Widget, ffi, prelude::*};
12
13impl Text {
14    ///  key.
15    pub fn connect_activate<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
16        unsafe extern "C" fn activate_trampoline<F: Fn(&Text) + 'static>(
17            this: *mut ffi::GtkText,
18            f: glib::ffi::gpointer,
19        ) {
20            unsafe {
21                let f: &F = &*(f as *const F);
22                f(&from_glib_borrow(this))
23            }
24        }
25        unsafe {
26            let f: Box<F> = Box::new(f);
27            connect_raw(
28                self.as_ptr() as *mut _,
29                c"activate".as_ptr() as *const _,
30                Some(transmute::<*const (), unsafe extern "C" fn()>(
31                    activate_trampoline::<F> as *const (),
32                )),
33                Box::into_raw(f),
34            )
35        }
36    }
37
38    /// .
39    pub fn connect_backspace<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
40        unsafe extern "C" fn backspace_trampoline<F: Fn(&Text) + 'static>(
41            this: *mut ffi::GtkText,
42            f: glib::ffi::gpointer,
43        ) {
44            unsafe {
45                let f: &F = &*(f as *const F);
46                f(&from_glib_borrow(this))
47            }
48        }
49        unsafe {
50            let f: Box<F> = Box::new(f);
51            connect_raw(
52                self.as_ptr() as *mut _,
53                c"backspace".as_ptr() as *const _,
54                Some(transmute::<*const (), unsafe extern "C" fn()>(
55                    backspace_trampoline::<F> as *const (),
56                )),
57                Box::into_raw(f),
58            )
59        }
60    }
61
62    /// .
63    pub fn connect_copy_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
64        unsafe extern "C" fn copy_clipboard_trampoline<F: Fn(&Text) + 'static>(
65            this: *mut ffi::GtkText,
66            f: glib::ffi::gpointer,
67        ) {
68            unsafe {
69                let f: &F = &*(f as *const F);
70                f(&from_glib_borrow(this))
71            }
72        }
73        unsafe {
74            let f: Box<F> = Box::new(f);
75            connect_raw(
76                self.as_ptr() as *mut _,
77                c"copy-clipboard".as_ptr() as *const _,
78                Some(transmute::<*const (), unsafe extern "C" fn()>(
79                    copy_clipboard_trampoline::<F> as *const (),
80                )),
81                Box::into_raw(f),
82            )
83        }
84    }
85
86    /// .
87    pub fn connect_cut_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
88        unsafe extern "C" fn cut_clipboard_trampoline<F: Fn(&Text) + 'static>(
89            this: *mut ffi::GtkText,
90            f: glib::ffi::gpointer,
91        ) {
92            unsafe {
93                let f: &F = &*(f as *const F);
94                f(&from_glib_borrow(this))
95            }
96        }
97        unsafe {
98            let f: Box<F> = Box::new(f);
99            connect_raw(
100                self.as_ptr() as *mut _,
101                c"cut-clipboard".as_ptr() as *const _,
102                Some(transmute::<*const (), unsafe extern "C" fn()>(
103                    cut_clipboard_trampoline::<F> as *const (),
104                )),
105                Box::into_raw(f),
106            )
107        }
108    }
109
110    ///
111    /// for deleting a word.
112    /// ## `type_`
113    /// the granularity of the deletion
114    /// ## `count`
115    /// the number of @type_ units to delete
116    pub fn connect_delete_from_cursor<F: Fn(&Text, DeleteType, i32) + 'static>(
117        &self,
118        f: F,
119    ) -> SignalHandlerId {
120        unsafe extern "C" fn delete_from_cursor_trampoline<
121            F: Fn(&Text, DeleteType, i32) + 'static,
122        >(
123            this: *mut ffi::GtkText,
124            type_: ffi::GtkDeleteType,
125            count: libc::c_int,
126            f: glib::ffi::gpointer,
127        ) {
128            unsafe {
129                let f: &F = &*(f as *const F);
130                f(&from_glib_borrow(this), from_glib(type_), count)
131            }
132        }
133        unsafe {
134            let f: Box<F> = Box::new(f);
135            connect_raw(
136                self.as_ptr() as *mut _,
137                c"delete-from-cursor".as_ptr() as *const _,
138                Some(transmute::<*const (), unsafe extern "C" fn()>(
139                    delete_from_cursor_trampoline::<F> as *const (),
140                )),
141                Box::into_raw(f),
142            )
143        }
144    }
145
146    /// Emitted when the user initiates the insertion of a
147    /// fixed string at the cursor.
148    ///
149    /// This is a [keybinding signal](class.SignalAction.html).
150    ///
151    /// This signal has no default bindings.
152    /// ## `string`
153    /// the string to insert
154    pub fn connect_insert_at_cursor<F: Fn(&Text, &str) + 'static>(&self, f: F) -> SignalHandlerId {
155        unsafe extern "C" fn insert_at_cursor_trampoline<F: Fn(&Text, &str) + 'static>(
156            this: *mut ffi::GtkText,
157            string: *mut libc::c_char,
158            f: glib::ffi::gpointer,
159        ) {
160            unsafe {
161                let f: &F = &*(f as *const F);
162                f(&from_glib_borrow(this), &GString::from_glib_borrow(string))
163            }
164        }
165        unsafe {
166            let f: Box<F> = Box::new(f);
167            connect_raw(
168                self.as_ptr() as *mut _,
169                c"insert-at-cursor".as_ptr() as *const _,
170                Some(transmute::<*const (), unsafe extern "C" fn()>(
171                    insert_at_cursor_trampoline::<F> as *const (),
172                )),
173                Box::into_raw(f),
174            )
175        }
176    }
177
178    /// /kbd
179    pub fn connect_insert_emoji<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
180        unsafe extern "C" fn insert_emoji_trampoline<F: Fn(&Text) + 'static>(
181            this: *mut ffi::GtkText,
182            f: glib::ffi::gpointer,
183        ) {
184            unsafe {
185                let f: &F = &*(f as *const F);
186                f(&from_glib_borrow(this))
187            }
188        }
189        unsafe {
190            let f: Box<F> = Box::new(f);
191            connect_raw(
192                self.as_ptr() as *mut _,
193                c"insert-emoji".as_ptr() as *const _,
194                Some(transmute::<*const (), unsafe extern "C" fn()>(
195                    insert_emoji_trampoline::<F> as *const (),
196                )),
197                Box::into_raw(f),
198            )
199        }
200    }
201
202    ///  move to the ends of the buffer
203    /// ## `step`
204    /// the granularity of the move
205    /// ## `count`
206    /// the number of @step units to move
207    /// ## `extend`
208    /// true if the move should extend the selection
209    pub fn connect_move_cursor<F: Fn(&Text, MovementStep, i32, bool) + 'static>(
210        &self,
211        f: F,
212    ) -> SignalHandlerId {
213        unsafe extern "C" fn move_cursor_trampoline<
214            F: Fn(&Text, MovementStep, i32, bool) + 'static,
215        >(
216            this: *mut ffi::GtkText,
217            step: ffi::GtkMovementStep,
218            count: libc::c_int,
219            extend: glib::ffi::gboolean,
220            f: glib::ffi::gpointer,
221        ) {
222            unsafe {
223                let f: &F = &*(f as *const F);
224                f(
225                    &from_glib_borrow(this),
226                    from_glib(step),
227                    count,
228                    from_glib(extend),
229                )
230            }
231        }
232        unsafe {
233            let f: Box<F> = Box::new(f);
234            connect_raw(
235                self.as_ptr() as *mut _,
236                c"move-cursor".as_ptr() as *const _,
237                Some(transmute::<*const (), unsafe extern "C" fn()>(
238                    move_cursor_trampoline::<F> as *const (),
239                )),
240                Box::into_raw(f),
241            )
242        }
243    }
244
245    /// .
246    pub fn connect_paste_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
247        unsafe extern "C" fn paste_clipboard_trampoline<F: Fn(&Text) + 'static>(
248            this: *mut ffi::GtkText,
249            f: glib::ffi::gpointer,
250        ) {
251            unsafe {
252                let f: &F = &*(f as *const F);
253                f(&from_glib_borrow(this))
254            }
255        }
256        unsafe {
257            let f: Box<F> = Box::new(f);
258            connect_raw(
259                self.as_ptr() as *mut _,
260                c"paste-clipboard".as_ptr() as *const _,
261                Some(transmute::<*const (), unsafe extern "C" fn()>(
262                    paste_clipboard_trampoline::<F> as *const (),
263                )),
264                Box::into_raw(f),
265            )
266        }
267    }
268
269    pub fn connect_populate_popup<F: Fn(&Text, &Widget) + 'static>(&self, f: F) -> SignalHandlerId {
270        unsafe extern "C" fn populate_popup_trampoline<F: Fn(&Text, &Widget) + 'static>(
271            this: *mut ffi::GtkText,
272            widget: *mut ffi::GtkWidget,
273            f: glib::ffi::gpointer,
274        ) {
275            unsafe {
276                let f: &F = &*(f as *const F);
277                f(&from_glib_borrow(this), &from_glib_borrow(widget))
278            }
279        }
280        unsafe {
281            let f: Box<F> = Box::new(f);
282            connect_raw(
283                self.as_ptr() as *mut _,
284                c"populate-popup".as_ptr() as *const _,
285                Some(transmute::<*const (), unsafe extern "C" fn()>(
286                    populate_popup_trampoline::<F> as *const (),
287                )),
288                Box::into_raw(f),
289            )
290        }
291    }
292
293    /// Emitted when the preedit text changes.
294    ///
295    /// If an input method is used, the typed text will not immediately
296    /// be committed to the buffer. So if you are interested in the text,
297    /// connect to this signal.
298    /// ## `preedit`
299    /// the current preedit string
300    pub fn connect_preedit_changed<F: Fn(&Text, &str) + 'static>(&self, f: F) -> SignalHandlerId {
301        unsafe extern "C" fn preedit_changed_trampoline<F: Fn(&Text, &str) + 'static>(
302            this: *mut ffi::GtkText,
303            preedit: *mut libc::c_char,
304            f: glib::ffi::gpointer,
305        ) {
306            unsafe {
307                let f: &F = &*(f as *const F);
308                f(&from_glib_borrow(this), &GString::from_glib_borrow(preedit))
309            }
310        }
311        unsafe {
312            let f: Box<F> = Box::new(f);
313            connect_raw(
314                self.as_ptr() as *mut _,
315                c"preedit-changed".as_ptr() as *const _,
316                Some(transmute::<*const (), unsafe extern "C" fn()>(
317                    preedit_changed_trampoline::<F> as *const (),
318                )),
319                Box::into_raw(f),
320            )
321        }
322    }
323
324    /// .
325    pub fn connect_toggle_overwrite<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
326        unsafe extern "C" fn toggle_overwrite_trampoline<F: Fn(&Text) + 'static>(
327            this: *mut ffi::GtkText,
328            f: glib::ffi::gpointer,
329        ) {
330            unsafe {
331                let f: &F = &*(f as *const F);
332                f(&from_glib_borrow(this))
333            }
334        }
335        unsafe {
336            let f: Box<F> = Box::new(f);
337            connect_raw(
338                self.as_ptr() as *mut _,
339                c"toggle-overwrite".as_ptr() as *const _,
340                Some(transmute::<*const (), unsafe extern "C" fn()>(
341                    toggle_overwrite_trampoline::<F> as *const (),
342                )),
343                Box::into_raw(f),
344            )
345        }
346    }
347
348    pub fn emit_activate(&self) {
349        self.emit_by_name::<()>("activate", &[]);
350    }
351
352    pub fn emit_backspace(&self) {
353        self.emit_by_name::<()>("backspace", &[]);
354    }
355
356    pub fn emit_copy_clipboard(&self) {
357        self.emit_by_name::<()>("copy-clipboard", &[]);
358    }
359
360    pub fn emit_cut_clipboard(&self) {
361        self.emit_by_name::<()>("cut-clipboard", &[]);
362    }
363
364    pub fn emit_delete_from_cursor(&self, type_: DeleteType, count: i32) {
365        self.emit_by_name::<()>("delete-from-cursor", &[&type_, &count]);
366    }
367
368    pub fn emit_insert_at_cursor(&self, string: impl IntoGStr) {
369        string.run_with_gstr(|string| {
370            self.emit_by_name::<()>("insert-at-cursor", &[&string]);
371        });
372    }
373
374    pub fn emit_insert_emoji(&self) {
375        self.emit_by_name::<()>("insert-emoji", &[]);
376    }
377
378    pub fn emit_move_cursor(&self, step: MovementStep, count: i32, extend: bool) {
379        self.emit_by_name::<()>("move-cursor", &[&step, &count, &extend]);
380    }
381
382    pub fn emit_paste_clipboard(&self) {
383        self.emit_by_name::<()>("paste-clipboard", &[]);
384    }
385
386    pub fn emit_preedit_changed(&self, preedit: impl IntoGStr) {
387        preedit.run_with_gstr(|preedit| {
388            self.emit_by_name::<()>("preedit-changed", &[&preedit]);
389        });
390    }
391
392    pub fn emit_toggle_overwrite(&self) {
393        self.emit_by_name::<()>("toggle-overwrite", &[]);
394    }
395}