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::object::Cast;
8
9use super::window::WindowImpl;
10use crate::ResponseType;
11use crate::{Dialog, ffi};
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 /// Signal emitted when the user uses a keybinding to close the dialog.
25 fn close(&self) {
26 self.parent_close()
27 }
28}
29
30mod sealed {
31 pub trait Sealed {}
32 impl<T: super::DialogImpl> Sealed for T {}
33}
34
35pub trait DialogImplExt: ObjectSubclass + sealed::Sealed {
36 fn parent_response(&self, response: ResponseType) {
37 unsafe {
38 let data = Self::type_data();
39 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDialogClass;
40 if let Some(f) = (*parent_class).response {
41 f(
42 self.obj().unsafe_cast_ref::<Dialog>().to_glib_none().0,
43 response.into_glib(),
44 )
45 }
46 }
47 }
48 fn parent_close(&self) {
49 unsafe {
50 let data = Self::type_data();
51 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDialogClass;
52 if let Some(f) = (*parent_class).close {
53 f(self.obj().unsafe_cast_ref::<Dialog>().to_glib_none().0)
54 }
55 }
56 }
57}
58
59impl<T: DialogImpl> DialogImplExt for T {}
60
61unsafe impl<T: DialogImpl> IsSubclassable<T> for Dialog {
62 fn class_init(class: &mut ::glib::Class<Self>) {
63 Self::parent_class_init::<T>(class);
64
65 if !crate::rt::is_initialized() {
66 panic!("GTK has to be initialized first");
67 }
68
69 let klass = class.as_mut();
70 klass.response = Some(dialog_response::<T>);
71 klass.close = Some(dialog_close::<T>);
72 }
73}
74
75unsafe extern "C" fn dialog_response<T: DialogImpl>(ptr: *mut ffi::GtkDialog, responseptr: i32) {
76 unsafe {
77 let instance = &*(ptr as *mut T::Instance);
78 let imp = instance.imp();
79 let res: ResponseType = from_glib(responseptr);
80
81 imp.response(res)
82 }
83}
84
85unsafe extern "C" fn dialog_close<T: DialogImpl>(ptr: *mut ffi::GtkDialog) {
86 unsafe {
87 let instance = &*(ptr as *mut T::Instance);
88 let imp = instance.imp();
89
90 imp.close()
91 }
92}