gtk4/subclass/dialog.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for subclassing [`Dialog`](crate::Dialog).
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, Dialog, ResponseType};
9
10pub trait DialogImpl: DialogImplExt + WindowImpl {
11 /// Emits the ::response signal with the given response ID.
12 ///
13 /// Used to indicate that the user has responded to the dialog in some way.
14 ///
15 /// # Deprecated since 4.10
16 ///
17 /// Use [`Window`][crate::Window] instead
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::DialogImplExt> Sealed for T {}
33}
34
35pub trait DialogImplExt: sealed::Sealed + ObjectSubclass {
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
49 fn parent_close(&self) {
50 unsafe {
51 let data = Self::type_data();
52 let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDialogClass;
53 if let Some(f) = (*parent_class).close {
54 f(self.obj().unsafe_cast_ref::<Dialog>().to_glib_none().0)
55 }
56 }
57 }
58}
59
60impl<T: DialogImpl> DialogImplExt for T {}
61
62unsafe impl<T: DialogImpl> IsSubclassable<T> for Dialog {
63 fn class_init(class: &mut glib::Class<Self>) {
64 Self::parent_class_init::<T>(class);
65
66 let klass = class.as_mut();
67 klass.response = Some(dialog_response::<T>);
68 klass.close = Some(dialog_close::<T>);
69 }
70}
71
72unsafe extern "C" fn dialog_response<T: DialogImpl>(ptr: *mut ffi::GtkDialog, responseptr: i32) {
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
80unsafe extern "C" fn dialog_close<T: DialogImpl>(ptr: *mut ffi::GtkDialog) {
81 let instance = &*(ptr as *mut T::Instance);
82 let imp = instance.imp();
83
84 imp.close()
85}