Skip to main content

gtk/auto/
tree_model_sort.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::{TreeDragSource, TreeIter, TreeModel, TreePath, TreeSortable};
6use glib::{prelude::*, translate::*};
7use std::fmt;
8
9glib::wrapper! {
10    /// The [`TreeModelSort`][crate::TreeModelSort] is a model which implements the [`TreeSortable`][crate::TreeSortable]
11    /// interface. It does not hold any data itself, but rather is created with
12    /// a child model and proxies its data. It has identical column types to
13    /// this child model, and the changes in the child are propagated. The
14    /// primary purpose of this model is to provide a way to sort a different
15    /// model without modifying it. Note that the sort function used by
16    /// [`TreeModelSort`][crate::TreeModelSort] is not guaranteed to be stable.
17    ///
18    /// The use of this is best demonstrated through an example. In the
19    /// following sample code we create two [`TreeView`][crate::TreeView] widgets each with a
20    /// view of the same data. As the model is wrapped here by a
21    /// [`TreeModelSort`][crate::TreeModelSort], the two `GtkTreeViews` can each sort their
22    /// view of the data without affecting the other. By contrast, if we
23    /// simply put the same model in each widget, then sorting the first would
24    /// sort the second.
25    ///
26    /// ## Using a [`TreeModelSort`][crate::TreeModelSort]
27    ///
28    ///
29    ///
30    /// **⚠️ The following code is in C ⚠️**
31    ///
32    /// ```C
33    /// {
34    ///   GtkTreeView *tree_view1;
35    ///   GtkTreeView *tree_view2;
36    ///   GtkTreeModel *sort_model1;
37    ///   GtkTreeModel *sort_model2;
38    ///   GtkTreeModel *child_model;
39    ///
40    ///   // get the child model
41    ///   child_model = get_my_model ();
42    ///
43    ///   // Create the first tree
44    ///   sort_model1 = gtk_tree_model_sort_new_with_model (child_model);
45    ///   tree_view1 = gtk_tree_view_new_with_model (sort_model1);
46    ///
47    ///   // Create the second tree
48    ///   sort_model2 = gtk_tree_model_sort_new_with_model (child_model);
49    ///   tree_view2 = gtk_tree_view_new_with_model (sort_model2);
50    ///
51    ///   // Now we can sort the two models independently
52    ///   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model1),
53    ///                                         COLUMN_1, GTK_SORT_ASCENDING);
54    ///   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort_model2),
55    ///                                         COLUMN_1, GTK_SORT_DESCENDING);
56    /// }
57    /// ```
58    ///
59    /// To demonstrate how to access the underlying child model from the sort
60    /// model, the next example will be a callback for the [`TreeSelection`][crate::TreeSelection]
61    /// [`changed`][struct@crate::TreeSelection#changed] signal. In this callback, we get a string
62    /// from COLUMN_1 of the model. We then modify the string, find the same
63    /// selected row on the child model, and change the row there.
64    ///
65    /// ## Accessing the child model of in a selection changed callback
66    ///
67    ///
68    ///
69    /// **⚠️ The following code is in C ⚠️**
70    ///
71    /// ```C
72    /// void
73    /// selection_changed (GtkTreeSelection *selection, gpointer data)
74    /// {
75    ///   GtkTreeModel *sort_model = NULL;
76    ///   GtkTreeModel *child_model;
77    ///   GtkTreeIter sort_iter;
78    ///   GtkTreeIter child_iter;
79    ///   char *some_data = NULL;
80    ///   char *modified_data;
81    ///
82    ///   // Get the current selected row and the model.
83    ///   if (! gtk_tree_selection_get_selected (selection,
84    ///                                          &sort_model,
85    ///                                          &sort_iter))
86    ///     return;
87    ///
88    ///   // Look up the current value on the selected row and get
89    ///   // a new value to change it to.
90    ///   gtk_tree_model_get (GTK_TREE_MODEL (sort_model), &sort_iter,
91    ///                       COLUMN_1, &some_data,
92    ///                       -1);
93    ///
94    ///   modified_data = change_the_data (some_data);
95    ///   g_free (some_data);
96    ///
97    ///   // Get an iterator on the child model, instead of the sort model.
98    ///   gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (sort_model),
99    ///                                                   &child_iter,
100    ///                                                   &sort_iter);
101    ///
102    ///   // Get the child model and change the value of the row. In this
103    ///   // example, the child model is a GtkListStore. It could be any other
104    ///   // type of model, though.
105    ///   child_model = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (sort_model));
106    ///   gtk_list_store_set (GTK_LIST_STORE (child_model), &child_iter,
107    ///                       COLUMN_1, &modified_data,
108    ///                       -1);
109    ///   g_free (modified_data);
110    /// }
111    /// ```
112    ///
113    /// ## Properties
114    ///
115    ///
116    /// #### `model`
117    ///  Readable | Writeable | Construct Only
118    ///
119    /// # Implements
120    ///
121    /// [`TreeModelSortExt`][trait@crate::prelude::TreeModelSortExt], [`trait@glib::ObjectExt`], [`TreeDragSourceExt`][trait@crate::prelude::TreeDragSourceExt], [`TreeModelExt`][trait@crate::prelude::TreeModelExt], [`TreeSortableExt`][trait@crate::prelude::TreeSortableExt], [`TreeSortableExtManual`][trait@crate::prelude::TreeSortableExtManual]
122    #[doc(alias = "GtkTreeModelSort")]
123    pub struct TreeModelSort(Object<ffi::GtkTreeModelSort, ffi::GtkTreeModelSortClass>) @implements TreeDragSource, TreeModel, TreeSortable;
124
125    match fn {
126        type_ => || ffi::gtk_tree_model_sort_get_type(),
127    }
128}
129
130impl TreeModelSort {
131    pub const NONE: Option<&'static TreeModelSort> = None;
132
133    /// Creates a new [`TreeModelSort`][crate::TreeModelSort], with `child_model` as the child model.
134    /// ## `child_model`
135    /// A [`TreeModel`][crate::TreeModel]
136    ///
137    /// # Returns
138    ///
139    /// A new [`TreeModelSort`][crate::TreeModelSort].
140    #[doc(alias = "gtk_tree_model_sort_new_with_model")]
141    #[doc(alias = "new_with_model")]
142    pub fn new(child_model: &impl IsA<TreeModel>) -> TreeModelSort {
143        skip_assert_initialized!();
144        unsafe {
145            from_glib_full(ffi::gtk_tree_model_sort_new_with_model(
146                child_model.as_ref().to_glib_none().0,
147            ))
148        }
149    }
150}
151
152mod sealed {
153    pub trait Sealed {}
154    impl<T: super::IsA<super::TreeModelSort>> Sealed for T {}
155}
156
157/// Trait containing all [`struct@TreeModelSort`] methods.
158///
159/// # Implementors
160///
161/// [`TreeModelSort`][struct@crate::TreeModelSort]
162pub trait TreeModelSortExt: IsA<TreeModelSort> + sealed::Sealed + 'static {
163    /// This function should almost never be called. It clears the `self`
164    /// of any cached iterators that haven’t been reffed with
165    /// `gtk_tree_model_ref_node()`. This might be useful if the child model being
166    /// sorted is static (and doesn’t change often) and there has been a lot of
167    /// unreffed access to nodes. As a side effect of this function, all unreffed
168    /// iters will be invalid.
169    #[doc(alias = "gtk_tree_model_sort_clear_cache")]
170    fn clear_cache(&self) {
171        unsafe {
172            ffi::gtk_tree_model_sort_clear_cache(self.as_ref().to_glib_none().0);
173        }
174    }
175
176    /// Sets `sort_iter` to point to the row in `self` that corresponds to
177    /// the row pointed at by `child_iter`. If `sort_iter` was not set, [`false`]
178    /// is returned. Note: a boolean is only returned since 2.14.
179    /// ## `child_iter`
180    /// A valid [`TreeIter`][crate::TreeIter] pointing to a row on the child model
181    ///
182    /// # Returns
183    ///
184    /// [`true`], if `sort_iter` was set, i.e. if `sort_iter` is a
185    /// valid iterator pointer to a visible row in the child model.
186    ///
187    /// ## `sort_iter`
188    /// An uninitialized [`TreeIter`][crate::TreeIter].
189    #[doc(alias = "gtk_tree_model_sort_convert_child_iter_to_iter")]
190    fn convert_child_iter_to_iter(&self, child_iter: &TreeIter) -> Option<TreeIter> {
191        unsafe {
192            let mut sort_iter = TreeIter::uninitialized();
193            let ret = from_glib(ffi::gtk_tree_model_sort_convert_child_iter_to_iter(
194                self.as_ref().to_glib_none().0,
195                sort_iter.to_glib_none_mut().0,
196                mut_override(child_iter.to_glib_none().0),
197            ));
198            if ret {
199                Some(sort_iter)
200            } else {
201                None
202            }
203        }
204    }
205
206    /// Converts `child_path` to a path relative to `self`. That is,
207    /// `child_path` points to a path in the child model. The returned path will
208    /// point to the same row in the sorted model. If `child_path` isn’t a valid
209    /// path on the child model, then [`None`] is returned.
210    /// ## `child_path`
211    /// A [`TreePath`][crate::TreePath] to convert
212    ///
213    /// # Returns
214    ///
215    /// A newly allocated [`TreePath`][crate::TreePath], or [`None`]
216    #[doc(alias = "gtk_tree_model_sort_convert_child_path_to_path")]
217    fn convert_child_path_to_path(&self, child_path: &TreePath) -> Option<TreePath> {
218        unsafe {
219            from_glib_full(ffi::gtk_tree_model_sort_convert_child_path_to_path(
220                self.as_ref().to_glib_none().0,
221                mut_override(child_path.to_glib_none().0),
222            ))
223        }
224    }
225
226    /// Sets `child_iter` to point to the row pointed to by `sorted_iter`.
227    /// ## `sorted_iter`
228    /// A valid [`TreeIter`][crate::TreeIter] pointing to a row on `self`.
229    ///
230    /// # Returns
231    ///
232    ///
233    /// ## `child_iter`
234    /// An uninitialized [`TreeIter`][crate::TreeIter]
235    #[doc(alias = "gtk_tree_model_sort_convert_iter_to_child_iter")]
236    fn convert_iter_to_child_iter(&self, sorted_iter: &TreeIter) -> TreeIter {
237        unsafe {
238            let mut child_iter = TreeIter::uninitialized();
239            ffi::gtk_tree_model_sort_convert_iter_to_child_iter(
240                self.as_ref().to_glib_none().0,
241                child_iter.to_glib_none_mut().0,
242                mut_override(sorted_iter.to_glib_none().0),
243            );
244            child_iter
245        }
246    }
247
248    /// Converts `sorted_path` to a path on the child model of `self`.
249    /// That is, `sorted_path` points to a location in `self`. The
250    /// returned path will point to the same location in the model not being
251    /// sorted. If `sorted_path` does not point to a location in the child model,
252    /// [`None`] is returned.
253    /// ## `sorted_path`
254    /// A [`TreePath`][crate::TreePath] to convert
255    ///
256    /// # Returns
257    ///
258    /// A newly allocated [`TreePath`][crate::TreePath], or [`None`]
259    #[doc(alias = "gtk_tree_model_sort_convert_path_to_child_path")]
260    fn convert_path_to_child_path(&self, sorted_path: &TreePath) -> Option<TreePath> {
261        unsafe {
262            from_glib_full(ffi::gtk_tree_model_sort_convert_path_to_child_path(
263                self.as_ref().to_glib_none().0,
264                mut_override(sorted_path.to_glib_none().0),
265            ))
266        }
267    }
268
269    /// Returns the model the [`TreeModelSort`][crate::TreeModelSort] is sorting.
270    ///
271    /// # Returns
272    ///
273    /// the "child model" being sorted
274    #[doc(alias = "gtk_tree_model_sort_get_model")]
275    #[doc(alias = "get_model")]
276    fn model(&self) -> TreeModel {
277        unsafe {
278            from_glib_none(ffi::gtk_tree_model_sort_get_model(
279                self.as_ref().to_glib_none().0,
280            ))
281        }
282    }
283
284    /// > This function is slow. Only use it for debugging and/or testing
285    /// > purposes.
286    ///
287    /// Checks if the given iter is a valid iter for this [`TreeModelSort`][crate::TreeModelSort].
288    /// ## `iter`
289    /// A [`TreeIter`][crate::TreeIter].
290    ///
291    /// # Returns
292    ///
293    /// [`true`] if the iter is valid, [`false`] if the iter is invalid.
294    #[doc(alias = "gtk_tree_model_sort_iter_is_valid")]
295    fn iter_is_valid(&self, iter: &TreeIter) -> bool {
296        unsafe {
297            from_glib(ffi::gtk_tree_model_sort_iter_is_valid(
298                self.as_ref().to_glib_none().0,
299                mut_override(iter.to_glib_none().0),
300            ))
301        }
302    }
303
304    /// This resets the default sort function to be in the “unsorted” state. That
305    /// is, it is in the same order as the child model. It will re-sort the model
306    /// to be in the same order as the child model only if the [`TreeModelSort`][crate::TreeModelSort]
307    /// is in “unsorted” state.
308    #[doc(alias = "gtk_tree_model_sort_reset_default_sort_func")]
309    fn reset_default_sort_func(&self) {
310        unsafe {
311            ffi::gtk_tree_model_sort_reset_default_sort_func(self.as_ref().to_glib_none().0);
312        }
313    }
314}
315
316impl<O: IsA<TreeModelSort>> TreeModelSortExt for O {}
317
318impl fmt::Display for TreeModelSort {
319    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
320        f.write_str("TreeModelSort")
321    }
322}