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