Skip to main content

gtk/subclass/
dialog.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use glib::subclass::prelude::*;
6
7use glib::Cast;
8
9use super::window::WindowImpl;
10use crate::Dialog;
11use crate::ResponseType;
12
13pub trait DialogImpl: DialogImplExt + WindowImpl {
14    /// Emits the [`response`][struct@crate::Dialog#response] signal with the given response ID.
15    /// Used to indicate that the user has responded to the dialog in some way;
16    /// typically either you or [`DialogExt::run()`][crate::prelude::DialogExt::run()] will be monitoring the
17    /// ::response signal and take appropriate action.
18    /// ## `response_id`
19    /// response ID
20    fn response(&self, response: ResponseType) {
21        self.parent_response(response)
22    }
23
24    fn close(&self) {
25        self.parent_close()
26    }
27}
28
29mod sealed {
30    pub trait Sealed {}
31    impl<T: super::DialogImpl> Sealed for T {}
32}
33
34pub trait DialogImplExt: ObjectSubclass + sealed::Sealed {
35    fn parent_response(&self, response: ResponseType) {
36        unsafe {
37            let data = Self::type_data();
38            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDialogClass;
39            if let Some(f) = (*parent_class).response {
40                f(
41                    self.obj().unsafe_cast_ref::<Dialog>().to_glib_none().0,
42                    response.into_glib(),
43                )
44            }
45        }
46    }
47    fn parent_close(&self) {
48        unsafe {
49            let data = Self::type_data();
50            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDialogClass;
51            if let Some(f) = (*parent_class).close {
52                f(self.obj().unsafe_cast_ref::<Dialog>().to_glib_none().0)
53            }
54        }
55    }
56}
57
58impl<T: DialogImpl> DialogImplExt for T {}
59
60unsafe impl<T: DialogImpl> IsSubclassable<T> for Dialog {
61    fn class_init(class: &mut ::glib::Class<Self>) {
62        Self::parent_class_init::<T>(class);
63
64        if !crate::rt::is_initialized() {
65            panic!("GTK has to be initialized first");
66        }
67
68        let klass = class.as_mut();
69        klass.response = Some(dialog_response::<T>);
70        klass.close = Some(dialog_close::<T>);
71    }
72}
73
74unsafe extern "C" fn dialog_response<T: DialogImpl>(ptr: *mut ffi::GtkDialog, responseptr: i32) {
75    let instance = &*(ptr as *mut T::Instance);
76    let imp = instance.imp();
77    let res: ResponseType = from_glib(responseptr);
78
79    imp.response(res)
80}
81
82unsafe extern "C" fn dialog_close<T: DialogImpl>(ptr: *mut ffi::GtkDialog) {
83    let instance = &*(ptr as *mut T::Instance);
84    let imp = instance.imp();
85
86    imp.close()
87}