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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::SelectionMode;
use crate::TreeIter;
use crate::TreeModel;
use crate::TreePath;
use crate::TreeView;
use glib::object::ObjectType as ObjectType_;
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 selection object for GtkTreeView
    ///
    /// 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
    /// gtk_tree_view_get_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 [`TreeSelection`][crate::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 [`TreeSelection`][crate::TreeSelection]::changed signal
    /// when nothing has happened (mostly as a result of programmers calling
    /// select_row on an already selected row).
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkTreeSelection")]
    pub struct TreeSelection(Object<ffi::GtkTreeSelection>);

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

impl TreeSelection {
    /// 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")]
    pub fn count_selected_rows(&self) -> i32 {
        unsafe { ffi::gtk_tree_selection_count_selected_rows(self.to_glib_none().0) }
    }

    /// Gets the selection mode for @self. See
    /// gtk_tree_selection_set_mode().
    ///
    /// # Returns
    ///
    /// the current selection mode
    #[doc(alias = "gtk_tree_selection_get_mode")]
    #[doc(alias = "get_mode")]
    pub fn mode(&self) -> SelectionMode {
        unsafe { from_glib(ffi::gtk_tree_selection_get_mode(self.to_glib_none().0)) }
    }

    /// Sets @iter to the currently selected node if @self is set to
    /// [`SelectionMode::Single`][crate::SelectionMode::Single] or [`SelectionMode::Browse`][crate::SelectionMode::Browse].  @iter may be NULL if you
    /// just want to test if @self has any selected nodes.  @model is filled
    /// with the current model as a convenience.  This function will not work if you
    /// use @self is [`SelectionMode::Multiple`][crate::SelectionMode::Multiple].
    ///
    /// # Returns
    ///
    /// TRUE, if there is a selected node.
    ///
    /// ## `model`
    /// A pointer to set to the [`TreeModel`][crate::TreeModel]
    ///
    /// ## `iter`
    /// The [`TreeIter`][crate::TreeIter]
    #[doc(alias = "gtk_tree_selection_get_selected")]
    #[doc(alias = "get_selected")]
    pub 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.to_glib_none().0,
                &mut model,
                iter.to_glib_none_mut().0,
            ));
            if ret {
                Some((from_glib_none(model), iter))
            } else {
                None
            }
        }
    }

    /// 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 [`TreeRowReference`][crate::TreeRowReference]s.
    /// To do this, you can use gtk_tree_row_reference_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
    ///
    /// A `GList` containing a [`TreePath`][crate::TreePath] for each selected row.
    ///
    /// ## `model`
    /// A pointer to set to the [`TreeModel`][crate::TreeModel]
    #[doc(alias = "gtk_tree_selection_get_selected_rows")]
    #[doc(alias = "get_selected_rows")]
    pub 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.to_glib_none().0, &mut model),
            );
            (ret, from_glib_none(model))
        }
    }

    /// 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")]
    pub fn tree_view(&self) -> TreeView {
        unsafe { from_glib_none(ffi::gtk_tree_selection_get_tree_view(self.to_glib_none().0)) }
    }

    /// 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")]
    pub fn iter_is_selected(&self, iter: &TreeIter) -> bool {
        unsafe {
            from_glib(ffi::gtk_tree_selection_iter_is_selected(
                self.to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            ))
        }
    }

    /// 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")]
    pub fn path_is_selected(&self, path: &TreePath) -> bool {
        unsafe {
            from_glib(ffi::gtk_tree_selection_path_is_selected(
                self.to_glib_none().0,
                mut_override(path.to_glib_none().0),
            ))
        }
    }

    /// Selects all the nodes. @self must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]
    /// mode.
    #[doc(alias = "gtk_tree_selection_select_all")]
    pub fn select_all(&self) {
        unsafe {
            ffi::gtk_tree_selection_select_all(self.to_glib_none().0);
        }
    }

    /// Selects the specified iterator.
    /// ## `iter`
    /// The [`TreeIter`][crate::TreeIter] to be selected.
    #[doc(alias = "gtk_tree_selection_select_iter")]
    pub fn select_iter(&self, iter: &TreeIter) {
        unsafe {
            ffi::gtk_tree_selection_select_iter(
                self.to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            );
        }
    }

    /// Select the row at @path.
    /// ## `path`
    /// The [`TreePath`][crate::TreePath] to be selected.
    #[doc(alias = "gtk_tree_selection_select_path")]
    pub fn select_path(&self, path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_select_path(
                self.to_glib_none().0,
                mut_override(path.to_glib_none().0),
            );
        }
    }

    /// 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")]
    pub fn select_range(&self, start_path: &TreePath, end_path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_select_range(
                self.to_glib_none().0,
                mut_override(start_path.to_glib_none().0),
                mut_override(end_path.to_glib_none().0),
            );
        }
    }

    /// Calls a function for each selected node. Note that you cannot modify
    /// the tree or selection from within this function. As a result,
    /// gtk_tree_selection_get_selected_rows() might be more useful.
    /// ## `func`
    /// The function to call for each selected node.
    #[doc(alias = "gtk_tree_selection_selected_foreach")]
    pub 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.to_glib_none().0,
                func,
                super_callback0 as *const _ as usize as *mut _,
            );
        }
    }

    /// 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")]
    pub fn set_mode(&self, type_: SelectionMode) {
        unsafe {
            ffi::gtk_tree_selection_set_mode(self.to_glib_none().0, type_.into_glib());
        }
    }

    /// 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")]
    pub fn set_select_function<
        P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static,
    >(
        &self,
        func: P,
    ) {
        let func_data: Box_<P> = Box_::new(func);
        unsafe extern "C" fn func_func<
            P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static,
        >(
            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: &P = &*(data as *mut _);
            let res = (*callback)(&selection, &model, &path, path_currently_selected);
            res.into_glib()
        }
        let func = Some(func_func::<P> as _);
        unsafe extern "C" fn destroy_func<
            P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> 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_selection_set_select_function(
                self.to_glib_none().0,
                func,
                Box_::into_raw(super_callback0) as *mut _,
                destroy_call3,
            );
        }
    }

    /// Unselects all the nodes.
    #[doc(alias = "gtk_tree_selection_unselect_all")]
    pub fn unselect_all(&self) {
        unsafe {
            ffi::gtk_tree_selection_unselect_all(self.to_glib_none().0);
        }
    }

    /// Unselects the specified iterator.
    /// ## `iter`
    /// The [`TreeIter`][crate::TreeIter] to be unselected.
    #[doc(alias = "gtk_tree_selection_unselect_iter")]
    pub fn unselect_iter(&self, iter: &TreeIter) {
        unsafe {
            ffi::gtk_tree_selection_unselect_iter(
                self.to_glib_none().0,
                mut_override(iter.to_glib_none().0),
            );
        }
    }

    /// Unselects the row at @path.
    /// ## `path`
    /// The [`TreePath`][crate::TreePath] to be unselected.
    #[doc(alias = "gtk_tree_selection_unselect_path")]
    pub fn unselect_path(&self, path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_unselect_path(
                self.to_glib_none().0,
                mut_override(path.to_glib_none().0),
            );
        }
    }

    /// 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")]
    pub fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath) {
        unsafe {
            ffi::gtk_tree_selection_unselect_range(
                self.to_glib_none().0,
                mut_override(start_path.to_glib_none().0),
                mut_override(end_path.to_glib_none().0),
            );
        }
    }

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

    #[doc(alias = "mode")]
    pub fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_mode_trampoline<F: Fn(&TreeSelection) + 'static>(
            this: *mut ffi::GtkTreeSelection,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(&from_glib_borrow(this))
        }
        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::<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")
    }
}