1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{Ordering, SorterChange, SorterOrder};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute};

glib::wrapper! {
    /// [`Sorter`][crate::Sorter] is an object to describe sorting criteria.
    ///
    /// Its primary user is [`SortListModel`][crate::SortListModel]
    ///
    /// The model will use a sorter to determine the order in which
    /// its items should appear by calling `Gtk::Sorter::compare()`
    /// for pairs of items.
    ///
    /// Sorters may change their sorting behavior through their lifetime.
    /// In that case, they will emit the [`changed`][struct@crate::Sorter#changed] signal
    /// to notify that the sort order is no longer valid and should be updated
    /// by calling gtk_sorter_compare() again.
    ///
    /// GTK provides various pre-made sorter implementations for common sorting
    /// operations. [`ColumnView`][crate::ColumnView] has built-in support for sorting lists
    /// via the [`sorter`][struct@crate::ColumnViewColumn#sorter] property, where the user can
    /// change the sorting by clicking on list headers.
    ///
    /// Of course, in particular for large lists, it is also possible to subclass
    /// [`Sorter`][crate::Sorter] and provide one's own sorter.
    ///
    /// ## Signals
    ///
    ///
    /// #### `changed`
    ///  Emitted whenever the sorter changed.
    ///
    /// Users of the sorter should then update the sort order
    /// again via gtk_sorter_compare().
    ///
    /// [`SortListModel`][crate::SortListModel] handles this signal automatically.
    ///
    /// Depending on the @change parameter, it may be possible to update
    /// the sort order without a full resorting. Refer to the
    /// [`SorterChange`][crate::SorterChange] documentation for details.
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`SorterExt`][trait@crate::prelude::SorterExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkSorter")]
    pub struct Sorter(Object<ffi::GtkSorter, ffi::GtkSorterClass>);

    match fn {
        type_ => || ffi::gtk_sorter_get_type(),
    }
}

impl Sorter {
    pub const NONE: Option<&'static Sorter> = None;
}

/// Trait containing all [`struct@Sorter`] methods.
///
/// # Implementors
///
/// [`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]
pub trait SorterExt: 'static {
    /// Notifies all users of the sorter that it has changed.
    ///
    /// This emits the [`changed`][struct@crate::Sorter#changed] signal. Users
    /// of the sorter should then update the sort order via
    /// `Gtk::Sorter::compare()`.
    ///
    /// Depending on the @change parameter, it may be possible to
    /// update the sort order without a full resorting. Refer to
    /// the [`SorterChange`][crate::SorterChange] documentation for details.
    ///
    /// This function is intended for implementors of [`Sorter`][crate::Sorter]
    /// subclasses and should not be called from other functions.
    /// ## `change`
    /// How the sorter changed
    #[doc(alias = "gtk_sorter_changed")]
    fn changed(&self, change: SorterChange);

    #[doc(alias = "gtk_sorter_compare")]
    fn compare(&self, item1: &impl IsA<glib::Object>, item2: &impl IsA<glib::Object>) -> Ordering;

    /// Gets the order that @self conforms to.
    ///
    /// See [`SorterOrder`][crate::SorterOrder] for details
    /// of the possible return values.
    ///
    /// This function is intended to allow optimizations.
    ///
    /// # Returns
    ///
    /// The order
    #[doc(alias = "gtk_sorter_get_order")]
    #[doc(alias = "get_order")]
    fn order(&self) -> SorterOrder;

    /// Emitted whenever the sorter changed.
    ///
    /// Users of the sorter should then update the sort order
    /// again via gtk_sorter_compare().
    ///
    /// [`SortListModel`][crate::SortListModel] handles this signal automatically.
    ///
    /// Depending on the @change parameter, it may be possible to update
    /// the sort order without a full resorting. Refer to the
    /// [`SorterChange`][crate::SorterChange] documentation for details.
    /// ## `change`
    /// how the sorter changed
    #[doc(alias = "changed")]
    fn connect_changed<F: Fn(&Self, SorterChange) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<Sorter>> SorterExt for O {
    fn changed(&self, change: SorterChange) {
        unsafe {
            ffi::gtk_sorter_changed(self.as_ref().to_glib_none().0, change.into_glib());
        }
    }

    fn compare(&self, item1: &impl IsA<glib::Object>, item2: &impl IsA<glib::Object>) -> Ordering {
        unsafe {
            from_glib(ffi::gtk_sorter_compare(
                self.as_ref().to_glib_none().0,
                item1.as_ref().to_glib_none().0,
                item2.as_ref().to_glib_none().0,
            ))
        }
    }

    fn order(&self) -> SorterOrder {
        unsafe { from_glib(ffi::gtk_sorter_get_order(self.as_ref().to_glib_none().0)) }
    }

    fn connect_changed<F: Fn(&Self, SorterChange) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn changed_trampoline<
            P: IsA<Sorter>,
            F: Fn(&P, SorterChange) + 'static,
        >(
            this: *mut ffi::GtkSorter,
            change: ffi::GtkSorterChange,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                Sorter::from_glib_borrow(this).unsafe_cast_ref(),
                from_glib(change),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"changed\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    changed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for Sorter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Sorter")
    }
}