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