1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::AccelFlags;
use crate::AccelGroup;
use glib::object::{Cast, IsA};
use glib::translate::*;
use glib::ToValue;

pub trait AccelGroupExtManual: 'static {
    /// Installs an accelerator in this group. When `self` is being
    /// activated in response to a call to [`accel_groups_activate()`][crate::accel_groups_activate()],
    /// `closure` will be invoked if the `accel_key` and `accel_mods` from
    /// [`accel_groups_activate()`][crate::accel_groups_activate()] match those of this connection.
    ///
    /// The signature used for the `closure` is that of `GtkAccelGroupActivate`.
    ///
    /// Note that, due to implementation details, a single closure can
    /// only be connected to one accelerator group.
    /// ## `accel_key`
    /// key value of the accelerator
    /// ## `accel_mods`
    /// modifier combination of the accelerator
    /// ## `accel_flags`
    /// a flag mask to configure this accelerator
    /// ## `closure`
    /// closure to be executed upon accelerator activation
    fn connect_accel_group<F>(
        &self,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        accel_flags: AccelFlags,
        func: F,
    ) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static;

    /// Installs an accelerator in this group, using an accelerator path
    /// to look up the appropriate key and modifiers (see
    /// `gtk_accel_map_add_entry()`). When `self` is being activated
    /// in response to a call to [`accel_groups_activate()`][crate::accel_groups_activate()], `closure` will
    /// be invoked if the `accel_key` and `accel_mods` from
    /// [`accel_groups_activate()`][crate::accel_groups_activate()] match the key and modifiers for the path.
    ///
    /// The signature used for the `closure` is that of `GtkAccelGroupActivate`.
    ///
    /// Note that `accel_path` string will be stored in a `GQuark`. Therefore,
    /// if you pass a static string, you can save some memory by interning it
    /// first with `g_intern_static_string()`.
    /// ## `accel_path`
    /// path used for determining key and modifiers
    /// ## `closure`
    /// closure to be executed upon accelerator activation
    fn connect_accel_group_by_path<F>(&self, accel_path: &str, func: F) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static;
}

impl<O: IsA<AccelGroup>> AccelGroupExtManual for O {
    fn connect_accel_group<F>(
        &self,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        accel_flags: AccelFlags,
        func: F,
    ) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
    {
        let closure = glib::Closure::new_local(move |values| {
            assert_eq!(values.len(), 4);
            let s = values[0]
                .get::<AccelGroup>()
                .expect("Wrong argument type for first closure argument");
            let s = s
                .downcast::<Self>()
                .expect("Wrong argument type for first closure argument");

            let obj = values[1]
                .get::<glib::Object>()
                .expect("Wrong argument type for second closure argument");
            let accel_key = values[2]
                .get::<u32>()
                .expect("Wrong argument type for third closure argument");
            let accel_mods = values[3]
                .get::<gdk::ModifierType>()
                .expect("Wrong argument type for fourth closure argument");

            let ret = func(&s, &obj, accel_key, accel_mods);

            Some(ret.to_value())
        });

        unsafe {
            ffi::gtk_accel_group_connect(
                self.as_ref().to_glib_none().0,
                accel_key,
                accel_mods.into_glib(),
                accel_flags.into_glib(),
                closure.to_glib_none().0,
            );
        }

        closure
    }

    fn connect_accel_group_by_path<F>(&self, accel_path: &str, func: F) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
    {
        let closure = glib::Closure::new_local(move |values| {
            assert_eq!(values.len(), 4);
            let s = values[0]
                .get::<AccelGroup>()
                .expect("Wrong argument type for first closure argument");
            let s = s
                .downcast::<Self>()
                .expect("Wrong argument type for first closure argument");
            let obj = values[1]
                .get::<glib::Object>()
                .expect("Wrong argument type for second closure argument");
            let accel_key = values[2]
                .get::<u32>()
                .expect("Wrong argument type for third closure argument");
            let accel_mods = values[3]
                .get::<gdk::ModifierType>()
                .expect("Wrong argument type for fourth closure argument");

            let ret = func(&s, &obj, accel_key, accel_mods);

            Some(ret.to_value())
        });

        unsafe {
            ffi::gtk_accel_group_connect_by_path(
                self.as_ref().to_glib_none().0,
                accel_path.to_glib_none().0,
                closure.to_glib_none().0,
            );
        }

        closure
    }
}