gtk4/auto/
string_sorter.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
5#[cfg(feature = "v4_10")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
7use crate::Collation;
8use crate::{ffi, Expression, Sorter};
9use glib::{
10    prelude::*,
11    signal::{connect_raw, SignalHandlerId},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16glib::wrapper! {
17    /// [`StringSorter`][crate::StringSorter] is a [`Sorter`][crate::Sorter] that compares strings.
18    ///
19    /// It does the comparison in a linguistically correct way using the
20    /// current locale by normalizing Unicode strings and possibly case-folding
21    /// them before performing the comparison.
22    ///
23    /// To obtain the strings to compare, this sorter evaluates a
24    /// [`Expression`][crate::Expression].
25    ///
26    /// ## Properties
27    ///
28    ///
29    /// #### `collation`
30    ///  The collation method to use for sorting.
31    ///
32    /// The `GTK_COLLATION_NONE` value is useful when the expression already
33    /// returns collation keys, or strings that need to be compared byte-by-byte.
34    ///
35    /// The default value, `GTK_COLLATION_UNICODE`, compares strings according
36    /// to the [Unicode collation algorithm](https://www.unicode.org/reports/tr10/).
37    ///
38    /// Readable | Writeable
39    ///
40    ///
41    /// #### `expression`
42    ///  The expression to evaluate on item to get a string to compare with.
43    ///
44    /// Readable | Writeable
45    ///
46    ///
47    /// #### `ignore-case`
48    ///  If sorting is case sensitive.
49    ///
50    /// Readable | Writeable
51    ///
52    /// # Implements
53    ///
54    /// [`SorterExt`][trait@crate::prelude::SorterExt], [`trait@glib::ObjectExt`]
55    #[doc(alias = "GtkStringSorter")]
56    pub struct StringSorter(Object<ffi::GtkStringSorter, ffi::GtkStringSorterClass>) @extends Sorter;
57
58    match fn {
59        type_ => || ffi::gtk_string_sorter_get_type(),
60    }
61}
62
63impl StringSorter {
64    /// Creates a new string sorter that compares items using the given
65    /// @expression.
66    ///
67    /// Unless an expression is set on it, this sorter will always
68    /// compare items as invalid.
69    /// ## `expression`
70    /// The expression to evaluate
71    ///
72    /// # Returns
73    ///
74    /// a new [`StringSorter`][crate::StringSorter]
75    #[doc(alias = "gtk_string_sorter_new")]
76    pub fn new(expression: Option<impl AsRef<Expression>>) -> StringSorter {
77        assert_initialized_main_thread!();
78        unsafe {
79            from_glib_full(ffi::gtk_string_sorter_new(
80                expression
81                    .map(|p| p.as_ref().clone().upcast())
82                    .into_glib_ptr(),
83            ))
84        }
85    }
86
87    // rustdoc-stripper-ignore-next
88    /// Creates a new builder-pattern struct instance to construct [`StringSorter`] objects.
89    ///
90    /// This method returns an instance of [`StringSorterBuilder`](crate::builders::StringSorterBuilder) which can be used to create [`StringSorter`] objects.
91    pub fn builder() -> StringSorterBuilder {
92        StringSorterBuilder::new()
93    }
94
95    /// Gets which collation method the sorter uses.
96    ///
97    /// # Returns
98    ///
99    /// The collation method
100    #[cfg(feature = "v4_10")]
101    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
102    #[doc(alias = "gtk_string_sorter_get_collation")]
103    #[doc(alias = "get_collation")]
104    pub fn collation(&self) -> Collation {
105        unsafe { from_glib(ffi::gtk_string_sorter_get_collation(self.to_glib_none().0)) }
106    }
107
108    /// Gets the expression that is evaluated to obtain strings from items.
109    ///
110    /// # Returns
111    ///
112    /// a [`Expression`][crate::Expression]
113    #[doc(alias = "gtk_string_sorter_get_expression")]
114    #[doc(alias = "get_expression")]
115    pub fn expression(&self) -> Option<Expression> {
116        unsafe { from_glib_none(ffi::gtk_string_sorter_get_expression(self.to_glib_none().0)) }
117    }
118
119    /// Gets whether the sorter ignores case differences.
120    ///
121    /// # Returns
122    ///
123    /// [`true`] if @self is ignoring case differences
124    #[doc(alias = "gtk_string_sorter_get_ignore_case")]
125    #[doc(alias = "get_ignore_case")]
126    #[doc(alias = "ignore-case")]
127    pub fn ignores_case(&self) -> bool {
128        unsafe {
129            from_glib(ffi::gtk_string_sorter_get_ignore_case(
130                self.to_glib_none().0,
131            ))
132        }
133    }
134
135    /// Sets the collation method to use for sorting.
136    /// ## `collation`
137    /// the collation method
138    #[cfg(feature = "v4_10")]
139    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
140    #[doc(alias = "gtk_string_sorter_set_collation")]
141    #[doc(alias = "collation")]
142    pub fn set_collation(&self, collation: Collation) {
143        unsafe {
144            ffi::gtk_string_sorter_set_collation(self.to_glib_none().0, collation.into_glib());
145        }
146    }
147
148    /// Sets the expression that is evaluated to obtain strings from items.
149    ///
150    /// The expression must have the type `G_TYPE_STRING`.
151    /// ## `expression`
152    /// a [`Expression`][crate::Expression]
153    #[doc(alias = "gtk_string_sorter_set_expression")]
154    #[doc(alias = "expression")]
155    pub fn set_expression(&self, expression: Option<impl AsRef<Expression>>) {
156        unsafe {
157            ffi::gtk_string_sorter_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 sorter will ignore case differences.
165    /// ## `ignore_case`
166    /// [`true`] to ignore case differences
167    #[doc(alias = "gtk_string_sorter_set_ignore_case")]
168    #[doc(alias = "ignore-case")]
169    pub fn set_ignore_case(&self, ignore_case: bool) {
170        unsafe {
171            ffi::gtk_string_sorter_set_ignore_case(self.to_glib_none().0, ignore_case.into_glib());
172        }
173    }
174
175    #[cfg(feature = "v4_10")]
176    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
177    #[doc(alias = "collation")]
178    pub fn connect_collation_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
179        unsafe extern "C" fn notify_collation_trampoline<F: Fn(&StringSorter) + 'static>(
180            this: *mut ffi::GtkStringSorter,
181            _param_spec: glib::ffi::gpointer,
182            f: glib::ffi::gpointer,
183        ) {
184            let f: &F = &*(f as *const F);
185            f(&from_glib_borrow(this))
186        }
187        unsafe {
188            let f: Box_<F> = Box_::new(f);
189            connect_raw(
190                self.as_ptr() as *mut _,
191                b"notify::collation\0".as_ptr() as *const _,
192                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
193                    notify_collation_trampoline::<F> as *const (),
194                )),
195                Box_::into_raw(f),
196            )
197        }
198    }
199
200    #[doc(alias = "expression")]
201    pub fn connect_expression_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
202        unsafe extern "C" fn notify_expression_trampoline<F: Fn(&StringSorter) + 'static>(
203            this: *mut ffi::GtkStringSorter,
204            _param_spec: glib::ffi::gpointer,
205            f: glib::ffi::gpointer,
206        ) {
207            let f: &F = &*(f as *const F);
208            f(&from_glib_borrow(this))
209        }
210        unsafe {
211            let f: Box_<F> = Box_::new(f);
212            connect_raw(
213                self.as_ptr() as *mut _,
214                b"notify::expression\0".as_ptr() as *const _,
215                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
216                    notify_expression_trampoline::<F> as *const (),
217                )),
218                Box_::into_raw(f),
219            )
220        }
221    }
222
223    #[doc(alias = "ignore-case")]
224    pub fn connect_ignore_case_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
225        unsafe extern "C" fn notify_ignore_case_trampoline<F: Fn(&StringSorter) + 'static>(
226            this: *mut ffi::GtkStringSorter,
227            _param_spec: glib::ffi::gpointer,
228            f: glib::ffi::gpointer,
229        ) {
230            let f: &F = &*(f as *const F);
231            f(&from_glib_borrow(this))
232        }
233        unsafe {
234            let f: Box_<F> = Box_::new(f);
235            connect_raw(
236                self.as_ptr() as *mut _,
237                b"notify::ignore-case\0".as_ptr() as *const _,
238                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
239                    notify_ignore_case_trampoline::<F> as *const (),
240                )),
241                Box_::into_raw(f),
242            )
243        }
244    }
245}
246
247impl Default for StringSorter {
248    fn default() -> Self {
249        glib::object::Object::new::<Self>()
250    }
251}
252
253// rustdoc-stripper-ignore-next
254/// A [builder-pattern] type to construct [`StringSorter`] objects.
255///
256/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
257#[must_use = "The builder must be built to be used"]
258pub struct StringSorterBuilder {
259    builder: glib::object::ObjectBuilder<'static, StringSorter>,
260}
261
262impl StringSorterBuilder {
263    fn new() -> Self {
264        Self {
265            builder: glib::object::Object::builder(),
266        }
267    }
268
269    /// The collation method to use for sorting.
270    ///
271    /// The `GTK_COLLATION_NONE` value is useful when the expression already
272    /// returns collation keys, or strings that need to be compared byte-by-byte.
273    ///
274    /// The default value, `GTK_COLLATION_UNICODE`, compares strings according
275    /// to the [Unicode collation algorithm](https://www.unicode.org/reports/tr10/).
276    #[cfg(feature = "v4_10")]
277    #[cfg_attr(docsrs, doc(cfg(feature = "v4_10")))]
278    pub fn collation(self, collation: Collation) -> Self {
279        Self {
280            builder: self.builder.property("collation", collation),
281        }
282    }
283
284    /// The expression to evaluate on item to get a string to compare with.
285    pub fn expression(self, expression: impl AsRef<Expression>) -> Self {
286        Self {
287            builder: self
288                .builder
289                .property("expression", expression.as_ref().clone()),
290        }
291    }
292
293    /// If sorting is case sensitive.
294    pub fn ignore_case(self, ignore_case: bool) -> Self {
295        Self {
296            builder: self.builder.property("ignore-case", ignore_case),
297        }
298    }
299
300    // rustdoc-stripper-ignore-next
301    /// Build the [`StringSorter`].
302    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
303    pub fn build(self) -> StringSorter {
304        assert_initialized_main_thread!();
305        self.builder.build()
306    }
307}