Skip to main content

gtk/auto/
native_dialog.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ResponseType, Window};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::{boxed::Box as Box_, fmt, mem::transmute};
12
13glib::wrapper! {
14    /// Native dialogs are platform dialogs that don't use [`Dialog`][crate::Dialog] or
15    /// [`Window`][crate::Window]. They are used in order to integrate better with a
16    /// platform, by looking the same as other native applications and
17    /// supporting platform specific features.
18    ///
19    /// The [`Dialog`][crate::Dialog] functions cannot be used on such objects, but we
20    /// need a similar API in order to drive them. The [`NativeDialog`][crate::NativeDialog]
21    /// object is an API that allows you to do this. It allows you to set
22    /// various common properties on the dialog, as well as show and hide
23    /// it and get a [`response`][struct@crate::NativeDialog#response] signal when the user finished
24    /// with the dialog.
25    ///
26    /// There is also a [`NativeDialogExt::run()`][crate::prelude::NativeDialogExt::run()] helper that makes it easy
27    /// to run any native dialog in a modal way with a recursive mainloop,
28    /// similar to [`DialogExt::run()`][crate::prelude::DialogExt::run()].
29    ///
30    /// This is an Abstract Base Class, you cannot instantiate it.
31    ///
32    /// ## Properties
33    ///
34    ///
35    /// #### `modal`
36    ///  Whether the window should be modal with respect to its transient parent.
37    ///
38    /// Readable | Writeable
39    ///
40    ///
41    /// #### `title`
42    ///  The title of the dialog window
43    ///
44    /// Readable | Writeable
45    ///
46    ///
47    /// #### `transient-for`
48    ///  The transient parent of the dialog, or [`None`] for none.
49    ///
50    /// Readable | Writeable | Construct
51    ///
52    ///
53    /// #### `visible`
54    ///  Whether the window is currenlty visible.
55    ///
56    /// Readable | Writeable
57    ///
58    /// ## Signals
59    ///
60    ///
61    /// #### `response`
62    ///  Emitted when the user responds to the dialog.
63    ///
64    /// When this is called the dialog has been hidden.
65    ///
66    /// If you call [`NativeDialogExt::hide()`][crate::prelude::NativeDialogExt::hide()] before the user responds to
67    /// the dialog this signal will not be emitted.
68    ///
69    ///
70    ///
71    /// # Implements
72    ///
73    /// [`NativeDialogExt`][trait@crate::prelude::NativeDialogExt], [`trait@glib::ObjectExt`], [`NativeDialogExtManual`][trait@crate::prelude::NativeDialogExtManual]
74    #[doc(alias = "GtkNativeDialog")]
75    pub struct NativeDialog(Object<ffi::GtkNativeDialog, ffi::GtkNativeDialogClass>);
76
77    match fn {
78        type_ => || ffi::gtk_native_dialog_get_type(),
79    }
80}
81
82impl NativeDialog {
83    pub const NONE: Option<&'static NativeDialog> = None;
84}
85
86mod sealed {
87    pub trait Sealed {}
88    impl<T: super::IsA<super::NativeDialog>> Sealed for T {}
89}
90
91/// Trait containing all [`struct@NativeDialog`] methods.
92///
93/// # Implementors
94///
95/// [`FileChooserNative`][struct@crate::FileChooserNative], [`NativeDialog`][struct@crate::NativeDialog]
96pub trait NativeDialogExt: IsA<NativeDialog> + sealed::Sealed + 'static {
97    #[doc(alias = "gtk_native_dialog_destroy")]
98    fn destroy(&self) {
99        unsafe {
100            ffi::gtk_native_dialog_destroy(self.as_ref().to_glib_none().0);
101        }
102    }
103
104    /// Returns whether the dialog is modal. See [`set_modal()`][Self::set_modal()].
105    ///
106    /// # Returns
107    ///
108    /// [`true`] if the dialog is set to be modal
109    #[doc(alias = "gtk_native_dialog_get_modal")]
110    #[doc(alias = "get_modal")]
111    fn is_modal(&self) -> bool {
112        unsafe {
113            from_glib(ffi::gtk_native_dialog_get_modal(
114                self.as_ref().to_glib_none().0,
115            ))
116        }
117    }
118
119    /// Gets the title of the [`NativeDialog`][crate::NativeDialog].
120    ///
121    /// # Returns
122    ///
123    /// the title of the dialog, or [`None`] if none has
124    ///  been set explicitly. The returned string is owned by the widget
125    ///  and must not be modified or freed.
126    #[doc(alias = "gtk_native_dialog_get_title")]
127    #[doc(alias = "get_title")]
128    fn title(&self) -> Option<glib::GString> {
129        unsafe {
130            from_glib_none(ffi::gtk_native_dialog_get_title(
131                self.as_ref().to_glib_none().0,
132            ))
133        }
134    }
135
136    /// Fetches the transient parent for this window. See
137    /// [`set_transient_for()`][Self::set_transient_for()].
138    ///
139    /// # Returns
140    ///
141    /// the transient parent for this window,
142    /// or [`None`] if no transient parent has been set.
143    #[doc(alias = "gtk_native_dialog_get_transient_for")]
144    #[doc(alias = "get_transient_for")]
145    fn transient_for(&self) -> Option<Window> {
146        unsafe {
147            from_glib_none(ffi::gtk_native_dialog_get_transient_for(
148                self.as_ref().to_glib_none().0,
149            ))
150        }
151    }
152
153    /// Determines whether the dialog is visible.
154    ///
155    /// # Returns
156    ///
157    /// [`true`] if the dialog is visible
158    #[doc(alias = "gtk_native_dialog_get_visible")]
159    #[doc(alias = "get_visible")]
160    fn is_visible(&self) -> bool {
161        unsafe {
162            from_glib(ffi::gtk_native_dialog_get_visible(
163                self.as_ref().to_glib_none().0,
164            ))
165        }
166    }
167
168    /// Hides the dialog if it is visilbe, aborting any interaction. Once this
169    /// is called the [`response`][struct@crate::NativeDialog#response] signal will not be emitted
170    /// until after the next call to [`show()`][Self::show()].
171    ///
172    /// If the dialog is not visible this does nothing.
173    #[doc(alias = "gtk_native_dialog_hide")]
174    fn hide(&self) {
175        unsafe {
176            ffi::gtk_native_dialog_hide(self.as_ref().to_glib_none().0);
177        }
178    }
179
180    /// Blocks in a recursive main loop until `self` emits the
181    /// [`response`][struct@crate::NativeDialog#response] signal. It then returns the response ID
182    /// from the ::response signal emission.
183    ///
184    /// Before entering the recursive main loop, [`run()`][Self::run()]
185    /// calls [`show()`][Self::show()] on the dialog for you.
186    ///
187    /// After [`run()`][Self::run()] returns, then dialog will be hidden.
188    ///
189    /// Typical usage of this function might be:
190    ///
191    ///
192    /// **⚠️ The following code is in C ⚠️**
193    ///
194    /// ```C
195    ///   gint result = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
196    ///   switch (result)
197    ///     {
198    ///       case GTK_RESPONSE_ACCEPT:
199    ///          do_application_specific_something ();
200    ///          break;
201    ///       default:
202    ///          do_nothing_since_dialog_was_cancelled ();
203    ///          break;
204    ///     }
205    ///   g_object_unref (dialog);
206    /// ```
207    ///
208    /// Note that even though the recursive main loop gives the effect of a
209    /// modal dialog (it prevents the user from interacting with other
210    /// windows in the same window group while the dialog is run), callbacks
211    /// such as timeouts, IO channel watches, DND drops, etc, will
212    /// be triggered during a [`run()`][Self::run()] call.
213    ///
214    /// # Returns
215    ///
216    /// response ID
217    #[doc(alias = "gtk_native_dialog_run")]
218    fn run(&self) -> ResponseType {
219        unsafe { from_glib(ffi::gtk_native_dialog_run(self.as_ref().to_glib_none().0)) }
220    }
221
222    /// Sets a dialog modal or non-modal. Modal dialogs prevent interaction
223    /// with other windows in the same application. To keep modal dialogs
224    /// on top of main application windows, use
225    /// [`set_transient_for()`][Self::set_transient_for()] to make the dialog transient for the
226    /// parent; most [window managers][gtk-X11-arch]
227    /// will then disallow lowering the dialog below the parent.
228    /// ## `modal`
229    /// whether the window is modal
230    #[doc(alias = "gtk_native_dialog_set_modal")]
231    fn set_modal(&self, modal: bool) {
232        unsafe {
233            ffi::gtk_native_dialog_set_modal(self.as_ref().to_glib_none().0, modal.into_glib());
234        }
235    }
236
237    /// Sets the title of the [`NativeDialog`][crate::NativeDialog].
238    /// ## `title`
239    /// title of the dialog
240    #[doc(alias = "gtk_native_dialog_set_title")]
241    fn set_title(&self, title: &str) {
242        unsafe {
243            ffi::gtk_native_dialog_set_title(
244                self.as_ref().to_glib_none().0,
245                title.to_glib_none().0,
246            );
247        }
248    }
249
250    /// Dialog windows should be set transient for the main application
251    /// window they were spawned from. This allows
252    /// [window managers][gtk-X11-arch] to e.g. keep the
253    /// dialog on top of the main window, or center the dialog over the
254    /// main window.
255    ///
256    /// Passing [`None`] for `parent` unsets the current transient window.
257    /// ## `parent`
258    /// parent window, or [`None`]
259    #[doc(alias = "gtk_native_dialog_set_transient_for")]
260    fn set_transient_for(&self, parent: Option<&impl IsA<Window>>) {
261        unsafe {
262            ffi::gtk_native_dialog_set_transient_for(
263                self.as_ref().to_glib_none().0,
264                parent.map(|p| p.as_ref()).to_glib_none().0,
265            );
266        }
267    }
268
269    /// Shows the dialog on the display, allowing the user to interact with
270    /// it. When the user accepts the state of the dialog the dialog will
271    /// be automatically hidden and the [`response`][struct@crate::NativeDialog#response] signal
272    /// will be emitted.
273    ///
274    /// Multiple calls while the dialog is visible will be ignored.
275    #[doc(alias = "gtk_native_dialog_show")]
276    fn show(&self) {
277        unsafe {
278            ffi::gtk_native_dialog_show(self.as_ref().to_glib_none().0);
279        }
280    }
281
282    /// Whether the window is currenlty visible.
283    fn set_visible(&self, visible: bool) {
284        ObjectExt::set_property(self.as_ref(), "visible", visible)
285    }
286
287    /// Emitted when the user responds to the dialog.
288    ///
289    /// When this is called the dialog has been hidden.
290    ///
291    /// If you call [`hide()`][Self::hide()] before the user responds to
292    /// the dialog this signal will not be emitted.
293    /// ## `response_id`
294    /// the response ID
295    #[doc(alias = "response")]
296    fn connect_response<F: Fn(&Self, ResponseType) + 'static>(&self, f: F) -> SignalHandlerId {
297        unsafe extern "C" fn response_trampoline<
298            P: IsA<NativeDialog>,
299            F: Fn(&P, ResponseType) + 'static,
300        >(
301            this: *mut ffi::GtkNativeDialog,
302            response_id: ffi::GtkResponseType,
303            f: glib::ffi::gpointer,
304        ) {
305            let f: &F = &*(f as *const F);
306            f(
307                NativeDialog::from_glib_borrow(this).unsafe_cast_ref(),
308                from_glib(response_id),
309            )
310        }
311        unsafe {
312            let f: Box_<F> = Box_::new(f);
313            connect_raw(
314                self.as_ptr() as *mut _,
315                b"response\0".as_ptr() as *const _,
316                Some(transmute::<_, unsafe extern "C" fn()>(
317                    response_trampoline::<Self, F> as *const (),
318                )),
319                Box_::into_raw(f),
320            )
321        }
322    }
323
324    #[doc(alias = "modal")]
325    fn connect_modal_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
326        unsafe extern "C" fn notify_modal_trampoline<P: IsA<NativeDialog>, F: Fn(&P) + 'static>(
327            this: *mut ffi::GtkNativeDialog,
328            _param_spec: glib::ffi::gpointer,
329            f: glib::ffi::gpointer,
330        ) {
331            let f: &F = &*(f as *const F);
332            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
333        }
334        unsafe {
335            let f: Box_<F> = Box_::new(f);
336            connect_raw(
337                self.as_ptr() as *mut _,
338                b"notify::modal\0".as_ptr() as *const _,
339                Some(transmute::<_, unsafe extern "C" fn()>(
340                    notify_modal_trampoline::<Self, F> as *const (),
341                )),
342                Box_::into_raw(f),
343            )
344        }
345    }
346
347    #[doc(alias = "title")]
348    fn connect_title_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
349        unsafe extern "C" fn notify_title_trampoline<P: IsA<NativeDialog>, F: Fn(&P) + 'static>(
350            this: *mut ffi::GtkNativeDialog,
351            _param_spec: glib::ffi::gpointer,
352            f: glib::ffi::gpointer,
353        ) {
354            let f: &F = &*(f as *const F);
355            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
356        }
357        unsafe {
358            let f: Box_<F> = Box_::new(f);
359            connect_raw(
360                self.as_ptr() as *mut _,
361                b"notify::title\0".as_ptr() as *const _,
362                Some(transmute::<_, unsafe extern "C" fn()>(
363                    notify_title_trampoline::<Self, F> as *const (),
364                )),
365                Box_::into_raw(f),
366            )
367        }
368    }
369
370    #[doc(alias = "transient-for")]
371    fn connect_transient_for_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
372        unsafe extern "C" fn notify_transient_for_trampoline<
373            P: IsA<NativeDialog>,
374            F: Fn(&P) + 'static,
375        >(
376            this: *mut ffi::GtkNativeDialog,
377            _param_spec: glib::ffi::gpointer,
378            f: glib::ffi::gpointer,
379        ) {
380            let f: &F = &*(f as *const F);
381            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
382        }
383        unsafe {
384            let f: Box_<F> = Box_::new(f);
385            connect_raw(
386                self.as_ptr() as *mut _,
387                b"notify::transient-for\0".as_ptr() as *const _,
388                Some(transmute::<_, unsafe extern "C" fn()>(
389                    notify_transient_for_trampoline::<Self, F> as *const (),
390                )),
391                Box_::into_raw(f),
392            )
393        }
394    }
395
396    #[doc(alias = "visible")]
397    fn connect_visible_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
398        unsafe extern "C" fn notify_visible_trampoline<
399            P: IsA<NativeDialog>,
400            F: Fn(&P) + 'static,
401        >(
402            this: *mut ffi::GtkNativeDialog,
403            _param_spec: glib::ffi::gpointer,
404            f: glib::ffi::gpointer,
405        ) {
406            let f: &F = &*(f as *const F);
407            f(NativeDialog::from_glib_borrow(this).unsafe_cast_ref())
408        }
409        unsafe {
410            let f: Box_<F> = Box_::new(f);
411            connect_raw(
412                self.as_ptr() as *mut _,
413                b"notify::visible\0".as_ptr() as *const _,
414                Some(transmute::<_, unsafe extern "C" fn()>(
415                    notify_visible_trampoline::<Self, F> as *const (),
416                )),
417                Box_::into_raw(f),
418            )
419        }
420    }
421}
422
423impl<O: IsA<NativeDialog>> NativeDialogExt for O {}
424
425impl fmt::Display for NativeDialog {
426    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
427        f.write_str("NativeDialog")
428    }
429}