gtk4/
file_chooser.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ffi, prelude::*, FileChooser};
4use glib::translate::*;
5
6mod sealed {
7    pub trait Sealed {}
8    impl<T: super::IsA<super::FileChooser>> Sealed for T {}
9}
10
11// rustdoc-stripper-ignore-next
12/// Trait containing manually implemented methods of
13/// [`FileChooser`](crate::FileChooser).
14#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
15#[allow(deprecated)]
16pub trait FileChooserExtManual: sealed::Sealed + IsA<FileChooser> + 'static {
17    /// Adds a 'choice' to the file chooser.
18    ///
19    /// This is typically implemented as a combobox or, for boolean choices,
20    /// as a checkbutton. You can select a value using
21    /// [`FileChooserExt::set_choice()`][crate::prelude::FileChooserExt::set_choice()] before the dialog is shown,
22    /// and you can obtain the user-selected value in the
23    /// [`response`][struct@crate::Dialog#response] signal handler using
24    /// [`FileChooserExt::choice()`][crate::prelude::FileChooserExt::choice()].
25    ///
26    /// # Deprecated since 4.10
27    ///
28    /// Use [`FileDialog`][crate::FileDialog] instead
29    /// ## `id`
30    /// id for the added choice
31    /// ## `label`
32    /// user-visible label for the added choice
33    /// ## `options`
34    /// ids for the options of the choice, or [`None`] for a boolean choice
35    /// ## `option_labels`
36    /// user-visible labels for the options, must be the same length as @options
37    #[doc(alias = "gtk_file_chooser_add_choice")]
38    fn add_choice(&self, id: impl IntoGStr, label: impl IntoGStr, options: &[(&str, &str)]) {
39        if options.is_empty() {
40            id.run_with_gstr(|id| {
41                label.run_with_gstr(|label| unsafe {
42                    ffi::gtk_file_chooser_add_choice(
43                        self.as_ref().to_glib_none().0,
44                        id.as_ptr(),
45                        label.as_ptr(),
46                        mut_override(std::ptr::null()),
47                        mut_override(std::ptr::null()),
48                    );
49                });
50            });
51        } else {
52            let stashes_ids = options
53                .iter()
54                .map(|o| o.0.to_glib_none())
55                .collect::<Vec<_>>();
56            let stashes_labels = options
57                .iter()
58                .map(|o| o.1.to_glib_none())
59                .collect::<Vec<_>>();
60            let options_ids = stashes_ids
61                .iter()
62                .map(|o| o.0)
63                .chain(std::iter::once(std::ptr::null()))
64                .collect::<Vec<*const libc::c_char>>();
65            let options_labels = stashes_labels
66                .iter()
67                .map(|o| o.0)
68                .chain(std::iter::once(std::ptr::null()))
69                .collect::<Vec<*const libc::c_char>>();
70            id.run_with_gstr(|id| {
71                label.run_with_gstr(|label| unsafe {
72                    ffi::gtk_file_chooser_add_choice(
73                        self.as_ref().to_glib_none().0,
74                        id.as_ptr(),
75                        label.as_ptr(),
76                        mut_override(options_ids.as_ptr()),
77                        mut_override(options_labels.as_ptr()),
78                    );
79                });
80            });
81        }
82    }
83
84    /// Sets the current folder for @self from a `GFile`.
85    ///
86    /// # Deprecated since 4.10
87    ///
88    /// Use [`FileDialog`][crate::FileDialog] instead
89    /// ## `file`
90    /// the `GFile` for the new folder
91    ///
92    /// # Returns
93    ///
94    /// [`true`] if the folder could be changed successfully, [`false`]
95    ///   otherwise.
96    #[doc(alias = "gtk_file_chooser_set_current_folder")]
97    fn set_current_folder(&self, file: Option<&impl IsA<gio::File>>) -> Result<bool, glib::Error> {
98        unsafe {
99            let mut error = std::ptr::null_mut();
100            let result = from_glib(ffi::gtk_file_chooser_set_current_folder(
101                self.as_ref().to_glib_none().0,
102                file.map(|p| p.as_ref()).to_glib_none().0,
103                &mut error,
104            ));
105            if error.is_null() {
106                Ok(result)
107            } else {
108                Err(from_glib_full(error))
109            }
110        }
111    }
112}
113
114impl<O: IsA<FileChooser>> FileChooserExtManual for O {}