gtk/auto/file_filter.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::{Buildable, FileFilterFlags, FileFilterInfo, ffi};
6use glib::translate::*;
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10 /// A GtkFileFilter can be used to restrict the files being shown in a
11 /// [`FileChooser`][crate::FileChooser]. Files can be filtered based on their name (with
12 /// [`add_pattern()`][Self::add_pattern()]), on their mime type (with
13 /// [`add_mime_type()`][Self::add_mime_type()]), or by a custom filter function
14 /// (with [`add_custom()`][Self::add_custom()]).
15 ///
16 /// Filtering by mime types handles aliasing and subclassing of mime
17 /// types; e.g. a filter for text/plain also matches a file with mime
18 /// type application/rtf, since application/rtf is a subclass of
19 /// text/plain. Note that [`FileFilter`][crate::FileFilter] allows wildcards for the
20 /// subtype of a mime type, so you can e.g. filter for image/\*.
21 ///
22 /// Normally, filters are used by adding them to a [`FileChooser`][crate::FileChooser],
23 /// see [`FileChooserExt::add_filter()`][crate::prelude::FileChooserExt::add_filter()], but it is also possible
24 /// to manually use a filter on a file with [`filter()`][Self::filter()].
25 ///
26 /// # GtkFileFilter as GtkBuildable
27 ///
28 /// The GtkFileFilter implementation of the GtkBuildable interface
29 /// supports adding rules using the ``<mime-types>``, ``<patterns>`` and
30 /// ``<applications>`` elements and listing the rules within. Specifying
31 /// a ``<mime-type>`` or ``<pattern>`` has the same effect as as calling
32 /// [`add_mime_type()`][Self::add_mime_type()] or [`add_pattern()`][Self::add_pattern()].
33 ///
34 /// An example of a UI definition fragment specifying GtkFileFilter
35 /// rules:
36 ///
37 ///
38 ///
39 /// **⚠️ The following code is in xml ⚠️**
40 ///
41 /// ```xml
42 /// <object class="GtkFileFilter">
43 /// <mime-types>
44 /// <mime-type>text/plain</mime-type>
45 /// <mime-type>image/ *</mime-type>
46 /// </mime-types>
47 /// <patterns>
48 /// <pattern>*.txt</pattern>
49 /// <pattern>*.png</pattern>
50 /// </patterns>
51 /// </object>
52 /// ```
53 ///
54 /// # Implements
55 ///
56 /// [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`BuildableExtManual`][trait@crate::prelude::BuildableExtManual]
57 #[doc(alias = "GtkFileFilter")]
58 pub struct FileFilter(Object<ffi::GtkFileFilter>) @implements Buildable;
59
60 match fn {
61 type_ => || ffi::gtk_file_filter_get_type(),
62 }
63}
64
65impl FileFilter {
66 /// Creates a new [`FileFilter`][crate::FileFilter] with no rules added to it.
67 /// Such a filter doesn’t accept any files, so is not
68 /// particularly useful until you add rules with
69 /// [`add_mime_type()`][Self::add_mime_type()], [`add_pattern()`][Self::add_pattern()],
70 /// or [`add_custom()`][Self::add_custom()]. To create a filter
71 /// that accepts any file, use:
72 ///
73 ///
74 /// **⚠️ The following code is in C ⚠️**
75 ///
76 /// ```C
77 /// GtkFileFilter *filter = gtk_file_filter_new ();
78 /// gtk_file_filter_add_pattern (filter, "*");
79 /// ```
80 ///
81 /// # Returns
82 ///
83 /// a new [`FileFilter`][crate::FileFilter]
84 #[doc(alias = "gtk_file_filter_new")]
85 pub fn new() -> FileFilter {
86 assert_initialized_main_thread!();
87 unsafe { from_glib_none(ffi::gtk_file_filter_new()) }
88 }
89
90 /// Deserialize a file filter from an a{sv} variant in
91 /// the format produced by [`to_gvariant()`][Self::to_gvariant()].
92 /// ## `variant`
93 /// an a{sv} [`glib::Variant`][struct@crate::glib::Variant]
94 ///
95 /// # Returns
96 ///
97 /// a new [`FileFilter`][crate::FileFilter] object
98 #[doc(alias = "gtk_file_filter_new_from_gvariant")]
99 #[doc(alias = "new_from_gvariant")]
100 pub fn from_gvariant(variant: &glib::Variant) -> FileFilter {
101 assert_initialized_main_thread!();
102 unsafe {
103 from_glib_full(ffi::gtk_file_filter_new_from_gvariant(
104 variant.to_glib_none().0,
105 ))
106 }
107 }
108
109 /// Adds rule to a filter that allows files based on a custom callback
110 /// function. The bitfield `needed` which is passed in provides information
111 /// about what sorts of information that the filter function needs;
112 /// this allows GTK+ to avoid retrieving expensive information when
113 /// it isn’t needed by the filter.
114 /// ## `needed`
115 /// bitfield of flags indicating the information that the custom
116 /// filter function needs.
117 /// ## `func`
118 /// callback function; if the function returns [`true`], then
119 /// the file will be displayed.
120 /// ## `notify`
121 /// function to call to free `data` when it is no longer needed.
122 #[doc(alias = "gtk_file_filter_add_custom")]
123 pub fn add_custom<P: Fn(&FileFilterInfo) -> bool + 'static>(
124 &self,
125 needed: FileFilterFlags,
126 func: P,
127 ) {
128 let func_data: Box_<P> = Box_::new(func);
129 unsafe extern "C" fn func_func<P: Fn(&FileFilterInfo) -> bool + 'static>(
130 filter_info: *const ffi::GtkFileFilterInfo,
131 data: glib::ffi::gpointer,
132 ) -> glib::ffi::gboolean {
133 unsafe {
134 let filter_info = from_glib_borrow(filter_info);
135 let callback = &*(data as *mut P);
136 (*callback)(&filter_info).into_glib()
137 }
138 }
139 let func = Some(func_func::<P> as _);
140 unsafe extern "C" fn notify_func<P: Fn(&FileFilterInfo) -> bool + 'static>(
141 data: glib::ffi::gpointer,
142 ) {
143 unsafe {
144 let _callback = Box_::from_raw(data as *mut P);
145 }
146 }
147 let destroy_call4 = Some(notify_func::<P> as _);
148 let super_callback0: Box_<P> = func_data;
149 unsafe {
150 ffi::gtk_file_filter_add_custom(
151 self.to_glib_none().0,
152 needed.into_glib(),
153 func,
154 Box_::into_raw(super_callback0) as *mut _,
155 destroy_call4,
156 );
157 }
158 }
159
160 /// Adds a rule allowing a given mime type to `self`.
161 /// ## `mime_type`
162 /// name of a MIME type
163 #[doc(alias = "gtk_file_filter_add_mime_type")]
164 pub fn add_mime_type(&self, mime_type: &str) {
165 unsafe {
166 ffi::gtk_file_filter_add_mime_type(self.to_glib_none().0, mime_type.to_glib_none().0);
167 }
168 }
169
170 /// Adds a rule allowing a shell style glob to a filter.
171 /// ## `pattern`
172 /// a shell style glob
173 #[doc(alias = "gtk_file_filter_add_pattern")]
174 pub fn add_pattern(&self, pattern: &str) {
175 unsafe {
176 ffi::gtk_file_filter_add_pattern(self.to_glib_none().0, pattern.to_glib_none().0);
177 }
178 }
179
180 /// Adds a rule allowing image files in the formats supported
181 /// by GdkPixbuf.
182 #[doc(alias = "gtk_file_filter_add_pixbuf_formats")]
183 pub fn add_pixbuf_formats(&self) {
184 unsafe {
185 ffi::gtk_file_filter_add_pixbuf_formats(self.to_glib_none().0);
186 }
187 }
188
189 /// Tests whether a file should be displayed according to `self`.
190 /// The [`FileFilterInfo`][crate::FileFilterInfo] `filter_info` should include
191 /// the fields returned from [`needed()`][Self::needed()].
192 ///
193 /// This function will not typically be used by applications; it
194 /// is intended principally for use in the implementation of
195 /// [`FileChooser`][crate::FileChooser].
196 /// ## `filter_info`
197 /// a [`FileFilterInfo`][crate::FileFilterInfo] containing information
198 /// about a file.
199 ///
200 /// # Returns
201 ///
202 /// [`true`] if the file should be displayed
203 #[doc(alias = "gtk_file_filter_filter")]
204 pub fn filter(&self, filter_info: &FileFilterInfo) -> bool {
205 unsafe {
206 from_glib(ffi::gtk_file_filter_filter(
207 self.to_glib_none().0,
208 filter_info.to_glib_none().0,
209 ))
210 }
211 }
212
213 /// Gets the human-readable name for the filter. See [`set_name()`][Self::set_name()].
214 ///
215 /// # Returns
216 ///
217 /// The human-readable name of the filter,
218 /// or [`None`]. This value is owned by GTK+ and must not
219 /// be modified or freed.
220 #[doc(alias = "gtk_file_filter_get_name")]
221 #[doc(alias = "get_name")]
222 pub fn name(&self) -> Option<glib::GString> {
223 unsafe { from_glib_none(ffi::gtk_file_filter_get_name(self.to_glib_none().0)) }
224 }
225
226 /// Gets the fields that need to be filled in for the [`FileFilterInfo`][crate::FileFilterInfo]
227 /// passed to [`filter()`][Self::filter()]
228 ///
229 /// This function will not typically be used by applications; it
230 /// is intended principally for use in the implementation of
231 /// [`FileChooser`][crate::FileChooser].
232 ///
233 /// # Returns
234 ///
235 /// bitfield of flags indicating needed fields when
236 /// calling [`filter()`][Self::filter()]
237 #[doc(alias = "gtk_file_filter_get_needed")]
238 #[doc(alias = "get_needed")]
239 pub fn needed(&self) -> FileFilterFlags {
240 unsafe { from_glib(ffi::gtk_file_filter_get_needed(self.to_glib_none().0)) }
241 }
242
243 /// Sets the human-readable name of the filter; this is the string
244 /// that will be displayed in the file selector user interface if
245 /// there is a selectable list of filters.
246 /// ## `name`
247 /// the human-readable-name for the filter, or [`None`]
248 /// to remove any existing name.
249 #[doc(alias = "gtk_file_filter_set_name")]
250 pub fn set_name(&self, name: Option<&str>) {
251 unsafe {
252 ffi::gtk_file_filter_set_name(self.to_glib_none().0, name.to_glib_none().0);
253 }
254 }
255
256 /// Serialize a file filter to an a{sv} variant.
257 ///
258 /// # Returns
259 ///
260 /// a new, floating, [`glib::Variant`][struct@crate::glib::Variant]
261 #[doc(alias = "gtk_file_filter_to_gvariant")]
262 pub fn to_gvariant(&self) -> Option<glib::Variant> {
263 unsafe { from_glib_none(ffi::gtk_file_filter_to_gvariant(self.to_glib_none().0)) }
264 }
265}
266
267impl Default for FileFilter {
268 fn default() -> Self {
269 Self::new()
270 }
271}