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