gtk/accel_group.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::AccelGroup;
4use crate::{AccelFlags, ffi};
5use glib::object::{Cast, IsA};
6use glib::translate::*;
7use glib::value::ToValue;
8
9pub trait AccelGroupExtManual: IsA<AccelGroup> + 'static {
10 /// Installs an accelerator in this group. When `self` is being
11 /// activated in response to a call to [`accel_groups_activate()`][crate::accel_groups_activate()],
12 /// `closure` will be invoked if the `accel_key` and `accel_mods` from
13 /// [`accel_groups_activate()`][crate::accel_groups_activate()] match those of this connection.
14 ///
15 /// The signature used for the `closure` is that of `GtkAccelGroupActivate`.
16 ///
17 /// Note that, due to implementation details, a single closure can
18 /// only be connected to one accelerator group.
19 /// ## `accel_key`
20 /// key value of the accelerator
21 /// ## `accel_mods`
22 /// modifier combination of the accelerator
23 /// ## `accel_flags`
24 /// a flag mask to configure this accelerator
25 /// ## `closure`
26 /// closure to be executed upon accelerator activation
27 fn connect_accel_group<F>(
28 &self,
29 accel_key: u32,
30 accel_mods: gdk::ModifierType,
31 accel_flags: AccelFlags,
32 func: F,
33 ) -> glib::Closure
34 where
35 F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
36 {
37 let closure = glib::Closure::new_local(move |values| {
38 assert_eq!(values.len(), 4);
39 let s = values[0]
40 .get::<AccelGroup>()
41 .expect("Wrong argument type for first closure argument");
42 let s = s
43 .downcast::<Self>()
44 .expect("Wrong argument type for first closure argument");
45
46 let obj = values[1]
47 .get::<glib::Object>()
48 .expect("Wrong argument type for second closure argument");
49 let accel_key = values[2]
50 .get::<u32>()
51 .expect("Wrong argument type for third closure argument");
52 let accel_mods = values[3]
53 .get::<gdk::ModifierType>()
54 .expect("Wrong argument type for fourth closure argument");
55
56 let ret = func(&s, &obj, accel_key, accel_mods);
57
58 Some(ret.to_value())
59 });
60
61 unsafe {
62 ffi::gtk_accel_group_connect(
63 self.as_ref().to_glib_none().0,
64 accel_key,
65 accel_mods.into_glib(),
66 accel_flags.into_glib(),
67 closure.to_glib_none().0,
68 );
69 }
70
71 closure
72 }
73
74 /// Installs an accelerator in this group, using an accelerator path
75 /// to look up the appropriate key and modifiers (see
76 /// `gtk_accel_map_add_entry()`). When `self` is being activated
77 /// in response to a call to [`accel_groups_activate()`][crate::accel_groups_activate()], `closure` will
78 /// be invoked if the `accel_key` and `accel_mods` from
79 /// [`accel_groups_activate()`][crate::accel_groups_activate()] match the key and modifiers for the path.
80 ///
81 /// The signature used for the `closure` is that of `GtkAccelGroupActivate`.
82 ///
83 /// Note that `accel_path` string will be stored in a `GQuark`. Therefore,
84 /// if you pass a static string, you can save some memory by interning it
85 /// first with `g_intern_static_string()`.
86 /// ## `accel_path`
87 /// path used for determining key and modifiers
88 /// ## `closure`
89 /// closure to be executed upon accelerator activation
90 fn connect_accel_group_by_path<F>(&self, accel_path: &str, func: F) -> glib::Closure
91 where
92 F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
93 {
94 let closure = glib::Closure::new_local(move |values| {
95 assert_eq!(values.len(), 4);
96 let s = values[0]
97 .get::<AccelGroup>()
98 .expect("Wrong argument type for first closure argument");
99 let s = s
100 .downcast::<Self>()
101 .expect("Wrong argument type for first closure argument");
102 let obj = values[1]
103 .get::<glib::Object>()
104 .expect("Wrong argument type for second closure argument");
105 let accel_key = values[2]
106 .get::<u32>()
107 .expect("Wrong argument type for third closure argument");
108 let accel_mods = values[3]
109 .get::<gdk::ModifierType>()
110 .expect("Wrong argument type for fourth closure argument");
111
112 let ret = func(&s, &obj, accel_key, accel_mods);
113
114 Some(ret.to_value())
115 });
116
117 unsafe {
118 ffi::gtk_accel_group_connect_by_path(
119 self.as_ref().to_glib_none().0,
120 accel_path.to_glib_none().0,
121 closure.to_glib_none().0,
122 );
123 }
124
125 closure
126 }
127}
128
129impl<O: IsA<AccelGroup>> AccelGroupExtManual for O {}