Skip to main content

gtk4/auto/
string_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::{Expression, Filter, StringFilterMatchMode, ffi};
6use glib::{
7    prelude::*,
8    signal::{SignalHandlerId, connect_raw},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// Determines whether to include items by comparing strings to a fixed search term.
15    ///
16    /// The strings are obtained from the items by evaluating an expression
17    /// set with [`set_expression()`][Self::set_expression()], and they are
18    /// compared against a search term set with [`set_search()`][Self::set_search()].
19    ///
20    /// [`StringFilter`][crate::StringFilter] has several different modes of comparison - it
21    /// can match the whole string, just a prefix, or any substring. Use
22    /// [`set_match_mode()`][Self::set_match_mode()] choose a mode.
23    ///
24    /// It is also possible to make case-insensitive comparisons, with
25    /// [`set_ignore_case()`][Self::set_ignore_case()].
26    ///
27    /// ## Properties
28    ///
29    ///
30    /// #### `expression`
31    ///  The expression to evaluate on each item to get a
32    /// string to compare with.
33    ///
34    /// Readable | Writeable
35    ///
36    ///
37    /// #### `ignore-case`
38    ///  If matching is case sensitive.
39    ///
40    /// Readable | Writeable
41    ///
42    ///
43    /// #### `match-mode`
44    ///  If exact matches are necessary or if substrings are allowed.
45    ///
46    /// Readable | Writeable
47    ///
48    ///
49    /// #### `search`
50    ///  The search term.
51    ///
52    /// Readable | Writeable
53    ///
54    /// # Implements
55    ///
56    /// [`FilterExt`][trait@crate::prelude::FilterExt], [`trait@glib::ObjectExt`]
57    #[doc(alias = "GtkStringFilter")]
58    pub struct StringFilter(Object<ffi::GtkStringFilter, ffi::GtkStringFilterClass>) @extends Filter;
59
60    match fn {
61        type_ => || ffi::gtk_string_filter_get_type(),
62    }
63}
64
65impl StringFilter {
66    /// Creates a new string filter.
67    ///
68    /// You will want to set up the filter by providing a string to search for
69    /// and by providing a property to look up on the item.
70    /// ## `expression`
71    /// the expression to evaluate
72    ///
73    /// # Returns
74    ///
75    /// a new [`StringFilter`][crate::StringFilter]
76    #[doc(alias = "gtk_string_filter_new")]
77    pub fn new(expression: Option<impl AsRef<Expression>>) -> StringFilter {
78        assert_initialized_main_thread!();
79        unsafe {
80            from_glib_full(ffi::gtk_string_filter_new(
81                expression
82                    .map(|p| p.as_ref().clone().upcast())
83                    .into_glib_ptr(),
84            ))
85        }
86    }
87
88    // rustdoc-stripper-ignore-next
89    /// Creates a new builder-pattern struct instance to construct [`StringFilter`] objects.
90    ///
91    /// This method returns an instance of [`StringFilterBuilder`](crate::builders::StringFilterBuilder) which can be used to create [`StringFilter`] objects.
92    pub fn builder() -> StringFilterBuilder {
93        StringFilterBuilder::new()
94    }
95
96    /// Gets the expression that the string filter uses to
97    /// obtain strings from items.
98    ///
99    /// # Returns
100    ///
101    /// the expression
102    #[doc(alias = "gtk_string_filter_get_expression")]
103    #[doc(alias = "get_expression")]
104    pub fn expression(&self) -> Option<Expression> {
105        unsafe { from_glib_none(ffi::gtk_string_filter_get_expression(self.to_glib_none().0)) }
106    }
107
108    /// Returns whether the filter ignores case differences.
109    ///
110    /// # Returns
111    ///
112    /// true if the filter ignores case
113    #[doc(alias = "gtk_string_filter_get_ignore_case")]
114    #[doc(alias = "get_ignore_case")]
115    #[doc(alias = "ignore-case")]
116    pub fn ignores_case(&self) -> bool {
117        unsafe {
118            from_glib(ffi::gtk_string_filter_get_ignore_case(
119                self.to_glib_none().0,
120            ))
121        }
122    }
123
124    /// Returns the match mode that the filter is using.
125    ///
126    /// # Returns
127    ///
128    /// the match mode of the filter
129    #[doc(alias = "gtk_string_filter_get_match_mode")]
130    #[doc(alias = "get_match_mode")]
131    #[doc(alias = "match-mode")]
132    pub fn match_mode(&self) -> StringFilterMatchMode {
133        unsafe { from_glib(ffi::gtk_string_filter_get_match_mode(self.to_glib_none().0)) }
134    }
135
136    /// Gets the search term.
137    ///
138    /// # Returns
139    ///
140    /// the search term
141    #[doc(alias = "gtk_string_filter_get_search")]
142    #[doc(alias = "get_search")]
143    pub fn search(&self) -> Option<glib::GString> {
144        unsafe { from_glib_none(ffi::gtk_string_filter_get_search(self.to_glib_none().0)) }
145    }
146
147    /// Sets the expression that the string filter uses to
148    /// obtain strings from items.
149    ///
150    /// The expression must have a value type of `G_TYPE_STRING`.
151    /// ## `expression`
152    /// the expression
153    #[doc(alias = "gtk_string_filter_set_expression")]
154    #[doc(alias = "expression")]
155    pub fn set_expression(&self, expression: Option<impl AsRef<Expression>>) {
156        unsafe {
157            ffi::gtk_string_filter_set_expression(
158                self.to_glib_none().0,
159                expression.as_ref().map(|p| p.as_ref()).to_glib_none().0,
160            );
161        }
162    }
163
164    /// Sets whether the filter ignores case differences.
165    /// ## `ignore_case`
166    /// true to ignore case
167    #[doc(alias = "gtk_string_filter_set_ignore_case")]
168    #[doc(alias = "ignore-case")]
169    pub fn set_ignore_case(&self, ignore_case: bool) {
170        unsafe {
171            ffi::gtk_string_filter_set_ignore_case(self.to_glib_none().0, ignore_case.into_glib());
172        }
173    }
174
175    /// Sets the match mode for the filter.
176    /// ## `mode`
177    /// the new match mode
178    #[doc(alias = "gtk_string_filter_set_match_mode")]
179    #[doc(alias = "match-mode")]
180    pub fn set_match_mode(&self, mode: StringFilterMatchMode) {
181        unsafe {
182            ffi::gtk_string_filter_set_match_mode(self.to_glib_none().0, mode.into_glib());
183        }
184    }
185
186    /// Sets the string to search for.
187    /// ## `search`
188    /// the string to search for
189    #[doc(alias = "gtk_string_filter_set_search")]
190    #[doc(alias = "search")]
191    pub fn set_search(&self, search: Option<&str>) {
192        unsafe {
193            ffi::gtk_string_filter_set_search(self.to_glib_none().0, search.to_glib_none().0);
194        }
195    }
196
197    #[doc(alias = "expression")]
198    pub fn connect_expression_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
199        unsafe extern "C" fn notify_expression_trampoline<F: Fn(&StringFilter) + 'static>(
200            this: *mut ffi::GtkStringFilter,
201            _param_spec: glib::ffi::gpointer,
202            f: glib::ffi::gpointer,
203        ) {
204            unsafe {
205                let f: &F = &*(f as *const F);
206                f(&from_glib_borrow(this))
207            }
208        }
209        unsafe {
210            let f: Box_<F> = Box_::new(f);
211            connect_raw(
212                self.as_ptr() as *mut _,
213                c"notify::expression".as_ptr() as *const _,
214                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
215                    notify_expression_trampoline::<F> as *const (),
216                )),
217                Box_::into_raw(f),
218            )
219        }
220    }
221
222    #[doc(alias = "ignore-case")]
223    pub fn connect_ignore_case_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
224        unsafe extern "C" fn notify_ignore_case_trampoline<F: Fn(&StringFilter) + 'static>(
225            this: *mut ffi::GtkStringFilter,
226            _param_spec: glib::ffi::gpointer,
227            f: glib::ffi::gpointer,
228        ) {
229            unsafe {
230                let f: &F = &*(f as *const F);
231                f(&from_glib_borrow(this))
232            }
233        }
234        unsafe {
235            let f: Box_<F> = Box_::new(f);
236            connect_raw(
237                self.as_ptr() as *mut _,
238                c"notify::ignore-case".as_ptr() as *const _,
239                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
240                    notify_ignore_case_trampoline::<F> as *const (),
241                )),
242                Box_::into_raw(f),
243            )
244        }
245    }
246
247    #[doc(alias = "match-mode")]
248    pub fn connect_match_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
249        unsafe extern "C" fn notify_match_mode_trampoline<F: Fn(&StringFilter) + 'static>(
250            this: *mut ffi::GtkStringFilter,
251            _param_spec: glib::ffi::gpointer,
252            f: glib::ffi::gpointer,
253        ) {
254            unsafe {
255                let f: &F = &*(f as *const F);
256                f(&from_glib_borrow(this))
257            }
258        }
259        unsafe {
260            let f: Box_<F> = Box_::new(f);
261            connect_raw(
262                self.as_ptr() as *mut _,
263                c"notify::match-mode".as_ptr() as *const _,
264                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
265                    notify_match_mode_trampoline::<F> as *const (),
266                )),
267                Box_::into_raw(f),
268            )
269        }
270    }
271
272    #[doc(alias = "search")]
273    pub fn connect_search_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
274        unsafe extern "C" fn notify_search_trampoline<F: Fn(&StringFilter) + 'static>(
275            this: *mut ffi::GtkStringFilter,
276            _param_spec: glib::ffi::gpointer,
277            f: glib::ffi::gpointer,
278        ) {
279            unsafe {
280                let f: &F = &*(f as *const F);
281                f(&from_glib_borrow(this))
282            }
283        }
284        unsafe {
285            let f: Box_<F> = Box_::new(f);
286            connect_raw(
287                self.as_ptr() as *mut _,
288                c"notify::search".as_ptr() as *const _,
289                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
290                    notify_search_trampoline::<F> as *const (),
291                )),
292                Box_::into_raw(f),
293            )
294        }
295    }
296}
297
298impl Default for StringFilter {
299    fn default() -> Self {
300        glib::object::Object::new::<Self>()
301    }
302}
303
304// rustdoc-stripper-ignore-next
305/// A [builder-pattern] type to construct [`StringFilter`] objects.
306///
307/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
308#[must_use = "The builder must be built to be used"]
309pub struct StringFilterBuilder {
310    builder: glib::object::ObjectBuilder<'static, StringFilter>,
311}
312
313impl StringFilterBuilder {
314    fn new() -> Self {
315        Self {
316            builder: glib::object::Object::builder(),
317        }
318    }
319
320    /// The expression to evaluate on each item to get a
321    /// string to compare with.
322    pub fn expression(self, expression: impl AsRef<Expression>) -> Self {
323        Self {
324            builder: self
325                .builder
326                .property("expression", expression.as_ref().clone()),
327        }
328    }
329
330    /// If matching is case sensitive.
331    pub fn ignore_case(self, ignore_case: bool) -> Self {
332        Self {
333            builder: self.builder.property("ignore-case", ignore_case),
334        }
335    }
336
337    /// If exact matches are necessary or if substrings are allowed.
338    pub fn match_mode(self, match_mode: StringFilterMatchMode) -> Self {
339        Self {
340            builder: self.builder.property("match-mode", match_mode),
341        }
342    }
343
344    /// The search term.
345    pub fn search(self, search: impl Into<glib::GString>) -> Self {
346        Self {
347            builder: self.builder.property("search", search.into()),
348        }
349    }
350
351    // rustdoc-stripper-ignore-next
352    /// Build the [`StringFilter`].
353    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
354    pub fn build(self) -> StringFilter {
355        assert_initialized_main_thread!();
356        self.builder.build()
357    }
358}