Skip to main content

gtk4/auto/
file_chooser_native.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#![allow(deprecated)]
5
6use crate::{FileChooser, FileChooserAction, FileFilter, NativeDialog, Window, ffi};
7use glib::{
8    prelude::*,
9    signal::{SignalHandlerId, connect_raw},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Use [`FileDialog`][crate::FileDialog] instead
16    ///  commands.
17    ///
18    /// By default, this just uses a [`FileChooserDialog`][crate::FileChooserDialog] to implement
19    /// the actual dialog. However, on some platforms, such as Windows and
20    /// macOS, the native platform file chooser is used instead. When the
21    /// application is running in a sandboxed environment without direct
22    /// filesystem access (such as Flatpak), [`FileChooserNative`][crate::FileChooserNative] may call
23    /// the proper APIs (portals) to let the user choose a file and make it
24    /// available to the application.
25    ///
26    /// While the API of [`FileChooserNative`][crate::FileChooserNative] closely mirrors [`FileChooserDialog`][crate::FileChooserDialog],
27    /// the main difference is that there is no access to any [`Window`][crate::Window] or [`Widget`][crate::Widget]
28    /// for the dialog. This is required, as there may not be one in the case of a
29    /// platform native dialog.
30    ///
31    /// Showing, hiding and running the dialog is handled by the
32    /// [`NativeDialog`][crate::NativeDialog] functions.
33    ///
34    /// Note that unlike [`FileChooserDialog`][crate::FileChooserDialog], [`FileChooserNative`][crate::FileChooserNative] objects
35    /// are not toplevel widgets, and GTK does not keep them alive. It is your
36    /// responsibility to keep a reference until you are done with the
37    /// object.
38    ///
39    /// ## Typical usage
40    ///
41    /// In the simplest of cases, you can the following code to use
42    /// [`FileChooserNative`][crate::FileChooserNative] to select a file for opening:
43    ///
44    /// **⚠️ The following code is in c ⚠️**
45    ///
46    /// ```c
47    /// static void
48    /// on_response (GtkNativeDialog *native,
49    ///              int              response)
50    /// {
51    ///   if (response == GTK_RESPONSE_ACCEPT)
52    ///     {
53    ///       GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
54    ///       GFile *file = gtk_file_chooser_get_file (chooser);
55    ///
56    ///       open_file (file);
57    ///
58    ///       g_object_unref (file);
59    ///     }
60    ///
61    ///   g_object_unref (native);
62    /// }
63    ///
64    ///   // ...
65    ///   GtkFileChooserNative *native;
66    ///   GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
67    ///
68    ///   native = gtk_file_chooser_native_new ("Open File",
69    ///                                         parent_window,
70    ///                                         action,
71    ///                                         "_Open",
72    ///                                         "_Cancel");
73    ///
74    ///   g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
75    ///   gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
76    /// ```
77    ///
78    /// To use a [`FileChooserNative`][crate::FileChooserNative] for saving, you can use this:
79    ///
80    /// **⚠️ The following code is in c ⚠️**
81    ///
82    /// ```c
83    /// static void
84    /// on_response (GtkNativeDialog *native,
85    ///              int              response)
86    /// {
87    ///   if (response == GTK_RESPONSE_ACCEPT)
88    ///     {
89    ///       GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
90    ///       GFile *file = gtk_file_chooser_get_file (chooser);
91    ///
92    ///       save_to_file (file);
93    ///
94    ///       g_object_unref (file);
95    ///     }
96    ///
97    ///   g_object_unref (native);
98    /// }
99    ///
100    ///   // ...
101    ///   GtkFileChooserNative *native;
102    ///   GtkFileChooser *chooser;
103    ///   GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
104    ///
105    ///   native = gtk_file_chooser_native_new ("Save File",
106    ///                                         parent_window,
107    ///                                         action,
108    ///                                         "_Save",
109    ///                                         "_Cancel");
110    ///   chooser = GTK_FILE_CHOOSER (native);
111    ///
112    ///   if (user_edited_a_new_document)
113    ///     gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
114    ///   else
115    ///     gtk_file_chooser_set_file (chooser, existing_file, NULL);
116    ///
117    ///   g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
118    ///   gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
119    /// ```
120    ///
121    /// For more information on how to best set up a file dialog,
122    /// see the [`FileChooserDialog`][crate::FileChooserDialog] documentation.
123    ///
124    /// ## Response Codes
125    ///
126    /// [`FileChooserNative`][crate::FileChooserNative] inherits from [`NativeDialog`][crate::NativeDialog],
127    /// which means it will return [`ResponseType::Accept`][crate::ResponseType::Accept] if the user accepted,
128    /// and [`ResponseType::Cancel`][crate::ResponseType::Cancel] if he pressed cancel. It can also return
129    /// [`ResponseType::DeleteEvent`][crate::ResponseType::DeleteEvent] if the window was unexpectedly closed.
130    ///
131    /// ## Differences from [`FileChooserDialog`][crate::FileChooserDialog]
132    ///
133    /// There are a few things in the [`FileChooser`][crate::FileChooser] interface that
134    /// are not possible to use with [`FileChooserNative`][crate::FileChooserNative], as such use would
135    /// prohibit the use of a native dialog.
136    ///
137    /// No operations that change the dialog work while the dialog is visible.
138    /// Set all the properties that are required before showing the dialog.
139    ///
140    /// ## Win32 details
141    ///
142    /// On windows the `IFileDialog` implementation (added in Windows Vista) is
143    /// used. It supports many of the features that [`FileChooser`][crate::FileChooser] has, but
144    /// there are some things it does not handle:
145    ///
146    /// * Any [`FileFilter`][crate::FileFilter] added using a mimetype
147    ///
148    /// If any of these features are used the regular [`FileChooserDialog`][crate::FileChooserDialog]
149    /// will be used in place of the native one.
150    ///
151    /// ## Portal details
152    ///
153    /// When the `org.freedesktop.portal.FileChooser` portal is available on
154    /// the session bus, it is used to bring up an out-of-process file chooser.
155    /// Depending on the kind of session the application is running in, this may
156    /// or may not be a GTK file chooser.
157    ///
158    /// ## macOS details
159    ///
160    /// On macOS the `NSSavePanel` and `NSOpenPanel` classes are used to provide
161    /// native file chooser dialogs. Some features provided by [`FileChooser`][crate::FileChooser]
162    /// are not supported:
163    ///
164    /// * Shortcut folders.
165    ///
166    /// ## Properties
167    ///
168    ///
169    /// #### `accept-label`
170    ///  The text used for the label on the accept button in the dialog, or
171    /// [`None`] to use the default text.
172    ///
173    /// Readable | Writable
174    ///
175    ///
176    /// #### `cancel-label`
177    ///  The text used for the label on the cancel button in the dialog, or
178    /// [`None`] to use the default text.
179    ///
180    /// Readable | Writable
181    /// <details><summary><h4>NativeDialog</h4></summary>
182    ///
183    ///
184    /// #### `modal`
185    ///  Whether the window should be modal with respect to its transient parent.
186    ///
187    /// Readable | Writable
188    ///
189    ///
190    /// #### `title`
191    ///  The title of the dialog window
192    ///
193    /// Readable | Writable
194    ///
195    ///
196    /// #### `transient-for`
197    ///  The transient parent of the dialog, or [`None`] for none.
198    ///
199    /// Readable | Writable | Construct
200    ///
201    ///
202    /// #### `visible`
203    ///  Whether the window is currently visible.
204    ///
205    /// Readable | Writable
206    /// </details>
207    /// <details><summary><h4>FileChooser</h4></summary>
208    ///
209    ///
210    /// #### `action`
211    ///  The type of operation that the file chooser is performing.
212    ///
213    /// Readable | Writable
214    ///
215    ///
216    /// #### `create-folders`
217    ///  Whether a file chooser not in [`FileChooserAction::Open`][crate::FileChooserAction::Open] mode
218    /// will offer the user to create new folders.
219    ///
220    /// Readable | Writable
221    ///
222    ///
223    /// #### `filter`
224    ///  The current filter for selecting files that are displayed.
225    ///
226    /// Readable | Writable
227    ///
228    ///
229    /// #### `filters`
230    ///  A `GListModel` containing the filters that have been
231    /// added with gtk_file_chooser_add_filter().
232    ///
233    /// The returned object should not be modified. It may
234    /// or may not be updated for later changes.
235    ///
236    /// Readable
237    ///
238    ///
239    /// #### `select-multiple`
240    ///  Whether to allow multiple files to be selected.
241    ///
242    /// Readable | Writable
243    ///
244    ///
245    /// #### `shortcut-folders`
246    ///  A `GListModel` containing the shortcut folders that have been
247    /// added with gtk_file_chooser_add_shortcut_folder().
248    ///
249    /// The returned object should not be modified. It may
250    /// or may not be updated for later changes.
251    ///
252    /// Readable
253    /// </details>
254    ///
255    /// # Implements
256    ///
257    /// [`NativeDialogExt`][trait@crate::prelude::NativeDialogExt], [`trait@glib::ObjectExt`], [`FileChooserExt`][trait@crate::prelude::FileChooserExt], [`NativeDialogExtManual`][trait@crate::prelude::NativeDialogExtManual], [`FileChooserExtManual`][trait@crate::prelude::FileChooserExtManual]
258    #[doc(alias = "GtkFileChooserNative")]
259    pub struct FileChooserNative(Object<ffi::GtkFileChooserNative, ffi::GtkFileChooserNativeClass>) @extends NativeDialog, @implements FileChooser;
260
261    match fn {
262        type_ => || ffi::gtk_file_chooser_native_get_type(),
263    }
264}
265
266impl FileChooserNative {
267    /// Creates a new [`FileChooserNative`][crate::FileChooserNative].
268    ///
269    /// # Deprecated since 4.10
270    ///
271    /// Use [`FileDialog`][crate::FileDialog] instead
272    /// ## `title`
273    /// Title of the native
274    /// ## `parent`
275    /// Transient parent of the native
276    /// ## `action`
277    /// Open or save mode for the dialog
278    /// ## `accept_label`
279    /// text to go in the accept button, or [`None`] for the default
280    /// ## `cancel_label`
281    /// text to go in the cancel button, or [`None`] for the default
282    ///
283    /// # Returns
284    ///
285    /// a new [`FileChooserNative`][crate::FileChooserNative]
286    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
287    #[allow(deprecated)]
288    #[doc(alias = "gtk_file_chooser_native_new")]
289    pub fn new(
290        title: Option<&str>,
291        parent: Option<&impl IsA<Window>>,
292        action: FileChooserAction,
293        accept_label: Option<&str>,
294        cancel_label: Option<&str>,
295    ) -> FileChooserNative {
296        assert_initialized_main_thread!();
297        unsafe {
298            from_glib_full(ffi::gtk_file_chooser_native_new(
299                title.to_glib_none().0,
300                parent.map(|p| p.as_ref()).to_glib_none().0,
301                action.into_glib(),
302                accept_label.to_glib_none().0,
303                cancel_label.to_glib_none().0,
304            ))
305        }
306    }
307
308    // rustdoc-stripper-ignore-next
309    /// Creates a new builder-pattern struct instance to construct [`FileChooserNative`] objects.
310    ///
311    /// This method returns an instance of [`FileChooserNativeBuilder`](crate::builders::FileChooserNativeBuilder) which can be used to create [`FileChooserNative`] objects.
312    pub fn builder() -> FileChooserNativeBuilder {
313        FileChooserNativeBuilder::new()
314    }
315
316    /// Retrieves the custom label text for the accept button.
317    ///
318    /// # Deprecated since 4.10
319    ///
320    /// Use [`FileDialog`][crate::FileDialog] instead
321    ///
322    /// # Returns
323    ///
324    /// The custom label
325    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
326    #[allow(deprecated)]
327    #[doc(alias = "gtk_file_chooser_native_get_accept_label")]
328    #[doc(alias = "get_accept_label")]
329    #[doc(alias = "accept-label")]
330    pub fn accept_label(&self) -> Option<glib::GString> {
331        unsafe {
332            from_glib_none(ffi::gtk_file_chooser_native_get_accept_label(
333                self.to_glib_none().0,
334            ))
335        }
336    }
337
338    /// Retrieves the custom label text for the cancel button.
339    ///
340    /// # Deprecated since 4.10
341    ///
342    /// Use [`FileDialog`][crate::FileDialog] instead
343    ///
344    /// # Returns
345    ///
346    /// The custom label
347    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
348    #[allow(deprecated)]
349    #[doc(alias = "gtk_file_chooser_native_get_cancel_label")]
350    #[doc(alias = "get_cancel_label")]
351    #[doc(alias = "cancel-label")]
352    pub fn cancel_label(&self) -> Option<glib::GString> {
353        unsafe {
354            from_glib_none(ffi::gtk_file_chooser_native_get_cancel_label(
355                self.to_glib_none().0,
356            ))
357        }
358    }
359
360    ///  (two underscores). The first underlined character represents
361    /// a keyboard accelerator called a mnemonic.
362    ///
363    /// Pressing Alt and that key should activate the button.
364    ///
365    /// # Deprecated since 4.10
366    ///
367    /// Use [`FileDialog`][crate::FileDialog] instead
368    /// ## `accept_label`
369    /// custom label
370    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
371    #[allow(deprecated)]
372    #[doc(alias = "gtk_file_chooser_native_set_accept_label")]
373    #[doc(alias = "accept-label")]
374    pub fn set_accept_label(&self, accept_label: Option<&str>) {
375        unsafe {
376            ffi::gtk_file_chooser_native_set_accept_label(
377                self.to_glib_none().0,
378                accept_label.to_glib_none().0,
379            );
380        }
381    }
382
383    ///  (two underscores). The first underlined character represents
384    /// a keyboard accelerator called a mnemonic.
385    ///
386    /// Pressing Alt and that key should activate the button.
387    ///
388    /// # Deprecated since 4.10
389    ///
390    /// Use [`FileDialog`][crate::FileDialog] instead
391    /// ## `cancel_label`
392    /// custom label
393    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
394    #[allow(deprecated)]
395    #[doc(alias = "gtk_file_chooser_native_set_cancel_label")]
396    #[doc(alias = "cancel-label")]
397    pub fn set_cancel_label(&self, cancel_label: Option<&str>) {
398        unsafe {
399            ffi::gtk_file_chooser_native_set_cancel_label(
400                self.to_glib_none().0,
401                cancel_label.to_glib_none().0,
402            );
403        }
404    }
405
406    #[doc(alias = "accept-label")]
407    pub fn connect_accept_label_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
408        unsafe extern "C" fn notify_accept_label_trampoline<F: Fn(&FileChooserNative) + 'static>(
409            this: *mut ffi::GtkFileChooserNative,
410            _param_spec: glib::ffi::gpointer,
411            f: glib::ffi::gpointer,
412        ) {
413            unsafe {
414                let f: &F = &*(f as *const F);
415                f(&from_glib_borrow(this))
416            }
417        }
418        unsafe {
419            let f: Box_<F> = Box_::new(f);
420            connect_raw(
421                self.as_ptr() as *mut _,
422                c"notify::accept-label".as_ptr(),
423                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
424                    notify_accept_label_trampoline::<F> as *const (),
425                )),
426                Box_::into_raw(f),
427            )
428        }
429    }
430
431    #[doc(alias = "cancel-label")]
432    pub fn connect_cancel_label_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
433        unsafe extern "C" fn notify_cancel_label_trampoline<F: Fn(&FileChooserNative) + 'static>(
434            this: *mut ffi::GtkFileChooserNative,
435            _param_spec: glib::ffi::gpointer,
436            f: glib::ffi::gpointer,
437        ) {
438            unsafe {
439                let f: &F = &*(f as *const F);
440                f(&from_glib_borrow(this))
441            }
442        }
443        unsafe {
444            let f: Box_<F> = Box_::new(f);
445            connect_raw(
446                self.as_ptr() as *mut _,
447                c"notify::cancel-label".as_ptr(),
448                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
449                    notify_cancel_label_trampoline::<F> as *const (),
450                )),
451                Box_::into_raw(f),
452            )
453        }
454    }
455}
456
457impl Default for FileChooserNative {
458    fn default() -> Self {
459        glib::object::Object::new::<Self>()
460    }
461}
462
463// rustdoc-stripper-ignore-next
464/// A [builder-pattern] type to construct [`FileChooserNative`] objects.
465///
466/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
467#[must_use = "The builder must be built to be used"]
468pub struct FileChooserNativeBuilder {
469    builder: glib::object::ObjectBuilder<'static, FileChooserNative>,
470}
471
472impl FileChooserNativeBuilder {
473    fn new() -> Self {
474        Self {
475            builder: glib::object::Object::builder(),
476        }
477    }
478
479    /// The text used for the label on the accept button in the dialog, or
480    /// [`None`] to use the default text.
481    pub fn accept_label(self, accept_label: impl Into<glib::GString>) -> Self {
482        Self {
483            builder: self.builder.property("accept-label", accept_label.into()),
484        }
485    }
486
487    /// The text used for the label on the cancel button in the dialog, or
488    /// [`None`] to use the default text.
489    pub fn cancel_label(self, cancel_label: impl Into<glib::GString>) -> Self {
490        Self {
491            builder: self.builder.property("cancel-label", cancel_label.into()),
492        }
493    }
494
495    /// Whether the window should be modal with respect to its transient parent.
496    pub fn modal(self, modal: bool) -> Self {
497        Self {
498            builder: self.builder.property("modal", modal),
499        }
500    }
501
502    /// The title of the dialog window
503    pub fn title(self, title: impl Into<glib::GString>) -> Self {
504        Self {
505            builder: self.builder.property("title", title.into()),
506        }
507    }
508
509    /// The transient parent of the dialog, or [`None`] for none.
510    pub fn transient_for(self, transient_for: &impl IsA<Window>) -> Self {
511        Self {
512            builder: self
513                .builder
514                .property("transient-for", transient_for.clone().upcast()),
515        }
516    }
517
518    /// Whether the window is currently visible.
519    pub fn visible(self, visible: bool) -> Self {
520        Self {
521            builder: self.builder.property("visible", visible),
522        }
523    }
524
525    /// The type of operation that the file chooser is performing.
526    /// Use [`FileDialog`][crate::FileDialog] instead
527    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
528    pub fn action(self, action: FileChooserAction) -> Self {
529        Self {
530            builder: self.builder.property("action", action),
531        }
532    }
533
534    /// Whether a file chooser not in [`FileChooserAction::Open`][crate::FileChooserAction::Open] mode
535    /// will offer the user to create new folders.
536    /// Use [`FileDialog`][crate::FileDialog] instead
537    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
538    pub fn create_folders(self, create_folders: bool) -> Self {
539        Self {
540            builder: self.builder.property("create-folders", create_folders),
541        }
542    }
543
544    /// The current filter for selecting files that are displayed.
545    /// Use [`FileDialog`][crate::FileDialog] instead
546    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
547    pub fn filter(self, filter: &FileFilter) -> Self {
548        Self {
549            builder: self.builder.property("filter", filter.clone()),
550        }
551    }
552
553    /// Whether to allow multiple files to be selected.
554    /// Use [`FileDialog`][crate::FileDialog] instead
555    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
556    pub fn select_multiple(self, select_multiple: bool) -> Self {
557        Self {
558            builder: self.builder.property("select-multiple", select_multiple),
559        }
560    }
561
562    // rustdoc-stripper-ignore-next
563    /// Build the [`FileChooserNative`].
564    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
565    pub fn build(self) -> FileChooserNative {
566        assert_initialized_main_thread!();
567        self.builder.build()
568    }
569}