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