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 /// [`Sorter`][crate::Sorter] is an object to describe sorting criteria.
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
68mod sealed {
69 pub trait Sealed {}
70 impl<T: super::IsA<super::Sorter>> Sealed for T {}
71}
72
73/// Trait containing all [`struct@Sorter`] methods.
74///
75/// # Implementors
76///
77/// [`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]
78pub trait SorterExt: IsA<Sorter> + sealed::Sealed + 'static {
79 /// Notifies all users of the sorter that it has changed.
80 ///
81 /// This emits the [`changed`][struct@crate::Sorter#changed] signal. Users
82 /// of the sorter should then update the sort order via
83 /// `Gtk::Sorter::compare()`.
84 ///
85 /// Depending on the @change parameter, it may be possible to
86 /// update the sort order without a full resorting. Refer to
87 /// the [`SorterChange`][crate::SorterChange] documentation for details.
88 ///
89 /// This function is intended for implementers of [`Sorter`][crate::Sorter]
90 /// subclasses and should not be called from other functions.
91 /// ## `change`
92 /// How the sorter changed
93 #[doc(alias = "gtk_sorter_changed")]
94 fn changed(&self, change: SorterChange) {
95 unsafe {
96 ffi::gtk_sorter_changed(self.as_ref().to_glib_none().0, change.into_glib());
97 }
98 }
99
100 #[doc(alias = "gtk_sorter_compare")]
101 fn compare(&self, item1: &impl IsA<glib::Object>, item2: &impl IsA<glib::Object>) -> Ordering {
102 unsafe {
103 from_glib(ffi::gtk_sorter_compare(
104 self.as_ref().to_glib_none().0,
105 item1.as_ref().to_glib_none().0,
106 item2.as_ref().to_glib_none().0,
107 ))
108 }
109 }
110
111 /// Gets the order that @self conforms to.
112 ///
113 /// See [`SorterOrder`][crate::SorterOrder] for details
114 /// of the possible return values.
115 ///
116 /// This function is intended to allow optimizations.
117 ///
118 /// # Returns
119 ///
120 /// The order
121 #[doc(alias = "gtk_sorter_get_order")]
122 #[doc(alias = "get_order")]
123 fn order(&self) -> SorterOrder {
124 unsafe { from_glib(ffi::gtk_sorter_get_order(self.as_ref().to_glib_none().0)) }
125 }
126
127 /// Emitted whenever the sorter changed.
128 ///
129 /// Users of the sorter should then update the sort order
130 /// again via gtk_sorter_compare().
131 ///
132 /// [`SortListModel`][crate::SortListModel] handles this signal automatically.
133 ///
134 /// Depending on the @change parameter, it may be possible to update
135 /// the sort order without a full resorting. Refer to the
136 /// [`SorterChange`][crate::SorterChange] documentation for details.
137 /// ## `change`
138 /// how the sorter changed
139 #[doc(alias = "changed")]
140 fn connect_changed<F: Fn(&Self, SorterChange) + 'static>(&self, f: F) -> SignalHandlerId {
141 unsafe extern "C" fn changed_trampoline<
142 P: IsA<Sorter>,
143 F: Fn(&P, SorterChange) + 'static,
144 >(
145 this: *mut ffi::GtkSorter,
146 change: ffi::GtkSorterChange,
147 f: glib::ffi::gpointer,
148 ) {
149 let f: &F = &*(f as *const F);
150 f(
151 Sorter::from_glib_borrow(this).unsafe_cast_ref(),
152 from_glib(change),
153 )
154 }
155 unsafe {
156 let f: Box_<F> = Box_::new(f);
157 connect_raw(
158 self.as_ptr() as *mut _,
159 b"changed\0".as_ptr() as *const _,
160 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
161 changed_trampoline::<Self, F> as *const (),
162 )),
163 Box_::into_raw(f),
164 )
165 }
166 }
167}
168
169impl<O: IsA<Sorter>> SorterExt for O {}