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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// 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::SelectionMode;
use crate::TreeIter;
use crate::TreeModel;
use crate::TreePath;
use crate::TreeView;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;
use std::ptr;

glib::wrapper! {
    /// The [`TreeSelection`][crate::TreeSelection] object is a helper object to manage the selection
    /// for a [`TreeView`][crate::TreeView] widget. The [`TreeSelection`][crate::TreeSelection] object is
    /// automatically created when a new [`TreeView`][crate::TreeView] widget is created, and
    /// cannot exist independently of this widget. The primary reason the
    /// [`TreeSelection`][crate::TreeSelection] objects exists is for cleanliness of code and API.
    /// That is, there is no conceptual reason all these functions could not be
    /// methods on the [`TreeView`][crate::TreeView] widget instead of a separate function.
    ///
    /// The [`TreeSelection`][crate::TreeSelection] object is gotten from a [`TreeView`][crate::TreeView] by calling
    /// [`TreeViewExt::selection()`][crate::prelude::TreeViewExt::selection()]. It can be manipulated to check the
    /// selection status of the tree, as well as select and deselect individual
    /// rows. Selection is done completely view side. As a result, multiple
    /// views of the same model can have completely different selections.
    /// Additionally, you cannot change the selection of a row on the model that
    /// is not currently displayed by the view without expanding its parents
    /// first.
    ///
    /// One of the important things to remember when monitoring the selection of
    /// a view is that the `signal::TreeSelection::changed` signal is mostly a hint.
    /// That is, it may only emit one signal when a range of rows is selected.
    /// Additionally, it may on occasion emit a `signal::TreeSelection::changed` signal
    /// when nothing has happened (mostly as a result of programmers calling
    /// select_row on an already selected row).
    ///
    /// # Implements
    ///
    /// [`TreeSelectionExt`][trait@crate::prelude::TreeSelectionExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkTreeSelection")]
    pub struct TreeSelection(Object<ffi::GtkTreeSelection, ffi::GtkTreeSelectionClass>);

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

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

/// Trait containing all [`struct@TreeSelection`] methods.
///
/// # Implementors
///
/// [`TreeSelection`][struct@crate::TreeSelection]
pub trait TreeSelectionExt: 'static {
    /// Returns the number of rows that have been selected in `tree`.
    ///
    /// # Returns
    ///
    /// The number of rows selected.
    #[doc(alias = "gtk_tree_selection_count_selected_rows")]
    fn count_selected_rows(&self) -> i32;

    /// Gets the selection mode for `self`. See
    /// [`set_mode()`][Self::set_mode()].
    ///
    /// # Returns
    ///
    /// the current selection mode
    #[doc(alias = "gtk_tree_selection_get_mode")]
    #[doc(alias = "get_mode")]
    fn mode(&self) -> SelectionMode;

    //#[doc(alias = "gtk_tree_selection_get_select_function")]
    //#[doc(alias = "get_select_function")]
    //fn select_function(&self) -> Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>;

    /// Sets `iter` to the currently selected node, if `self` is set to
    /// [`SelectionMode::Single`][crate::SelectionMode::Single] or [`SelectionMode::Browse`][crate::SelectionMode::Browse].
    ///
    /// The `iter` argument may be [`None`] if you just want to test if `self`
    /// has any selected nodes.
    ///
    /// The `model` argument is filled with the current model as a convenience.
    ///
    /// This function will not work with [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]. See
    /// [`selected_rows()`][Self::selected_rows()] instead.
    ///
    /// # Returns
    ///
    /// [`true`], if there is a selected node.
    ///
    /// ## `model`
    /// the model
    ///
    /// ## `iter`
    /// the iterator for the selected row
    #[doc(alias = "gtk_tree_selection_get_selected")]
    #[doc(alias = "get_selected")]
    fn selected(&self) -> Option<(TreeModel, TreeIter)>;

    /// Creates a list of path of all selected rows.
    ///
    /// Additionally, if you are planning on modifying the model after calling
    /// this function, you may want to convert the returned list into a list
    /// of `GtkTreeRowReferences`.
    ///
    /// To do this, you can use [`TreeRowReference::new()`][crate::TreeRowReference::new()].
    ///
    /// To free the return value, use:
    ///
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
    /// ```
    ///
    /// # Returns
    ///
    /// the selected paths
    ///
    /// ## `model`
    /// A pointer to set to the [`TreeModel`][crate::TreeModel], or [`None`].
    #[doc(alias = "gtk_tree_selection_get_selected_rows")]
    #[doc(alias = "get_selected_rows")]
    fn selected_rows(&self) -> (Vec<TreePath>, TreeModel);

    /// Returns the tree view associated with `self`.
    ///
    /// # Returns
    ///
    /// A [`TreeView`][crate::TreeView]
    #[doc(alias = "gtk_tree_selection_get_tree_view")]
    #[doc(alias = "get_tree_view")]
    fn tree_view(&self) -> Option<TreeView>;

    //#[doc(alias = "gtk_tree_selection_get_user_data")]
    //#[doc(alias = "get_user_data")]
    //fn user_data(&self) -> /*Unimplemented*/Option<Fundamental: Pointer>;

    /// Returns [`true`] if the row at `iter` is currently selected.
    /// ## `iter`
    /// A valid [`TreeIter`][crate::TreeIter]
    ///
    /// # Returns
    ///
    /// [`true`], if `iter` is selected
    #[doc(alias = "gtk_tree_selection_iter_is_selected")]
    fn iter_is_selected(&self, iter: &TreeIter) -> bool;

    /// Returns [`true`] if the row pointed to by `path` is currently selected. If `path`
    /// does not point to a valid location, [`false`] is returned
    /// ## `path`
    /// A [`TreePath`][crate::TreePath] to check selection on.
    ///
    /// # Returns
    ///
    /// [`true`] if `path` is selected.
    #[doc(alias = "gtk_tree_selection_path_is_selected")]
    fn path_is_selected(&self, path: &TreePath) -> bool;

    /// Selects all the nodes. `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]
    /// mode.
    #[doc(alias = "gtk_tree_selection_select_all")]
    fn select_all(&self);

    /// Selects the specified iterator.
    /// ## `iter`
    /// The [`TreeIter`][crate::TreeIter] to be selected.
    #[doc(alias = "gtk_tree_selection_select_iter")]
    fn select_iter(&self, iter: &TreeIter);

    /// Select the row at `path`.
    /// ## `path`
    /// The [`TreePath`][crate::TreePath] to be selected.
    #[doc(alias = "gtk_tree_selection_select_path")]
    fn select_path(&self, path: &TreePath);

    /// Selects a range of nodes, determined by `start_path` and `end_path` inclusive.
    /// `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple] mode.
    /// ## `start_path`
    /// The initial node of the range.
    /// ## `end_path`
    /// The final node of the range.
    #[doc(alias = "gtk_tree_selection_select_range")]
    fn select_range(&self, start_path: &TreePath, end_path: &TreePath);

    /// Calls a function for each selected node. Note that you cannot modify
    /// the tree or selection from within this function. As a result,
    /// [`selected_rows()`][Self::selected_rows()] might be more useful.
    /// ## `func`
    /// The function to call for each selected node.
    #[doc(alias = "gtk_tree_selection_selected_foreach")]
    fn selected_foreach<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(&self, func: P);

    /// Sets the selection mode of the `self`. If the previous type was
    /// [`SelectionMode::Multiple`][crate::SelectionMode::Multiple], then the anchor is kept selected, if it was
    /// previously selected.
    /// ## `type_`
    /// The selection mode
    #[doc(alias = "gtk_tree_selection_set_mode")]
    fn set_mode(&self, type_: SelectionMode);

    /// Sets the selection function.
    ///
    /// If set, this function is called before any node is selected or unselected,
    /// giving some control over which nodes are selected. The select function
    /// should return [`true`] if the state of the node may be toggled, and [`false`]
    /// if the state of the node should be left unchanged.
    /// ## `func`
    /// The selection function. May be [`None`]
    #[doc(alias = "gtk_tree_selection_set_select_function")]
    fn set_select_function(
        &self,
        func: Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
    );

    /// Unselects all the nodes.
    #[doc(alias = "gtk_tree_selection_unselect_all")]
    fn unselect_all(&self);

    /// Unselects the specified iterator.
    /// ## `iter`
    /// The [`TreeIter`][crate::TreeIter] to be unselected.
    #[doc(alias = "gtk_tree_selection_unselect_iter")]
    fn unselect_iter(&self, iter: &TreeIter);

    /// Unselects the row at `path`.
    /// ## `path`
    /// The [`TreePath`][crate::TreePath] to be unselected.
    #[doc(alias = "gtk_tree_selection_unselect_path")]
    fn unselect_path(&self, path: &TreePath);

    /// Unselects a range of nodes, determined by `start_path` and `end_path`
    /// inclusive.
    /// ## `start_path`
    /// The initial node of the range.
    /// ## `end_path`
    /// The initial node of the range.
    #[doc(alias = "gtk_tree_selection_unselect_range")]
    fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath);

    /// Emitted whenever the selection has (possibly) changed. Please note that
    /// this signal is mostly a hint. It may only be emitted once when a range
    /// of rows are selected, and it may occasionally be emitted when nothing
    /// has happened.
    #[doc(alias = "changed")]
    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "mode")]
    fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<TreeSelection>> TreeSelectionExt for O {
    fn count_selected_rows(&self) -> i32 {
        unsafe { ffi::gtk_tree_selection_count_selected_rows(self.as_ref().to_glib_none().0) }
    }

    fn mode(&self) -> SelectionMode {
        unsafe {
            from_glib(ffi::gtk_tree_selection_get_mode(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    //fn select_function(&self) -> Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>> {
    //    unsafe { TODO: call ffi:gtk_tree_selection_get_select_function() }
    //}

    fn selected(&self) -> Option<(TreeModel, TreeIter)> {
        unsafe {
            let mut model = ptr::null_mut();
            let mut iter = TreeIter::uninitialized();
            let ret = from_glib(ffi::gtk_tree_selection_get_selected(
                self.as_ref().to_glib_none().0,
                &mut model,
                iter.to_glib_none_mut().0,
            ));
            if ret {
                Some((from_glib_none(model), iter))
            } else {
                None
            }
        }
    }

    fn selected_rows(&self) -> (Vec<TreePath>, TreeModel) {
        unsafe {
            let mut model = ptr::null_mut();
            let ret =
                FromGlibPtrContainer::from_glib_full(ffi::gtk_tree_selection_get_selected_rows(
                    self.as_ref().to_glib_none().0,
                    &mut model,
                ));
            (ret, from_glib_none(model))
        }
    }

    fn tree_view(&self) -> Option<TreeView> {
        unsafe {
            from_glib_none(ffi::gtk_tree_selection_get_tree_view(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    //fn user_data(&self) -> /*Unimplemented*/Option<Fundamental: Pointer> {
    //    unsafe { TODO: call ffi:gtk_tree_selection_get_user_data() }
    //}

    fn iter_is_selected(&self, iter: &TreeIter) -> bool {
        unsafe {
            from_glib(ffi::gtk_tree_selection_iter_is_selected(
                self.as_ref().to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            ))
        }
    }

    fn path_is_selected(&self, path: &TreePath) -> bool {
        unsafe {
            from_glib(ffi::gtk_tree_selection_path_is_selected(
                self.as_ref().to_glib_none().0,
                mut_override(path.to_glib_none().0),
            ))
        }
    }

    fn select_all(&self) {
        unsafe {
            ffi::gtk_tree_selection_select_all(self.as_ref().to_glib_none().0);
        }
    }

    fn select_iter(&self, iter: &TreeIter) {
        unsafe {
            ffi::gtk_tree_selection_select_iter(
                self.as_ref().to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            );
        }
    }

    fn select_path(&self, path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_select_path(
                self.as_ref().to_glib_none().0,
                mut_override(path.to_glib_none().0),
            );
        }
    }

    fn select_range(&self, start_path: &TreePath, end_path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_select_range(
                self.as_ref().to_glib_none().0,
                mut_override(start_path.to_glib_none().0),
                mut_override(end_path.to_glib_none().0),
            );
        }
    }

    fn selected_foreach<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(&self, func: P) {
        let func_data: P = func;
        unsafe extern "C" fn func_func<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(
            model: *mut ffi::GtkTreeModel,
            path: *mut ffi::GtkTreePath,
            iter: *mut ffi::GtkTreeIter,
            data: glib::ffi::gpointer,
        ) {
            let model = from_glib_borrow(model);
            let path = from_glib_borrow(path);
            let iter = from_glib_borrow(iter);
            let callback: *mut P = data as *const _ as usize as *mut P;
            (*callback)(&model, &path, &iter);
        }
        let func = Some(func_func::<P> as _);
        let super_callback0: &P = &func_data;
        unsafe {
            ffi::gtk_tree_selection_selected_foreach(
                self.as_ref().to_glib_none().0,
                func,
                super_callback0 as *const _ as usize as *mut _,
            );
        }
    }

    fn set_mode(&self, type_: SelectionMode) {
        unsafe {
            ffi::gtk_tree_selection_set_mode(self.as_ref().to_glib_none().0, type_.into_glib());
        }
    }

    fn set_select_function(
        &self,
        func: Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
    ) {
        let func_data: Box_<
            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
        > = Box_::new(func);
        unsafe extern "C" fn func_func(
            selection: *mut ffi::GtkTreeSelection,
            model: *mut ffi::GtkTreeModel,
            path: *mut ffi::GtkTreePath,
            path_currently_selected: glib::ffi::gboolean,
            data: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let selection = from_glib_borrow(selection);
            let model = from_glib_borrow(model);
            let path = from_glib_borrow(path);
            let path_currently_selected = from_glib(path_currently_selected);
            let callback: &Option<
                Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>,
            > = &*(data as *mut _);
            let res = if let Some(ref callback) = *callback {
                callback(&selection, &model, &path, path_currently_selected)
            } else {
                panic!("cannot get closure...")
            };
            res.into_glib()
        }
        let func = if func_data.is_some() {
            Some(func_func as _)
        } else {
            None
        };
        unsafe extern "C" fn destroy_func(data: glib::ffi::gpointer) {
            let _callback: Box_<
                Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
            > = Box_::from_raw(data as *mut _);
        }
        let destroy_call3 = Some(destroy_func as _);
        let super_callback0: Box_<
            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
        > = func_data;
        unsafe {
            ffi::gtk_tree_selection_set_select_function(
                self.as_ref().to_glib_none().0,
                func,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call3,
            );
        }
    }

    fn unselect_all(&self) {
        unsafe {
            ffi::gtk_tree_selection_unselect_all(self.as_ref().to_glib_none().0);
        }
    }

    fn unselect_iter(&self, iter: &TreeIter) {
        unsafe {
            ffi::gtk_tree_selection_unselect_iter(
                self.as_ref().to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            );
        }
    }

    fn unselect_path(&self, path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_unselect_path(
                self.as_ref().to_glib_none().0,
                mut_override(path.to_glib_none().0),
            );
        }
    }

    fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_unselect_range(
                self.as_ref().to_glib_none().0,
                mut_override(start_path.to_glib_none().0),
                mut_override(end_path.to_glib_none().0),
            );
        }
    }

    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn changed_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
            this: *mut ffi::GtkTreeSelection,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
        }
        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),
            )
        }
    }

    fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_mode_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
            this: *mut ffi::GtkTreeSelection,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::mode\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_mode_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

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