gtk4/auto/
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
5use crate::{ffi, Ordering, SorterChange, SorterOrder};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// Describes sorting criteria for a [`SortListModel`][crate::SortListModel].
16    ///
17    /// Its primary user is [`SortListModel`][crate::SortListModel]
18    ///
19    /// The model will use a sorter to determine the order in which
20    /// its items should appear by calling `Gtk::Sorter::compare()`
21    /// for pairs of items.
22    ///
23    /// Sorters may change their sorting behavior through their lifetime.
24    /// In that case, they will emit the [`changed`][struct@crate::Sorter#changed] signal
25    /// to notify that the sort order is no longer valid and should be updated
26    /// by calling gtk_sorter_compare() again.
27    ///
28    /// GTK provides various pre-made sorter implementations for common sorting
29    /// operations. [`ColumnView`][crate::ColumnView] has built-in support for sorting lists
30    /// via the [`sorter`][struct@crate::ColumnViewColumn#sorter] property, where the user can
31    /// change the sorting by clicking on list headers.
32    ///
33    /// Of course, in particular for large lists, it is also possible to subclass
34    /// [`Sorter`][crate::Sorter] and provide one's own sorter.
35    ///
36    /// ## Signals
37    ///
38    ///
39    /// #### `changed`
40    ///  Emitted whenever the sorter changed.
41    ///
42    /// Users of the sorter should then update the sort order
43    /// again via gtk_sorter_compare().
44    ///
45    /// [`SortListModel`][crate::SortListModel] handles this signal automatically.
46    ///
47    /// Depending on the @change parameter, it may be possible to update
48    /// the sort order without a full resorting. Refer to the
49    /// [`SorterChange`][crate::SorterChange] documentation for details.
50    ///
51    ///
52    ///
53    /// # Implements
54    ///
55    /// [`SorterExt`][trait@crate::prelude::SorterExt], [`trait@glib::ObjectExt`]
56    #[doc(alias = "GtkSorter")]
57    pub struct Sorter(Object<ffi::GtkSorter, ffi::GtkSorterClass>);
58
59    match fn {
60        type_ => || ffi::gtk_sorter_get_type(),
61    }
62}
63
64impl Sorter {
65    pub const NONE: Option<&'static Sorter> = None;
66}
67
68/// Trait containing all [`struct@Sorter`] methods.
69///
70/// # Implementors
71///
72/// [`ColumnViewSorter`][struct@crate::ColumnViewSorter], [`CustomSorter`][struct@crate::CustomSorter], [`MultiSorter`][struct@crate::MultiSorter], [`NumericSorter`][struct@crate::NumericSorter], [`Sorter`][struct@crate::Sorter], [`StringSorter`][struct@crate::StringSorter], [`TreeListRowSorter`][struct@crate::TreeListRowSorter]
73pub trait SorterExt: IsA<Sorter> + 'static {
74    /// Notifies all users of the sorter that it has changed.
75    ///
76    /// This emits the [`changed`][struct@crate::Sorter#changed] signal. Users
77    /// of the sorter should then update the sort order via
78    /// `Gtk::Sorter::compare()`.
79    ///
80    /// Depending on the @change parameter, it may be possible to
81    /// update the sort order without a full resorting. Refer to
82    /// the [`SorterChange`][crate::SorterChange] documentation for details.
83    ///
84    /// This function is intended for implementers of [`Sorter`][crate::Sorter]
85    /// subclasses and should not be called from other functions.
86    /// ## `change`
87    /// How the sorter changed
88    #[doc(alias = "gtk_sorter_changed")]
89    fn changed(&self, change: SorterChange) {
90        unsafe {
91            ffi::gtk_sorter_changed(self.as_ref().to_glib_none().0, change.into_glib());
92        }
93    }
94
95    #[doc(alias = "gtk_sorter_compare")]
96    fn compare(&self, item1: &impl IsA<glib::Object>, item2: &impl IsA<glib::Object>) -> Ordering {
97        unsafe {
98            from_glib(ffi::gtk_sorter_compare(
99                self.as_ref().to_glib_none().0,
100                item1.as_ref().to_glib_none().0,
101                item2.as_ref().to_glib_none().0,
102            ))
103        }
104    }
105
106    /// Gets the order that @self conforms to.
107    ///
108    /// See [`SorterOrder`][crate::SorterOrder] for details
109    /// of the possible return values.
110    ///
111    /// This function is intended to allow optimizations.
112    ///
113    /// # Returns
114    ///
115    /// The order
116    #[doc(alias = "gtk_sorter_get_order")]
117    #[doc(alias = "get_order")]
118    fn order(&self) -> SorterOrder {
119        unsafe { from_glib(ffi::gtk_sorter_get_order(self.as_ref().to_glib_none().0)) }
120    }
121
122    /// Emitted whenever the sorter changed.
123    ///
124    /// Users of the sorter should then update the sort order
125    /// again via gtk_sorter_compare().
126    ///
127    /// [`SortListModel`][crate::SortListModel] handles this signal automatically.
128    ///
129    /// Depending on the @change parameter, it may be possible to update
130    /// the sort order without a full resorting. Refer to the
131    /// [`SorterChange`][crate::SorterChange] documentation for details.
132    /// ## `change`
133    /// how the sorter changed
134    #[doc(alias = "changed")]
135    fn connect_changed<F: Fn(&Self, SorterChange) + 'static>(&self, f: F) -> SignalHandlerId {
136        unsafe extern "C" fn changed_trampoline<
137            P: IsA<Sorter>,
138            F: Fn(&P, SorterChange) + 'static,
139        >(
140            this: *mut ffi::GtkSorter,
141            change: ffi::GtkSorterChange,
142            f: glib::ffi::gpointer,
143        ) {
144            let f: &F = &*(f as *const F);
145            f(
146                Sorter::from_glib_borrow(this).unsafe_cast_ref(),
147                from_glib(change),
148            )
149        }
150        unsafe {
151            let f: Box_<F> = Box_::new(f);
152            connect_raw(
153                self.as_ptr() as *mut _,
154                c"changed".as_ptr() as *const _,
155                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
156                    changed_trampoline::<Self, F> as *const (),
157                )),
158                Box_::into_raw(f),
159            )
160        }
161    }
162}
163
164impl<O: IsA<Sorter>> SorterExt for O {}