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::{ffi, Expression, Filter, StringFilterMatchMode};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
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            let f: &F = &*(f as *const F);
205            f(&from_glib_borrow(this))
206        }
207        unsafe {
208            let f: Box_<F> = Box_::new(f);
209            connect_raw(
210                self.as_ptr() as *mut _,
211                c"notify::expression".as_ptr() as *const _,
212                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
213                    notify_expression_trampoline::<F> as *const (),
214                )),
215                Box_::into_raw(f),
216            )
217        }
218    }
219
220    #[doc(alias = "ignore-case")]
221    pub fn connect_ignore_case_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
222        unsafe extern "C" fn notify_ignore_case_trampoline<F: Fn(&StringFilter) + 'static>(
223            this: *mut ffi::GtkStringFilter,
224            _param_spec: glib::ffi::gpointer,
225            f: glib::ffi::gpointer,
226        ) {
227            let f: &F = &*(f as *const F);
228            f(&from_glib_borrow(this))
229        }
230        unsafe {
231            let f: Box_<F> = Box_::new(f);
232            connect_raw(
233                self.as_ptr() as *mut _,
234                c"notify::ignore-case".as_ptr() as *const _,
235                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
236                    notify_ignore_case_trampoline::<F> as *const (),
237                )),
238                Box_::into_raw(f),
239            )
240        }
241    }
242
243    #[doc(alias = "match-mode")]
244    pub fn connect_match_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
245        unsafe extern "C" fn notify_match_mode_trampoline<F: Fn(&StringFilter) + 'static>(
246            this: *mut ffi::GtkStringFilter,
247            _param_spec: glib::ffi::gpointer,
248            f: glib::ffi::gpointer,
249        ) {
250            let f: &F = &*(f as *const F);
251            f(&from_glib_borrow(this))
252        }
253        unsafe {
254            let f: Box_<F> = Box_::new(f);
255            connect_raw(
256                self.as_ptr() as *mut _,
257                c"notify::match-mode".as_ptr() as *const _,
258                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
259                    notify_match_mode_trampoline::<F> as *const (),
260                )),
261                Box_::into_raw(f),
262            )
263        }
264    }
265
266    #[doc(alias = "search")]
267    pub fn connect_search_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
268        unsafe extern "C" fn notify_search_trampoline<F: Fn(&StringFilter) + 'static>(
269            this: *mut ffi::GtkStringFilter,
270            _param_spec: glib::ffi::gpointer,
271            f: glib::ffi::gpointer,
272        ) {
273            let f: &F = &*(f as *const F);
274            f(&from_glib_borrow(this))
275        }
276        unsafe {
277            let f: Box_<F> = Box_::new(f);
278            connect_raw(
279                self.as_ptr() as *mut _,
280                c"notify::search".as_ptr() as *const _,
281                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
282                    notify_search_trampoline::<F> as *const (),
283                )),
284                Box_::into_raw(f),
285            )
286        }
287    }
288}
289
290impl Default for StringFilter {
291    fn default() -> Self {
292        glib::object::Object::new::<Self>()
293    }
294}
295
296// rustdoc-stripper-ignore-next
297/// A [builder-pattern] type to construct [`StringFilter`] objects.
298///
299/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
300#[must_use = "The builder must be built to be used"]
301pub struct StringFilterBuilder {
302    builder: glib::object::ObjectBuilder<'static, StringFilter>,
303}
304
305impl StringFilterBuilder {
306    fn new() -> Self {
307        Self {
308            builder: glib::object::Object::builder(),
309        }
310    }
311
312    /// The expression to evaluate on each item to get a
313    /// string to compare with.
314    pub fn expression(self, expression: impl AsRef<Expression>) -> Self {
315        Self {
316            builder: self
317                .builder
318                .property("expression", expression.as_ref().clone()),
319        }
320    }
321
322    /// If matching is case sensitive.
323    pub fn ignore_case(self, ignore_case: bool) -> Self {
324        Self {
325            builder: self.builder.property("ignore-case", ignore_case),
326        }
327    }
328
329    /// If exact matches are necessary or if substrings are allowed.
330    pub fn match_mode(self, match_mode: StringFilterMatchMode) -> Self {
331        Self {
332            builder: self.builder.property("match-mode", match_mode),
333        }
334    }
335
336    /// The search term.
337    pub fn search(self, search: impl Into<glib::GString>) -> Self {
338        Self {
339            builder: self.builder.property("search", search.into()),
340        }
341    }
342
343    // rustdoc-stripper-ignore-next
344    /// Build the [`StringFilter`].
345    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
346    pub fn build(self) -> StringFilter {
347        assert_initialized_main_thread!();
348        self.builder.build()
349    }
350}