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
// Take a look at the license at the top of the repository in the LICENSE file.

use libc::{c_char, c_uint};

use glib::subclass::prelude::*;
use glib::translate::*;
use glib::{Cast, GString};

use super::cell_renderer_text::CellRendererTextImpl;
use crate::CellRendererAccel;

pub trait CellRendererAccelImpl: CellRendererAccelImplExt + CellRendererTextImpl {
    fn accel_edited(
        &self,
        path: &str,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        hardware_keycode: u32,
    ) {
        self.parent_accel_edited(path, accel_key, accel_mods, hardware_keycode);
    }

    fn accel_cleared(&self, path: &str) {
        self.parent_accel_cleared(path);
    }
}

pub trait CellRendererAccelImplExt: ObjectSubclass {
    fn parent_accel_edited(
        &self,
        path: &str,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        hardware_keycode: u32,
    );
    fn parent_accel_cleared(&self, path: &str);
}

impl<T: CellRendererAccelImpl> CellRendererAccelImplExt for T {
    fn parent_accel_edited(
        &self,
        path: &str,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        hardware_keycode: u32,
    ) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellRendererAccelClass;
            if let Some(f) = (*parent_class).accel_edited {
                f(
                    self.instance()
                        .unsafe_cast_ref::<CellRendererAccel>()
                        .to_glib_none()
                        .0,
                    path.to_glib_none().0,
                    accel_key,
                    accel_mods.into_glib(),
                    hardware_keycode,
                )
            }
        }
    }

    fn parent_accel_cleared(&self, path: &str) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkCellRendererAccelClass;
            if let Some(f) = (*parent_class).accel_cleared {
                f(
                    self.instance()
                        .unsafe_cast_ref::<CellRendererAccel>()
                        .to_glib_none()
                        .0,
                    path.to_glib_none().0,
                )
            }
        }
    }
}

unsafe impl<T: CellRendererAccelImpl> IsSubclassable<T> for CellRendererAccel {
    fn class_init(class: &mut ::glib::Class<Self>) {
        Self::parent_class_init::<T>(class);

        if !crate::rt::is_initialized() {
            panic!("GTK has to be initialized first");
        }

        let klass = class.as_mut();
        klass.accel_edited = Some(cell_renderer_accel_edited::<T>);
        klass.accel_cleared = Some(cell_renderer_accel_cleared::<T>);
    }
}

unsafe extern "C" fn cell_renderer_accel_edited<T: CellRendererAccelImpl>(
    ptr: *mut ffi::GtkCellRendererAccel,
    path: *const c_char,
    accel_key: c_uint,
    accel_mods: gdk::ffi::GdkModifierType,
    hardware_keycode: c_uint,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.accel_edited(
        &GString::from_glib_borrow(path),
        accel_key,
        from_glib(accel_mods),
        hardware_keycode,
    )
}

unsafe extern "C" fn cell_renderer_accel_cleared<T: CellRendererAccelImpl>(
    ptr: *mut ffi::GtkCellRendererAccel,
    path: *const c_char,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.accel_cleared(&GString::from_glib_borrow(path))
}