clutter/auto/
binding_pool.rs

1use crate::ModifierType;
2use glib::{
3    object::{IsA, ObjectType as ObjectType_},
4    translate::*,
5    GString, StaticType, Value,
6};
7use std::boxed::Box as Box_;
8use std::fmt;
9
10glib_wrapper! {
11    pub struct BindingPool(Object<ffi::ClutterBindingPool, ffi::ClutterBindingPoolClass, BindingPoolClass>);
12
13    match fn {
14        get_type => || ffi::clutter_binding_pool_get_type(),
15    }
16}
17
18impl BindingPool {
19    /// Creates a new `BindingPool` that can be used to store
20    /// key bindings for an actor. The `name` must be a unique identifier
21    /// for the binding pool, so that `BindingPool::find` will
22    /// be able to return the correct binding pool.
23    /// ## `name`
24    /// the name of the binding pool
25    ///
26    /// # Returns
27    ///
28    /// the newly created binding pool with the given
29    ///  name. Use `gobject::ObjectExt::unref` when done.
30    pub fn new(name: &str) -> BindingPool {
31        unsafe { from_glib_full(ffi::clutter_binding_pool_new(name.to_glib_none().0)) }
32    }
33
34    /// Activates the callback associated to the action that is
35    /// bound to the `key_val` and `modifiers` pair.
36    ///
37    /// The callback has the following signature:
38    ///
39    ///
40    /// ```text
41    ///   void (* callback) (GObject             *gobject,
42    ///                      const gchar         *action_name,
43    ///                      guint                key_val,
44    ///                      ClutterModifierType  modifiers,
45    ///                      gpointer             user_data);
46    /// ```
47    ///
48    /// Where the `gobject::Object` instance is `gobject` and the user data
49    /// is the one passed when installing the action with
50    /// `BindingPool::install_action`.
51    ///
52    /// If the action bound to the `key_val`, `modifiers` pair has been
53    /// blocked using `BindingPool::block_action`, the callback
54    /// will not be invoked, and this function will return `false`.
55    /// ## `key_val`
56    /// the key symbol
57    /// ## `modifiers`
58    /// bitmask for the modifiers
59    /// ## `gobject`
60    /// a `gobject::Object`
61    ///
62    /// # Returns
63    ///
64    /// `true` if an action was found and was activated
65    pub fn activate<P: IsA<glib::Object>>(
66        &self,
67        key_val: u32,
68        modifiers: ModifierType,
69        gobject: &P,
70    ) -> bool {
71        unsafe {
72            from_glib(ffi::clutter_binding_pool_activate(
73                self.to_glib_none().0,
74                key_val,
75                modifiers.to_glib(),
76                gobject.as_ref().to_glib_none().0,
77            ))
78        }
79    }
80
81    /// Blocks all the actions with name `action_name` inside `self`.
82    /// ## `action_name`
83    /// an action name
84    pub fn block_action(&self, action_name: &str) {
85        unsafe {
86            ffi::clutter_binding_pool_block_action(
87                self.to_glib_none().0,
88                action_name.to_glib_none().0,
89            );
90        }
91    }
92
93    /// Retrieves the name of the action matching the given key symbol
94    /// and modifiers bitmask.
95    /// ## `key_val`
96    /// a key symbol
97    /// ## `modifiers`
98    /// a bitmask for the modifiers
99    ///
100    /// # Returns
101    ///
102    /// the name of the action, if found, or `None`. The
103    ///  returned string is owned by the binding pool and should never
104    ///  be modified or freed
105    pub fn find_action(&self, key_val: u32, modifiers: ModifierType) -> Option<GString> {
106        unsafe {
107            from_glib_none(ffi::clutter_binding_pool_find_action(
108                self.to_glib_none().0,
109                key_val,
110                modifiers.to_glib(),
111            ))
112        }
113    }
114
115    // /// Installs a new action inside a `BindingPool`. The action
116    // /// is bound to `key_val` and `modifiers`.
117    // ///
118    // /// The same action name can be used for multiple `key_val`, `modifiers`
119    // /// pairs.
120    // ///
121    // /// When an action has been activated using `BindingPool::activate`
122    // /// the passed `callback` will be invoked (with `data`).
123    // ///
124    // /// Actions can be blocked with `BindingPool::block_action`
125    // /// and then unblocked using `BindingPool::unblock_action`.
126    // /// ## `action_name`
127    // /// the name of the action
128    // /// ## `key_val`
129    // /// key symbol
130    // /// ## `modifiers`
131    // /// bitmask of modifiers
132    // /// ## `callback`
133    // /// function to be called
134    // ///  when the action is activated
135    // /// ## `data`
136    // /// data to be passed to `callback`
137    // /// ## `notify`
138    // /// function to be called when the action is removed
139    // ///  from the pool
140    // pub fn install_action<P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static>(
141    //     &self,
142    //     action_name: &str,
143    //     key_val: u32,
144    //     modifiers: ModifierType,
145    //     callback: P,
146    // ) {
147    //     let callback_data: Box_<P> = Box_::new(callback);
148    //     unsafe extern "C" fn callback_func<
149    //         P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static,
150    //     >(
151    //         gobject: *mut gobject_sys::GObject,
152    //         action_name: *const libc::c_char,
153    //         key_val: libc::c_uint,
154    //         modifiers: ffi::ClutterModifierType,
155    //         user_data: glib_sys::gpointer,
156    //     ) -> glib_sys::gboolean {
157    //         let gobject = from_glib_borrow(gobject);
158    //         let action_name: Borrowed<GString> = from_glib_borrow(action_name);
159    //         let modifiers = from_glib_borrow(modifiers);
160    //         let callback: &P = &*(user_data as *mut _);
161    //         let res = (*callback)(&gobject, action_name.as_str(), key_val, &modifiers);
162    //         res.to_glib()
163    //     }
164    //     let callback = Some(callback_func::<P> as _);
165    //     unsafe extern "C" fn notify_func<
166    //         P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static,
167    //     >(
168    //         data: glib_sys::gpointer,
169    //     ) {
170    //         let _callback: Box_<P> = Box_::from_raw(data as *mut _);
171    //     }
172    //     let destroy_call6 = Some(notify_func::<P> as _);
173    //     let super_callback0: Box_<P> = callback_data;
174    //     unsafe {
175    //         ffi::clutter_binding_pool_install_action(
176    //             self.to_glib_none().0,
177    //             action_name.to_glib_none().0,
178    //             key_val,
179    //             modifiers.to_glib(),
180    //             callback,
181    //             Box_::into_raw(super_callback0) as *mut _,
182    //             destroy_call6,
183    //         );
184    //     }
185    // }
186
187    /// A `gobject::Closure` variant of `BindingPool::install_action`.
188    ///
189    /// Installs a new action inside a `BindingPool`. The action
190    /// is bound to `key_val` and `modifiers`.
191    ///
192    /// The same action name can be used for multiple `key_val`, `modifiers`
193    /// pairs.
194    ///
195    /// When an action has been activated using `BindingPool::activate`
196    /// the passed `closure` will be invoked.
197    ///
198    /// Actions can be blocked with `BindingPool::block_action`
199    /// and then unblocked using `BindingPool::unblock_action`.
200    /// ## `action_name`
201    /// the name of the action
202    /// ## `key_val`
203    /// key symbol
204    /// ## `modifiers`
205    /// bitmask of modifiers
206    /// ## `closure`
207    /// a `gobject::Closure`
208    pub fn install_closure(
209        &self,
210        action_name: &str,
211        key_val: u32,
212        modifiers: ModifierType,
213        closure: &glib::Closure,
214    ) {
215        unsafe {
216            ffi::clutter_binding_pool_install_closure(
217                self.to_glib_none().0,
218                action_name.to_glib_none().0,
219                key_val,
220                modifiers.to_glib(),
221                closure.to_glib_none().0,
222            );
223        }
224    }
225
226    // /// Allows overriding the action for `key_val` and `modifiers` inside a
227    // /// `BindingPool`. See `BindingPool::install_action`.
228    // ///
229    // /// When an action has been activated using `BindingPool::activate`
230    // /// the passed `callback` will be invoked (with `data`).
231    // ///
232    // /// Actions can be blocked with `BindingPool::block_action`
233    // /// and then unblocked using `BindingPool::unblock_action`.
234    // /// ## `key_val`
235    // /// key symbol
236    // /// ## `modifiers`
237    // /// bitmask of modifiers
238    // /// ## `callback`
239    // /// function to be called when the action is activated
240    // /// ## `data`
241    // /// data to be passed to `callback`
242    // /// ## `notify`
243    // /// function to be called when the action is removed
244    // ///  from the pool
245    // pub fn override_action<P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static>(
246    //     &self,
247    //     key_val: u32,
248    //     modifiers: ModifierType,
249    //     callback: P,
250    // ) {
251    //     let callback_data: Box_<P> = Box_::new(callback);
252    //     unsafe extern "C" fn callback_func<
253    //         P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static,
254    //     >(
255    //         gobject: *mut gobject_sys::GObject,
256    //         action_name: *const libc::c_char,
257    //         key_val: libc::c_uint,
258    //         modifiers: ffi::ClutterModifierType,
259    //         user_data: glib_sys::gpointer,
260    //     ) -> glib_sys::gboolean {
261    //         let gobject = from_glib_borrow(gobject);
262    //         let action_name: Borrowed<GString> = from_glib_borrow(action_name);
263    //         let modifiers = from_glib_borrow(modifiers);
264    //         let callback: &P = &*(user_data as *mut _);
265    //         let res = (*callback)(&gobject, action_name.as_str(), key_val, &modifiers);
266    //         res.to_glib()
267    //     }
268    //     let callback = Some(callback_func::<P> as _);
269    //     unsafe extern "C" fn notify_func<
270    //         P: Fn(&glib::Object, &str, u32, &ModifierType) -> bool + 'static,
271    //     >(
272    //         data: glib_sys::gpointer,
273    //     ) {
274    //         let _callback: Box_<P> = Box_::from_raw(data as *mut _);
275    //     }
276    //     let destroy_call5 = Some(notify_func::<P> as _);
277    //     let super_callback0: Box_<P> = callback_data;
278    //     unsafe {
279    //         ffi::clutter_binding_pool_override_action(
280    //             self.to_glib_none().0,
281    //             key_val,
282    //             modifiers.to_glib(),
283    //             callback,
284    //             Box_::into_raw(super_callback0) as *mut _,
285    //             destroy_call5,
286    //         );
287    //     }
288    // }
289
290    /// A `gobject::Closure` variant of `BindingPool::override_action`.
291    ///
292    /// Allows overriding the action for `key_val` and `modifiers` inside a
293    /// `BindingPool`. See `BindingPool::install_closure`.
294    ///
295    /// When an action has been activated using `BindingPool::activate`
296    /// the passed `callback` will be invoked (with `data`).
297    ///
298    /// Actions can be blocked with `BindingPool::block_action`
299    /// and then unblocked using `BindingPool::unblock_action`.
300    /// ## `key_val`
301    /// key symbol
302    /// ## `modifiers`
303    /// bitmask of modifiers
304    /// ## `closure`
305    /// a `gobject::Closure`
306    pub fn override_closure(&self, key_val: u32, modifiers: ModifierType, closure: &glib::Closure) {
307        unsafe {
308            ffi::clutter_binding_pool_override_closure(
309                self.to_glib_none().0,
310                key_val,
311                modifiers.to_glib(),
312                closure.to_glib_none().0,
313            );
314        }
315    }
316
317    /// Removes the action matching the given `key_val`, `modifiers` pair,
318    /// if any exists.
319    /// ## `key_val`
320    /// a key symbol
321    /// ## `modifiers`
322    /// a bitmask for the modifiers
323    pub fn remove_action(&self, key_val: u32, modifiers: ModifierType) {
324        unsafe {
325            ffi::clutter_binding_pool_remove_action(
326                self.to_glib_none().0,
327                key_val,
328                modifiers.to_glib(),
329            );
330        }
331    }
332
333    /// Unblockes all the actions with name `action_name` inside `self`.
334    ///
335    /// Unblocking an action does not cause the callback bound to it to
336    /// be invoked in case `BindingPool::activate` was called on
337    /// an action previously blocked with `BindingPool::block_action`.
338    /// ## `action_name`
339    /// an action name
340    pub fn unblock_action(&self, action_name: &str) {
341        unsafe {
342            ffi::clutter_binding_pool_unblock_action(
343                self.to_glib_none().0,
344                action_name.to_glib_none().0,
345            );
346        }
347    }
348
349    /// The unique name of the `BindingPool`.
350    pub fn get_property_name(&self) -> Option<GString> {
351        unsafe {
352            let mut value = Value::from_type(<GString as StaticType>::static_type());
353            gobject_sys::g_object_get_property(
354                self.as_ptr() as *mut gobject_sys::GObject,
355                b"name\0".as_ptr() as *const _,
356                value.to_glib_none_mut().0,
357            );
358            value
359                .get()
360                .expect("Return Value for property `name` getter")
361        }
362    }
363
364    /// Finds the `BindingPool` with `name`.
365    /// ## `name`
366    /// the name of the binding pool to find
367    ///
368    /// # Returns
369    ///
370    /// a pointer to the `BindingPool`, or `None`
371    pub fn find(name: &str) -> Option<BindingPool> {
372        unsafe { from_glib_none(ffi::clutter_binding_pool_find(name.to_glib_none().0)) }
373    }
374
375    //pub fn get_for_class(klass: /*Unimplemented*/Option<Fundamental: Pointer>) -> Option<BindingPool> {
376    //    unsafe { TODO: call clutter_sys:clutter_binding_pool_get_for_class() }
377    //}
378}
379
380impl fmt::Display for BindingPool {
381    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
382        write!(f, "BindingPool")
383    }
384}