[go: up one dir, main page]

gtk/
menu.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::Menu;
4use crate::Widget;
5use glib::object::Cast;
6use glib::translate::*;
7use glib::IsA;
8use libc::c_int;
9use std::boxed::Box as Box_;
10use std::ptr;
11
12mod sealed {
13    pub trait Sealed {}
14    impl<T: glib::IsA<crate::Menu>> Sealed for T {}
15}
16
17pub trait GtkMenuExtManual: IsA<Menu> + sealed::Sealed + 'static {
18    #[doc(alias = "gtk_menu_popup")]
19    fn popup<
20        T: IsA<Widget>,
21        U: IsA<Widget>,
22        F: FnOnce(&Self, &mut i32, &mut i32) -> bool + 'static,
23    >(
24        &self,
25        parent_menu_shell: Option<&T>,
26        parent_menu_item: Option<&U>,
27        f: F,
28        button: u32,
29        activate_time: u32,
30    ) {
31        unsafe extern "C" fn position_callback<
32            T,
33            F: FnOnce(&T, &mut i32, &mut i32) -> bool + 'static,
34        >(
35            this: *mut ffi::GtkMenu,
36            x: *mut c_int,
37            y: *mut c_int,
38            push_in: *mut glib::ffi::gboolean,
39            f: glib::ffi::gpointer,
40        ) where
41            T: IsA<Menu>,
42        {
43            let mut f: Box<Option<F>> = Box::from_raw(f as *mut _);
44            let f = f.take().expect("No callback");
45            *push_in = f(
46                &Menu::from_glib_none(this).unsafe_cast(),
47                x.as_mut().unwrap(),
48                y.as_mut().unwrap(),
49            )
50            .into_glib();
51        }
52        unsafe {
53            let f: Box_<Option<F>> = Box_::new(Some(f));
54            ffi::gtk_menu_popup(
55                self.as_ref().to_glib_none().0,
56                parent_menu_shell.map(|p| p.as_ref()).to_glib_none().0,
57                parent_menu_item.map(|p| p.as_ref()).to_glib_none().0,
58                Some(position_callback::<Self, F>),
59                Box_::into_raw(f) as *mut _,
60                button,
61                activate_time,
62            )
63        }
64    }
65
66    #[doc(alias = "gtk_menu_popup_easy")]
67    fn popup_easy(&self, button: u32, activate_time: u32) {
68        unsafe {
69            ffi::gtk_menu_popup(
70                self.as_ref().to_glib_none().0,
71                ptr::null_mut(),
72                ptr::null_mut(),
73                None,
74                ptr::null_mut(),
75                button,
76                activate_time,
77            )
78        }
79    }
80}
81
82impl<O: IsA<Menu>> GtkMenuExtManual for O {}