1use std::mem::transmute;
4
5use glib::{
6 signal::{connect_raw, SignalHandlerId},
7 translate::*,
8 GString,
9};
10
11use crate::{ffi, prelude::*, DeleteType, MovementStep, Text, Widget};
12
13impl Text {
14 pub fn connect_activate<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
15 unsafe extern "C" fn activate_trampoline<F: Fn(&Text) + 'static>(
16 this: *mut ffi::GtkText,
17 f: glib::ffi::gpointer,
18 ) {
19 let f: &F = &*(f as *const F);
20 f(&from_glib_borrow(this))
21 }
22 unsafe {
23 let f: Box<F> = Box::new(f);
24 connect_raw(
25 self.as_ptr() as *mut _,
26 c"activate".as_ptr() as *const _,
27 Some(transmute::<usize, unsafe extern "C" fn()>(
28 activate_trampoline::<F> as usize,
29 )),
30 Box::into_raw(f),
31 )
32 }
33 }
34
35 pub fn connect_backspace<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
36 unsafe extern "C" fn backspace_trampoline<F: Fn(&Text) + 'static>(
37 this: *mut ffi::GtkText,
38 f: glib::ffi::gpointer,
39 ) {
40 let f: &F = &*(f as *const F);
41 f(&from_glib_borrow(this))
42 }
43 unsafe {
44 let f: Box<F> = Box::new(f);
45 connect_raw(
46 self.as_ptr() as *mut _,
47 c"backspace".as_ptr() as *const _,
48 Some(transmute::<usize, unsafe extern "C" fn()>(
49 backspace_trampoline::<F> as usize,
50 )),
51 Box::into_raw(f),
52 )
53 }
54 }
55
56 pub fn connect_copy_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
57 unsafe extern "C" fn copy_clipboard_trampoline<F: Fn(&Text) + 'static>(
58 this: *mut ffi::GtkText,
59 f: glib::ffi::gpointer,
60 ) {
61 let f: &F = &*(f as *const F);
62 f(&from_glib_borrow(this))
63 }
64 unsafe {
65 let f: Box<F> = Box::new(f);
66 connect_raw(
67 self.as_ptr() as *mut _,
68 c"copy-clipboard".as_ptr() as *const _,
69 Some(transmute::<usize, unsafe extern "C" fn()>(
70 copy_clipboard_trampoline::<F> as usize,
71 )),
72 Box::into_raw(f),
73 )
74 }
75 }
76
77 pub fn connect_cut_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
78 unsafe extern "C" fn cut_clipboard_trampoline<F: Fn(&Text) + 'static>(
79 this: *mut ffi::GtkText,
80 f: glib::ffi::gpointer,
81 ) {
82 let f: &F = &*(f as *const F);
83 f(&from_glib_borrow(this))
84 }
85 unsafe {
86 let f: Box<F> = Box::new(f);
87 connect_raw(
88 self.as_ptr() as *mut _,
89 c"cut-clipboard".as_ptr() as *const _,
90 Some(transmute::<usize, unsafe extern "C" fn()>(
91 cut_clipboard_trampoline::<F> as usize,
92 )),
93 Box::into_raw(f),
94 )
95 }
96 }
97
98 pub fn connect_delete_from_cursor<F: Fn(&Text, DeleteType, i32) + 'static>(
99 &self,
100 f: F,
101 ) -> SignalHandlerId {
102 unsafe extern "C" fn delete_from_cursor_trampoline<
103 F: Fn(&Text, DeleteType, i32) + 'static,
104 >(
105 this: *mut ffi::GtkText,
106 type_: ffi::GtkDeleteType,
107 count: libc::c_int,
108 f: glib::ffi::gpointer,
109 ) {
110 let f: &F = &*(f as *const F);
111 f(&from_glib_borrow(this), from_glib(type_), count)
112 }
113 unsafe {
114 let f: Box<F> = Box::new(f);
115 connect_raw(
116 self.as_ptr() as *mut _,
117 c"delete-from-cursor".as_ptr() as *const _,
118 Some(transmute::<usize, unsafe extern "C" fn()>(
119 delete_from_cursor_trampoline::<F> as usize,
120 )),
121 Box::into_raw(f),
122 )
123 }
124 }
125
126 pub fn connect_insert_at_cursor<F: Fn(&Text, &str) + 'static>(&self, f: F) -> SignalHandlerId {
127 unsafe extern "C" fn insert_at_cursor_trampoline<F: Fn(&Text, &str) + 'static>(
128 this: *mut ffi::GtkText,
129 string: *mut libc::c_char,
130 f: glib::ffi::gpointer,
131 ) {
132 let f: &F = &*(f as *const F);
133 f(&from_glib_borrow(this), &GString::from_glib_borrow(string))
134 }
135 unsafe {
136 let f: Box<F> = Box::new(f);
137 connect_raw(
138 self.as_ptr() as *mut _,
139 c"insert-at-cursor".as_ptr() as *const _,
140 Some(transmute::<usize, unsafe extern "C" fn()>(
141 insert_at_cursor_trampoline::<F> as usize,
142 )),
143 Box::into_raw(f),
144 )
145 }
146 }
147
148 pub fn connect_insert_emoji<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
149 unsafe extern "C" fn insert_emoji_trampoline<F: Fn(&Text) + 'static>(
150 this: *mut ffi::GtkText,
151 f: glib::ffi::gpointer,
152 ) {
153 let f: &F = &*(f as *const F);
154 f(&from_glib_borrow(this))
155 }
156 unsafe {
157 let f: Box<F> = Box::new(f);
158 connect_raw(
159 self.as_ptr() as *mut _,
160 c"insert-emoji".as_ptr() as *const _,
161 Some(transmute::<usize, unsafe extern "C" fn()>(
162 insert_emoji_trampoline::<F> as usize,
163 )),
164 Box::into_raw(f),
165 )
166 }
167 }
168
169 pub fn connect_move_cursor<F: Fn(&Text, MovementStep, i32, bool) + 'static>(
170 &self,
171 f: F,
172 ) -> SignalHandlerId {
173 unsafe extern "C" fn move_cursor_trampoline<
174 F: Fn(&Text, MovementStep, i32, bool) + 'static,
175 >(
176 this: *mut ffi::GtkText,
177 step: ffi::GtkMovementStep,
178 count: libc::c_int,
179 extend: glib::ffi::gboolean,
180 f: glib::ffi::gpointer,
181 ) {
182 let f: &F = &*(f as *const F);
183 f(
184 &from_glib_borrow(this),
185 from_glib(step),
186 count,
187 from_glib(extend),
188 )
189 }
190 unsafe {
191 let f: Box<F> = Box::new(f);
192 connect_raw(
193 self.as_ptr() as *mut _,
194 c"move-cursor".as_ptr() as *const _,
195 Some(transmute::<usize, unsafe extern "C" fn()>(
196 move_cursor_trampoline::<F> as usize,
197 )),
198 Box::into_raw(f),
199 )
200 }
201 }
202
203 pub fn connect_paste_clipboard<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
204 unsafe extern "C" fn paste_clipboard_trampoline<F: Fn(&Text) + 'static>(
205 this: *mut ffi::GtkText,
206 f: glib::ffi::gpointer,
207 ) {
208 let f: &F = &*(f as *const F);
209 f(&from_glib_borrow(this))
210 }
211 unsafe {
212 let f: Box<F> = Box::new(f);
213 connect_raw(
214 self.as_ptr() as *mut _,
215 c"paste-clipboard".as_ptr() as *const _,
216 Some(transmute::<usize, unsafe extern "C" fn()>(
217 paste_clipboard_trampoline::<F> as usize,
218 )),
219 Box::into_raw(f),
220 )
221 }
222 }
223
224 pub fn connect_populate_popup<F: Fn(&Text, &Widget) + 'static>(&self, f: F) -> SignalHandlerId {
225 unsafe extern "C" fn populate_popup_trampoline<F: Fn(&Text, &Widget) + 'static>(
226 this: *mut ffi::GtkText,
227 widget: *mut ffi::GtkWidget,
228 f: glib::ffi::gpointer,
229 ) {
230 let f: &F = &*(f as *const F);
231 f(&from_glib_borrow(this), &from_glib_borrow(widget))
232 }
233 unsafe {
234 let f: Box<F> = Box::new(f);
235 connect_raw(
236 self.as_ptr() as *mut _,
237 c"populate-popup".as_ptr() as *const _,
238 Some(transmute::<usize, unsafe extern "C" fn()>(
239 populate_popup_trampoline::<F> as usize,
240 )),
241 Box::into_raw(f),
242 )
243 }
244 }
245
246 pub fn connect_preedit_changed<F: Fn(&Text, &str) + 'static>(&self, f: F) -> SignalHandlerId {
247 unsafe extern "C" fn preedit_changed_trampoline<F: Fn(&Text, &str) + 'static>(
248 this: *mut ffi::GtkText,
249 preedit: *mut libc::c_char,
250 f: glib::ffi::gpointer,
251 ) {
252 let f: &F = &*(f as *const F);
253 f(&from_glib_borrow(this), &GString::from_glib_borrow(preedit))
254 }
255 unsafe {
256 let f: Box<F> = Box::new(f);
257 connect_raw(
258 self.as_ptr() as *mut _,
259 c"preedit-changed".as_ptr() as *const _,
260 Some(transmute::<usize, unsafe extern "C" fn()>(
261 preedit_changed_trampoline::<F> as usize,
262 )),
263 Box::into_raw(f),
264 )
265 }
266 }
267
268 pub fn connect_toggle_overwrite<F: Fn(&Text) + 'static>(&self, f: F) -> SignalHandlerId {
269 unsafe extern "C" fn toggle_overwrite_trampoline<F: Fn(&Text) + 'static>(
270 this: *mut ffi::GtkText,
271 f: glib::ffi::gpointer,
272 ) {
273 let f: &F = &*(f as *const F);
274 f(&from_glib_borrow(this))
275 }
276 unsafe {
277 let f: Box<F> = Box::new(f);
278 connect_raw(
279 self.as_ptr() as *mut _,
280 c"toggle-overwrite".as_ptr() as *const _,
281 Some(transmute::<usize, unsafe extern "C" fn()>(
282 toggle_overwrite_trampoline::<F> as usize,
283 )),
284 Box::into_raw(f),
285 )
286 }
287 }
288
289 pub fn emit_activate(&self) {
290 self.emit_by_name::<()>("activate", &[]);
291 }
292
293 pub fn emit_backspace(&self) {
294 self.emit_by_name::<()>("backspace", &[]);
295 }
296
297 pub fn emit_copy_clipboard(&self) {
298 self.emit_by_name::<()>("copy-clipboard", &[]);
299 }
300
301 pub fn emit_cut_clipboard(&self) {
302 self.emit_by_name::<()>("cut-clipboard", &[]);
303 }
304
305 pub fn emit_delete_from_cursor(&self, type_: DeleteType, count: i32) {
306 self.emit_by_name::<()>("delete-from-cursor", &[&type_, &count]);
307 }
308
309 pub fn emit_insert_at_cursor(&self, string: impl IntoGStr) {
310 string.run_with_gstr(|string| {
311 self.emit_by_name::<()>("insert-at-cursor", &[&string]);
312 });
313 }
314
315 pub fn emit_insert_emoji(&self) {
316 self.emit_by_name::<()>("insert-emoji", &[]);
317 }
318
319 pub fn emit_move_cursor(&self, step: MovementStep, count: i32, extend: bool) {
320 self.emit_by_name::<()>("move-cursor", &[&step, &count, &extend]);
321 }
322
323 pub fn emit_paste_clipboard(&self) {
324 self.emit_by_name::<()>("paste-clipboard", &[]);
325 }
326
327 pub fn emit_preedit_changed(&self, preedit: impl IntoGStr) {
328 preedit.run_with_gstr(|preedit| {
329 self.emit_by_name::<()>("preedit-changed", &[&preedit]);
330 });
331 }
332
333 pub fn emit_toggle_overwrite(&self) {
334 self.emit_by_name::<()>("toggle-overwrite", &[]);
335 }
336}