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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// 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::TreeDragSource;
use crate::TreeIter;
use crate::TreeModel;
use crate::TreePath;
use glib::object::IsA;
use glib::translate::*;
use glib::StaticType;
use std::boxed::Box as Box_;
use std::fmt;

glib::wrapper! {
    /// A [`TreeModelFilter`][crate::TreeModelFilter] is a tree model which wraps another tree model,
    /// and can do the following things:
    ///
    /// - Filter specific rows, based on data from a “visible column”, a column
    ///  storing booleans indicating whether the row should be filtered or not,
    ///  or based on the return value of a “visible function”, which gets a
    ///  model, iter and user_data and returns a boolean indicating whether the
    ///  row should be filtered or not.
    ///
    /// - Modify the “appearance” of the model, using a modify function.
    ///  This is extremely powerful and allows for just changing some
    ///  values and also for creating a completely different model based
    ///  on the given child model.
    ///
    /// - Set a different root node, also known as a “virtual root”. You can pass
    ///  in a [`TreePath`][crate::TreePath] indicating the root node for the filter at construction
    ///  time.
    ///
    /// The basic API is similar to [`TreeModelSort`][crate::TreeModelSort]. For an example on its usage,
    /// see the section on [`TreeModelSort`][crate::TreeModelSort].
    ///
    /// When using [`TreeModelFilter`][crate::TreeModelFilter], it is important to realize that
    /// [`TreeModelFilter`][crate::TreeModelFilter] maintains an internal cache of all nodes which are
    /// visible in its clients. The cache is likely to be a subtree of the tree
    /// exposed by the child model. [`TreeModelFilter`][crate::TreeModelFilter] will not cache the entire
    /// child model when unnecessary to not compromise the caching mechanism
    /// that is exposed by the reference counting scheme. If the child model
    /// implements reference counting, unnecessary signals may not be emitted
    /// because of reference counting rule 3, see the [`TreeModel`][crate::TreeModel]
    /// documentation. (Note that e.g. [`TreeStore`][crate::TreeStore] does not implement
    /// reference counting and will always emit all signals, even when
    /// the receiving node is not visible).
    ///
    /// Because of this, limitations for possible visible functions do apply.
    /// In general, visible functions should only use data or properties from
    /// the node for which the visibility state must be determined, its siblings
    /// or its parents. Usually, having a dependency on the state of any child
    /// node is not possible, unless references are taken on these explicitly.
    /// When no such reference exists, no signals may be received for these child
    /// nodes (see reference couting rule number 3 in the [`TreeModel`][crate::TreeModel] section).
    ///
    /// Determining the visibility state of a given node based on the state
    /// of its child nodes is a frequently occurring use case. Therefore,
    /// [`TreeModelFilter`][crate::TreeModelFilter] explicitly supports this. For example, when a node
    /// does not have any children, you might not want the node to be visible.
    /// As soon as the first row is added to the node’s child level (or the
    /// last row removed), the node’s visibility should be updated.
    ///
    /// This introduces a dependency from the node on its child nodes. In order
    /// to accommodate this, [`TreeModelFilter`][crate::TreeModelFilter] must make sure the necessary
    /// signals are received from the child model. This is achieved by building,
    /// for all nodes which are exposed as visible nodes to [`TreeModelFilter`][crate::TreeModelFilter]'s
    /// clients, the child level (if any) and take a reference on the first node
    /// in this level. Furthermore, for every row-inserted, row-changed or
    /// row-deleted signal (also these which were not handled because the node
    /// was not cached), [`TreeModelFilter`][crate::TreeModelFilter] will check if the visibility state
    /// of any parent node has changed.
    ///
    /// Beware, however, that this explicit support is limited to these two
    /// cases. For example, if you want a node to be visible only if two nodes
    /// in a child’s child level (2 levels deeper) are visible, you are on your
    /// own. In this case, either rely on [`TreeStore`][crate::TreeStore] to emit all signals
    /// because it does not implement reference counting, or for models that
    /// do implement reference counting, obtain references on these child levels
    /// yourself.
    ///
    /// # Implements
    ///
    /// [`TreeModelFilterExt`][trait@crate::prelude::TreeModelFilterExt], [`trait@glib::ObjectExt`], [`TreeDragSourceExt`][trait@crate::prelude::TreeDragSourceExt], [`TreeModelExt`][trait@crate::prelude::TreeModelExt]
    #[doc(alias = "GtkTreeModelFilter")]
    pub struct TreeModelFilter(Object<ffi::GtkTreeModelFilter, ffi::GtkTreeModelFilterClass>) @implements TreeDragSource, TreeModel;

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

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

/// Trait containing all [`struct@TreeModelFilter`] methods.
///
/// # Implementors
///
/// [`TreeModelFilter`][struct@crate::TreeModelFilter]
pub trait TreeModelFilterExt: 'static {
    /// This function should almost never be called. It clears the `self`
    /// of any cached iterators that haven’t been reffed with
    /// `gtk_tree_model_ref_node()`. This might be useful if the child model
    /// being filtered is static (and doesn’t change often) and there has been
    /// a lot of unreffed access to nodes. As a side effect of this function,
    /// all unreffed iters will be invalid.
    #[doc(alias = "gtk_tree_model_filter_clear_cache")]
    fn clear_cache(&self);

    /// Sets `filter_iter` to point to the row in `self` that corresponds to the
    /// row pointed at by `child_iter`. If `filter_iter` was not set, [`false`] is
    /// returned.
    /// ## `child_iter`
    /// A valid [`TreeIter`][crate::TreeIter] pointing to a row on the child model.
    ///
    /// # Returns
    ///
    /// [`true`], if `filter_iter` was set, i.e. if `child_iter` is a
    /// valid iterator pointing to a visible row in child model.
    ///
    /// ## `filter_iter`
    /// An uninitialized [`TreeIter`][crate::TreeIter].
    #[doc(alias = "gtk_tree_model_filter_convert_child_iter_to_iter")]
    fn convert_child_iter_to_iter(&self, child_iter: &TreeIter) -> Option<TreeIter>;

    /// Converts `child_path` to a path relative to `self`. That is, `child_path`
    /// points to a path in the child model. The rerturned path will point to the
    /// same row in the filtered model. If `child_path` isn’t a valid path on the
    /// child model or points to a row which is not visible in `self`, then [`None`]
    /// is returned.
    /// ## `child_path`
    /// A [`TreePath`][crate::TreePath] to convert.
    ///
    /// # Returns
    ///
    /// A newly allocated [`TreePath`][crate::TreePath], or [`None`].
    #[doc(alias = "gtk_tree_model_filter_convert_child_path_to_path")]
    fn convert_child_path_to_path(&self, child_path: &TreePath) -> Option<TreePath>;

    /// Sets `child_iter` to point to the row pointed to by `filter_iter`.
    /// ## `filter_iter`
    /// A valid [`TreeIter`][crate::TreeIter] pointing to a row on `self`.
    ///
    /// # Returns
    ///
    ///
    /// ## `child_iter`
    /// An uninitialized [`TreeIter`][crate::TreeIter].
    #[doc(alias = "gtk_tree_model_filter_convert_iter_to_child_iter")]
    fn convert_iter_to_child_iter(&self, filter_iter: &TreeIter) -> TreeIter;

    /// Converts `filter_path` to a path on the child model of `self`. That is,
    /// `filter_path` points to a location in `self`. The returned path will
    /// point to the same location in the model not being filtered. If `filter_path`
    /// does not point to a location in the child model, [`None`] is returned.
    /// ## `filter_path`
    /// A [`TreePath`][crate::TreePath] to convert.
    ///
    /// # Returns
    ///
    /// A newly allocated [`TreePath`][crate::TreePath], or [`None`].
    #[doc(alias = "gtk_tree_model_filter_convert_path_to_child_path")]
    fn convert_path_to_child_path(&self, filter_path: &TreePath) -> Option<TreePath>;

    /// Returns a pointer to the child model of `self`.
    ///
    /// # Returns
    ///
    /// A pointer to a [`TreeModel`][crate::TreeModel].
    #[doc(alias = "gtk_tree_model_filter_get_model")]
    #[doc(alias = "get_model")]
    fn model(&self) -> Option<TreeModel>;

    /// Emits ::row_changed for each row in the child model, which causes
    /// the filter to re-evaluate whether a row is visible or not.
    #[doc(alias = "gtk_tree_model_filter_refilter")]
    fn refilter(&self);

    //#[doc(alias = "gtk_tree_model_filter_set_modify_func")]
    //fn set_modify_func<P: Fn(&TreeModel, &TreeIter, &glib::Value, i32) + 'static>(&self, types: /*Unimplemented*/&CArray TypeId { ns_id: 0, id: 30 }, func: P);

    /// Sets `column` of the child_model to be the column where `self` should
    /// look for visibility information. `columns` should be a column of type
    /// `G_TYPE_BOOLEAN`, where [`true`] means that a row is visible, and [`false`]
    /// if not.
    ///
    /// Note that [`set_visible_func()`][Self::set_visible_func()] or
    /// [`set_visible_column()`][Self::set_visible_column()] can only be called
    /// once for a given filter model.
    /// ## `column`
    /// A `gint` which is the column containing the visible information
    #[doc(alias = "gtk_tree_model_filter_set_visible_column")]
    fn set_visible_column(&self, column: i32);

    /// Sets the visible function used when filtering the `self` to be `func`.
    /// The function should return [`true`] if the given row should be visible and
    /// [`false`] otherwise.
    ///
    /// If the condition calculated by the function changes over time (e.g.
    /// because it depends on some global parameters), you must call
    /// [`refilter()`][Self::refilter()] to keep the visibility information
    /// of the model up-to-date.
    ///
    /// Note that `func` is called whenever a row is inserted, when it may still
    /// be empty. The visible function should therefore take special care of empty
    /// rows, like in the example below.
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// static gboolean
    /// visible_func (GtkTreeModel *model,
    ///               GtkTreeIter  *iter,
    ///               gpointer      data)
    /// {
    ///   // Visible if row is non-empty and first column is “HI”
    ///   gchar *str;
    ///   gboolean visible = FALSE;
    ///
    ///   gtk_tree_model_get (model, iter, 0, &str, -1);
    ///   if (str && strcmp (str, "HI") == 0)
    ///     visible = TRUE;
    ///   g_free (str);
    ///
    ///   return visible;
    /// }
    /// ```
    ///
    /// Note that [`set_visible_func()`][Self::set_visible_func()] or
    /// [`set_visible_column()`][Self::set_visible_column()] can only be called
    /// once for a given filter model.
    /// ## `func`
    /// A `GtkTreeModelFilterVisibleFunc`, the visible function
    #[doc(alias = "gtk_tree_model_filter_set_visible_func")]
    fn set_visible_func<P: Fn(&TreeModel, &TreeIter) -> bool + 'static>(&self, func: P);

    #[doc(alias = "child-model")]
    fn child_model(&self) -> Option<TreeModel>;
}

