Debian Codegraph @

gtk/src/treepath.ccg

raw
1 /* Copyright (C) 1998-2002 The gtkmm Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <gtkmm/treemodel.h>
18 #include <glibmm/utility.h>
19 #include <gtk/gtk.h>
20
21
22 namespace Gtk
23 {
24
25 TreePath::TreePath(GtkTreePath* gobject, bool make_a_copy)
26 :
27 // For BoxedType wrappers, make_a_copy is true by default. The static
28 // BoxedType wrappers must always take a copy, thus make_a_copy = true
29 // ensures identical behaviour if the default argument is used.
30 gobject_ (gobject ? (make_a_copy ? gtk_tree_path_copy(gobject) : gobject) : gtk_tree_path_new())
31 {}
32
33 TreePath::TreePath(TreePath::size_type n, TreePath::value_type value)
34 :
35 gobject_(gtk_tree_path_new())
36 {
37 for(; n > 0; --n)
38 gtk_tree_path_append_index(gobject_, value);
39 }
40
41 TreePath::TreePath(const Glib::ustring& path)
42 :
43 gobject_ (gtk_tree_path_new_from_string(path.c_str()))
44 {
45 if (!gobject_)
46 gobject_ = gtk_tree_path_new();
47 }
48
49 TreePath::TreePath(const TreeModel::iterator& iter)
50 :
51 // The GtkTreePath* is always newly created.
52 gobject_ (gtk_tree_model_get_path(iter.get_model_gobject(), const_cast<GtkTreeIter*>(iter.gobj())))
53 {
54 if (!gobject_)
55 gobject_ = gtk_tree_path_new();
56 }
57
58 TreePath& TreePath::operator=(const TreeModel::iterator& iter)
59 {
60 TreePath temp(iter);
61 swap(temp);
62 return *this;
63 }
64
65 void TreePath::clear()
66 {
67 TreePath empty_path;
68 swap(empty_path);
69 }
70
71 TreePath::size_type TreePath::size() const
72 {
73 return gtk_tree_path_get_depth(gobject_);
74 }
75
76 _DEPRECATE_IFDEF_START
77 TreePath::operator const void*() const
78 {
79 return !empty() ? GINT_TO_POINTER(1) : nullptr;
80 }
81 _DEPRECATE_IFDEF_END
82
83 TreePath::operator bool() const
84 {
85 return !empty();
86 }
87
88 bool TreePath::empty() const
89 {
90 return (gtk_tree_path_get_depth(gobject_) == 0);
91 }
92
93 TreePath::reference TreePath::operator[](TreePath::size_type i)
94 {
95 int *const indices = gtk_tree_path_get_indices(gobject_);
96 return indices[i];
97 }
98
99 TreePath::const_reference TreePath::operator[](TreePath::size_type i) const
100 {
101 const int *const indices = gtk_tree_path_get_indices(gobject_);
102 return indices[i];
103 }
104
105 TreePath::iterator TreePath::begin()
106 {
107 return gtk_tree_path_get_indices(gobject_);
108 }
109
110 TreePath::iterator TreePath::end()
111 {
112 return gtk_tree_path_get_indices(gobject_) + gtk_tree_path_get_depth(gobject_);
113 }
114
115 TreePath::const_iterator TreePath::begin() const
116 {
117 return gtk_tree_path_get_indices(gobject_);
118 }
119
120 TreePath::const_iterator TreePath::end() const
121 {
122 return gtk_tree_path_get_indices(gobject_) + gtk_tree_path_get_depth(gobject_);
123 }
124
125 bool TreePath::get_from_selection_data(const SelectionData& selection_data, Glib::RefPtr<TreeModel>& model, TreePath& path) //static
126 {
127 GtkTreeModel* src_model = nullptr;
128 GtkTreePath* src_path = nullptr;
129 gboolean result = gtk_tree_get_row_drag_data(const_cast<GtkSelectionData*>(selection_data.gobj()), &src_model, &src_path);
130
131 model = Glib::wrap(src_model, true /* take_copy=true */);
132
133 //gtk_tree_get_row_drag_data gives us ownership of src_path.
134 path = Glib::wrap(src_path, false /* take_copy=false */);
135
136 return result;
137 }
138
139 bool TreePath::get_from_selection_data(const SelectionData& selection_data, TreePath& path) //static
140 {
141 GtkTreePath* src_path = nullptr;
142 gboolean result = gtk_tree_get_row_drag_data(const_cast<GtkSelectionData*>(selection_data.gobj()), nullptr, &src_path);
143
144 //gtk_tree_get_row_drag_data gives us ownership of src_path.
145 path = Glib::wrap(src_path, false /* take_copy=false */);
146
147 return result;
148 }
149
150 bool TreePath::set_in_selection_data(SelectionData& selection_data, const Glib::RefPtr<const TreeModel>& model) const
151 {
152 return gtk_tree_set_row_drag_data(selection_data.gobj(), const_cast<GtkTreeModel*>(model->gobj()), const_cast<GtkTreePath*>(gobj()));
153 }
154
155 } // namespace Gtk