impl<O: IsA<TreeModelFilter>> TreeModelFilterExt for O {
    fn clear_cache(&self) {
        unsafe {
            ffi::gtk_tree_model_filter_clear_cache(self.as_ref().to_glib_none().0);
        }
    }

    fn convert_child_iter_to_iter(&self, child_iter: &TreeIter) -> Option<TreeIter> {
        unsafe {
            let mut filter_iter = TreeIter::uninitialized();
            let ret = from_glib(ffi::gtk_tree_model_filter_convert_child_iter_to_iter(
                self.as_ref().to_glib_none().0,
                filter_iter.to_glib_none_mut().0,
                mut_override(child_iter.to_glib_none().0),
            ));
            if ret {
                Some(filter_iter)
            } else {
                None
            }
        }
    }

    fn convert_child_path_to_path(&self, child_path: &TreePath) -> Option<TreePath> {
        unsafe {
            from_glib_full(ffi::gtk_tree_model_filter_convert_child_path_to_path(
                self.as_ref().to_glib_none().0,
                mut_override(child_path.to_glib_none().0),
            ))
        }
    }

    fn convert_iter_to_child_iter(&self, filter_iter: &TreeIter) -> TreeIter {
        unsafe {
            let mut child_iter = TreeIter::uninitialized();
            ffi::gtk_tree_model_filter_convert_iter_to_child_iter(
                self.as_ref().to_glib_none().0,
                child_iter.to_glib_none_mut().0,
                mut_override(filter_iter.to_glib_none().0),
            );
            child_iter
        }
    }

    fn convert_path_to_child_path(&self, filter_path: &TreePath) -> Option<TreePath> {
        unsafe {
            from_glib_full(ffi::gtk_tree_model_filter_convert_path_to_child_path(
                self.as_ref().to_glib_none().0,
                mut_override(filter_path.to_glib_none().0),
            ))
        }
    }

    fn model(&self) -> Option<TreeModel> {
        unsafe {
            from_glib_none(ffi::gtk_tree_model_filter_get_model(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn refilter(&self) {
        unsafe {
            ffi::gtk_tree_model_filter_refilter(self.as_ref().to_glib_none().0);
        }
    }

    //fn set_modify_func<P: Fn(&TreeModel, &TreeIter, &glib::Value, i32) + 'static>(&self, types: /*Unimplemented*/&CArray TypeId { ns_id: 0, id: 30 }, func: P) {
    //    unsafe { TODO: call ffi:gtk_tree_model_filter_set_modify_func() }
    //}

    fn set_visible_column(&self, column: i32) {
        unsafe {
            ffi::gtk_tree_model_filter_set_visible_column(self.as_ref().to_glib_none().0, column);
        }
    }

    fn set_visible_func<P: Fn(&TreeModel, &TreeIter) -> bool + 'static>(&self, func: P) {
        let func_data: Box_<P> = Box_::new(func);
        unsafe extern "C" fn func_func<P: Fn(&TreeModel, &TreeIter) -> bool + 'static>(
            model: *mut ffi::GtkTreeModel,
            iter: *mut ffi::GtkTreeIter,
            data: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let model = from_glib_borrow(model);
            let iter = from_glib_borrow(iter);
            let callback: &P = &*(data as *mut _);
            let res = (*callback)(&model, &iter);
            res.into_glib()
        }
        let func = Some(func_func::<P> as _);
        unsafe extern "C" fn destroy_func<P: Fn(&TreeModel, &TreeIter) -> bool + 'static>(
            data: glib::ffi::gpointer,
        ) {
            let _callback: Box_<P> = Box_::from_raw(data as *mut _);
        }
        let destroy_call3 = Some(destroy_func::<P> as _);
        let super_callback0: Box_<P> = func_data;
        unsafe {
            ffi::gtk_tree_model_filter_set_visible_func(
                self.as_ref().to_glib_none().0,
                func,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call3,
            );
        }
    }

    fn child_model(&self) -> Option<TreeModel> {
        glib::ObjectExt::property(self.as_ref(), "child-model")
    }
}

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