Debian Codegraph @

gtk/src/treemodel.hg

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 <vector>
18
19 #include <gtk/gtk.h>
20 #include <glibmm/interface.h>
21 #include <gtkmm/treeiter.h>
22 #include <gtkmm/treemodelcolumn.h>
23
24 _DEFS(gtkmm,gtk)
25 _PINCLUDE(glibmm/private/interface_p.h)
26
27 namespace Gtk
28 {
29
30 class GTKMM_API TreeModelFilter;
31 class GTKMM_API TreeModelSort;
32 class GTKMM_API TreePath;
33 class GTKMM_API TreeRowReference;
34
35 _CC_INCLUDE(gtk/gtk.h)
36 _WRAP_ENUM(TreeModelFlags, GtkTreeModelFlags, decl_prefix GTKMM_API)
37
38 //TODO: Remove any mention of null arguments from the method documentation, by adding overrides in gtk_docs_override.xml.
39 /** This class defines a generic tree interface for use by the Gtk::TreeView widget.
40 * @ingroup TreeView
41 *
42 * It is is designed to be usable with any appropriate data structure. The
43 * programmer just has to implement this interface on their own data type for
44 * it to be viewable by a Gtk::TreeView widget.
45 *
46 * The model is represented as a hierarchical tree of strongly-typed, columned
47 * data. In other words, the model can be seen as a tree where every node has
48 * different values depending on which column is being queried. The type of
49 * data found in a column is determined by TreeModel::Column<> templates.
50 * The types are homogeneous per column across all nodes. It is important to note that this
51 * interface only provides a way of examining a model and observing changes.
52 * The implementation of each individual model decides how and if changes are
53 * made.
54 *
55 * In order to make life simpler for programmers who do not need to write their
56 * own specialized model, two generic models are provided - the Gtk::TreeStore
57 * and the Gtk::ListStore. To use these, the developer simply pushes data into
58 * these models as necessary. These models provide the data structure as well
59 * as all appropriate tree interfaces. As a result, implementing drag and drop,
60 * sorting, and storing data is trivial. For the vast majority of trees and
61 * lists, these two models are sufficient.
62 *
63 * Models are accessed on a node/column level of granularity. One can query for
64 * the value of a model at a certain node and a certain column on that node.
65 * There are two structures used to reference a particular node in a model.
66 * They are the @link Gtk::TreePath Path@endlink and the iterator. Most of the interface consists of
67 * operations on an @link Gtk::TreeIter iterator@endlink.
68 *
69 * A @link Gtk::TreePath Gtk::TreeModel::Path@endlink is essentially a potential node. It is a location on a model that may
70 * or may not actually correspond to a node on a specific model.
71 *
72 * By contrast, an @link Gtk::TreeIter Gtk::TreeModel::iterator@endlink is a reference to a specific node on a specific
73 * model. One can convert a path to an @link Gtk::TreeIter iterator@endlink by calling Gtk::TreeModel::get_iter().
74 * These iterators are the primary way of accessing a model and are similar to the iterators
75 * used by Gtk::TextBuffer. The model interface defines a set of operations
76 * using them for navigating the model.
77 *
78 * The @link Gtk::TreeRowReference RowReference@endlink is also useful, because it remains
79 * valid as long as there is an existing row pointed to by it's path. You can convert between RowReferences and iterators and @link Gtk::TreePath Path@endlink s.
80 */
81 class GTKMM_API TreeModel : public Glib::Interface
82 {
83 _CLASS_INTERFACE(TreeModel, GtkTreeModel, GTK_TREE_MODEL, GtkTreeModelIface, , , GTKMM_API)
84 _IGNORE(
85 gtk_tree_model_iter_parent, gtk_tree_model_get_iter, gtk_tree_model_get,
86 gtk_tree_model_get_iter_from_string, gtk_tree_model_get_string_from_iter,
87 gtk_tree_model_iter_nth_child,
88 gtk_tree_model_ref_node, gtk_tree_model_get_valist,
89 gtk_tree_model_iter_next, gtk_tree_model_iter_previous, gtk_tree_model_iter_has_child,
90 gtk_tree_model_unref_node, gtk_tree_model_iter_children,
91 gtk_tree_model_iter_n_children, gtk_tree_model_get_iter_first,
92 gtk_tree_model_get_value)
93
94 public:
95 typedef TreeModelColumnRecord ColumnRecord;
96
97 typedef TreeNodeChildren Children;
98 typedef Children::iterator iterator;
99 typedef Children::const_iterator const_iterator;
100
101 // Reverse iterator: https://bugzilla.gnome.org/show_bug.cgi?id=554889
102 _DEPRECATE_IFDEF_START
103 /** @deprecated This reverse_iterator has never worked,
104 * use the normal (forward) iterator instead.
105 */
106 typedef Children::reverse_iterator reverse_iterator;
107
108 /** @deprecated This reverse_iterator has never worked,
109 * use the normal (forward) iterator instead.
110 */
111 typedef Children::const_reverse_iterator const_reverse_iterator;
112 _DEPRECATE_IFDEF_END
113
114 typedef TreeRow Row;
115 typedef TreePath Path;
116 typedef TreeRowReference RowReference;
117
118
119 //These are part of GtkTreeModelFilter or GtkTreeModelSort, not GtkTreeModel:
120 _IGNORE(gtk_tree_model_filter_new, gtk_tree_model_sort_new_with_model)
121
122 /** Returns a valid iterator pointing to @a path.
123 *
124 * @param path The @link Gtk::TreePath Gtk::TreeModel::Path@endlink.
125 * @result A valid iterator pointing to the path, or an invalid iterator if that is not possible.
126 */
127 iterator get_iter(const Path& path);
128 //TODO: Add const_iterator get_iter(const Path& path) const;
129 //Implement a const_iterator?
130
131 /** Returns a valid iterator pointing to @a path_string.
132 *
133 * @param path_string The path, as a string representation.
134 * @result A valid iterator pointing to the path, or an invalid iterator if that is not possible.
135 */
136 iterator get_iter(const Glib::ustring& path_string);
137 //TODO: Implement a const_iterator? const_iterator get_iter(const Glib::ustring& path_string) const;
138
139 ///This returns an STL-like container API, for iterating over the rows.
140 Children children();
141
142 //TODO: Return a real TreeNodeChildren (a container of const_iterators), when we have a real const_iterator.
143 ///This returns an STL-like container API, for iterating over the rows.
144 Children children() const;
145
146 _IGNORE(gtk_tree_model_foreach)
147
148 /** The type of callback slot used by foreach_iter().
149 *
150 * If the callback function returns true, then the tree ceases to be walked,
151 * and foreach_iter() returns.
152 *
153 * For example: bool on_foreach_iter(const Gtk::TreeModel::iterator& iter);
154 */
155 typedef sigc::slot<bool, const TreeModel::iterator&> SlotForeachIter;
156
157 /** Calls a callback of type @link Gtk::TreeModel::SlotForeachIter
158 * SlotForeachIter @endlink on each node in the model in a depth-first
159 * fashion.
160 *
161 * If the callback function returns true, then the tree ceases to be walked,
162 * and foreach_iter() returns.
163 *
164 * @param slot The function to call for each selected node.
165 */
166 void foreach_iter(const SlotForeachIter& slot);
167
168 /** The type of callback slot used by foreach_path().
169 *
170 * For example: bool on_foreach_path(const Gtk::TreeModel::Path& path);
171 *
172 * If the callback function returns true, then the tree ceases to be walked,
173 * and foreach_path() returns.
174 */
175 typedef sigc::slot<bool, const TreeModel::Path&> SlotForeachPath;
176
177 /** Calls a callback of type @link Gtk::TreeModel::SlotForeachPath
178 * SlotForeachPath @endlink on each node in the model in a depth-first
179 * fashion.
180 *
181 * If the callback function returns true, then the tree ceases to be walked,
182 * and foreach_path() returns.
183 *
184 * @param slot The function to call for each selected node.
185 */
186 void foreach_path(const SlotForeachPath& slot);
187
188 /** The type of callback slot used by foreach().
189 *
190 * For example: bool on_foreach(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter);
191 *
192 * If the callback function returns true, then the tree ceases to be walked,
193 * and foreach() returns.
194 */
195 typedef sigc::slot<bool, const TreeModel::Path&, const TreeModel::iterator&> SlotForeachPathAndIter;
196
197 /** Calls a callback of type @link Gtk::TreeModel::SlotForeachPathAndIter
198 * SlotForeachPathAndIter @endlink on each node in the model in a depth-first
199 * fashion.
200 *
201 * If the callback function returns true, then the tree ceases to be walked,
202 * and foreach() returns.
203 *
204 * @param slot The function to call for each selected node.
205 */
206 void foreach(const SlotForeachPathAndIter& slot);
207
208 _WRAP_METHOD(TreeModelFlags get_flags() const, gtk_tree_model_get_flags)
209 _WRAP_METHOD(int get_n_columns() const, gtk_tree_model_get_n_columns)
210 _WRAP_METHOD(GType get_column_type(int index) const, gtk_tree_model_get_column_type)
211 //TODO: A C++-type version of get_column_type()?
212
213 _WRAP_METHOD(TreeModel::Path get_path(const iterator& iter) const, gtk_tree_model_get_path)
214
215 _WRAP_METHOD(void row_changed(const Path& path, const iterator& iter), gtk_tree_model_row_changed)
216 _WRAP_METHOD(void row_inserted(const Path& path, const iterator& iter), gtk_tree_model_row_inserted)
217 _WRAP_METHOD(void row_has_child_toggled(const Path& path, const iterator& iter), gtk_tree_model_row_has_child_toggled)
218 _WRAP_METHOD(void row_deleted(const Path& path), gtk_tree_model_row_deleted)
219
220 /** Emits the "rows_reordered" signal on the tree model. This should be called by
221 * custom models when their rows have been reordered.
222 *
223 * @param path A tree path pointing to the tree node whose children have been reordered.
224 * @param iter A valid iterator pointing to the node whose children have been reordered. See also, rows_reordered(const Path& path, const std::vector<int>& new_order), if the path has a depth of 0.
225 * @param new_order An array of integers mapping the current position of each child
226 * to its old position before the re-ordering, i.e. @a new_order<literal>[newpos] = oldpos.
227 */
228 void rows_reordered(const Path& path, const iterator& iter, const std::vector<int>& new_order);
229 _IGNORE(gtk_tree_model_rows_reordered_with_length)
230
231 /** Emits the "rows_reordered" signal on the tree model. This should be called by
232 * custom models when their rows have been reordered. This method overload is for nodes whose
233 * path has a depth of 0.
234 * @newin{2,10}
235 *
236 * @param path A tree path pointing to the tree node whose children have been reordered.
237 * @param new_order An array of integers mapping the current position of each child
238 * to its old position before the re-ordering, i.e. @a new_order<literal>[newpos] = oldpos.
239 */
240 void rows_reordered(const Path& path, const std::vector<int>& new_order);
241
242 _WRAP_METHOD(void rows_reordered(const Path& path, const iterator& iter, int* new_order), gtk_tree_model_rows_reordered)
243
244 _WRAP_METHOD(Glib::ustring get_string(const iterator& iter) const, gtk_tree_model_get_string_from_iter)
245
246 #m4 _CONVERSION(`GtkTreeIter*',`const iterator&',`Gtk::TreeModel::iterator(self, $3)')
247 #m4 _CONVERSION(`GtkTreeIter*',`const TreeModel::iterator&',`Gtk::TreeModel::iterator(self, $3)')
248 _WRAP_SIGNAL(void row_changed(const TreeModel::Path& path, const TreeModel::iterator& iter), "row_changed")
249 _WRAP_SIGNAL(void row_inserted(const TreeModel::Path& path, const TreeModel::iterator& iter), "row_inserted")
250 _WRAP_SIGNAL(void row_has_child_toggled(const TreeModel::Path& path, const TreeModel::iterator& iter), "row_has_child_toggled")
251 _WRAP_SIGNAL(void row_deleted(const TreeModel::Path& path), "row_deleted")
252 _WRAP_SIGNAL(void rows_reordered(const TreeModel::Path& path, const TreeModel::iterator& iter, int* new_order), "rows_reordered")
253
254 protected:
255 _WRAP_VFUNC(TreeModelFlags get_flags() const, get_flags)
256 _WRAP_VFUNC(int get_n_columns() const, get_n_columns)
257 _WRAP_VFUNC(GType get_column_type(int index) const, get_column_type)
258
259 //These are only for deriving new TreeModels, which isn't very common or easy:
260
261 /** Override and implement this in a derived TreeModel class.
262 * Sets @a iter_next to refer to the node following @a iter it at the current level.
263 * If there is no next iter, false is returned and iter_next is set to be invalid.
264 *
265 * @param iter An iterator.
266 * @param iter_next An iterator that will be set to refer to the next node, or will be set as invalid.
267 * @result true if the operation was possible.
268 */
269 virtual bool iter_next_vfunc(const iterator& iter, iterator& iter_next) const;
270
271 /** Override and implement this in a derived TreeModel class.
272 * Sets @a iter to a valid iterator pointing to @a path
273 *
274 * @param path An path to a node.
275 * @param iter An iterator that will be set to refer to a node to the path, or will be set as invalid.
276 * @result true if the operation was possible.
277 */
278 virtual bool get_iter_vfunc(const Path& path, iterator& iter) const;
279
280 /** Override and implement this in a derived TreeModel class.
281 * Sets @a iter to refer to the first child of @a parent. If @a parent has no children,
282 * false is returned and @a iter is set to be invalid.
283 *
284 * @param parent An iterator.
285 * @param iter An iterator that will be set to refer to the firt child node, or will be set as invalid.
286 * @result true if the operation was possible.
287 */
288 virtual bool iter_children_vfunc(const iterator& parent, iterator& iter) const;
289
290 /** Override and implement this in a derived TreeModel class.
291 * Sets @a iter to be the parent of @a child. If @a child is at the toplevel, and
292 * doesn't have a parent, then @a iter is set to an invalid iterator and false
293 * is returned.
294 *
295 * @param child An iterator.
296 * @param iter An iterator that will be set to refer to the parent node, or will be set as invalid.
297 * @result true if the operation was possible.
298 */
299 virtual bool iter_parent_vfunc(const iterator& child, iterator& iter) const;
300
301 /** Override and implement this in a derived TreeModel class.
302 * Sets @a iter to be the child of @a parent using the given index. The first
303 * index is 0. If @a n is too big, or @a parent has no children, @a iter is set
304 * to an invalid iterator and false is returned.
305 * See also iter_nth_root_child_vfunc()
306 *
307 * @param parent An iterator.
308 * @param n The index of the child node to which @a iter should be set.
309 * @param iter An iterator that will be set to refer to the nth node, or will be set as invalid.
310 * @result true if the operation was possible.
311 */
312 virtual bool iter_nth_child_vfunc(const iterator& parent, int n, iterator& iter) const;
313
314 /** Override and implement this in a derived TreeModel class.
315 * Sets @a iter to be the child of at the root level using the given index. The first
316 * index is 0. If @a n is too big, or if there are no children, @a iter is set
317 * to an invalid iterator and false is returned.
318 * See also iter_nth_child_vfunc().
319 *
320 * @param n The index of the child node to which @a iter should be set.
321 * @param iter An iterator that will be set to refer to the nth node, or will be set as invalid.
322 * @result true if the operation was possible.
323 */
324 virtual bool iter_nth_root_child_vfunc(int n, iterator& iter) const;
325
326
327
328 #m4begin
329 dnl// We want to hand-code these C vfunc callbacks,
330 dnl// so that we can do forward them to C++ methods with different parameters and return types.
331 dnl
332 _PUSH(SECTION_PCC_CLASS_INIT_VFUNCS)
333 klass->iter_next = &iter_next_vfunc_callback;
334 klass->get_iter = &get_iter_vfunc_callback;
335 klass->iter_children = &iter_children_vfunc_callback;
336 klass->iter_parent = &iter_parent_vfunc_callback;
337 klass->iter_nth_child = &iter_nth_child_vfunc_callback;
338 klass->iter_n_children = &iter_n_children_vfunc_callback;
339 _SECTION(SECTION_PH_VFUNCS)
340 static gboolean iter_next_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter);
341 static gboolean get_iter_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreePath* path);
342 static gboolean iter_children_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* parent);
343 static gboolean iter_parent_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* child);
344 static gboolean iter_nth_child_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* parent, int n);
345 static int iter_n_children_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter);
346 _POP()
347 #m4end
348
349 /** Override and implement this in a derived TreeModel class.
350 * Returns true if @a iter has children, false otherwise.
351 *
352 * @param iter The iterator to test for children.
353 * @result true if @a iter has children.
354 */
355 _WRAP_VFUNC(bool iter_has_child(const iterator& iter) const, iter_has_child)
356
357 /** Override and implement this in a derived TreeModel class.
358 * Returns the number of children that @a iter has.
359 * See also iter_n_root_children_vfunc().
360 *
361 * @param iter The iterator to test for children.
362 * @result The number of children of @a iter.
363 */
364 virtual int iter_n_children_vfunc(const iterator& iter) const;
365
366 /** Override and implement this in a derived TreeModel class.
367 * Returns the number of toplevel nodes.
368 * See also iter_n_children().
369 *
370 * @result The number of children at the root level.
371 */
372 virtual int iter_n_root_children_vfunc() const;
373
374 /** Override and implement this in a derived TreeModel class.
375 * Lets the tree ref the node. This is an optional method for models to
376 * implement. To be more specific, models may ignore this call as it exists
377 * primarily for performance reasons.
378 *
379 * This function is primarily meant as a way for views to let caching model know
380 * when nodes are being displayed (and hence, whether or not to cache that
381 * node.) For example, a file-system based model would not want to keep the
382 * entire file-hierarchy in memory, just the sections that are currently being
383 * displayed by every current view.
384 *
385 * A model should be expected to be able to get an iter independent of its
386 * reffed state.
387 *
388 * @param iter the iterator.
389 */
390 _WRAP_VFUNC(void ref_node(const iterator& iter) const, ref_node)
391
392 /** Override and implement this in a derived TreeModel class.
393 * Lets the tree unref the node. This is an optional method for models to
394 * implement. To be more specific, models may ignore this call as it exists
395 * primarily for performance reasons.
396 *
397 * For more information on what this means, see unref_node_vfunc().
398 * Please note that nodes that are deleted are not unreffed.
399 *
400 * @param iter the iterator.
401 */
402 _WRAP_VFUNC(void unref_node(const iterator& iter) const, unref_node)
403
404 /** Override and implement this in a derived TreeModel class.
405 * Returns a Path referenced by @a iter.
406 *
407 * @param iter The iterator.
408 * @result The path.
409 */
410 #m4 _CONVERSION(`TreeModel::Path', `GtkTreePath*', `($3).gobj_copy()')
411 _WRAP_VFUNC(TreeModel::Path get_path(const iterator& iter) const, get_path)
412
413 /** Override and implement this in a derived TreeModel class.
414 * Initializes and sets @a value to that at @a column.
415 *
416 * @param iter The iterator.
417 * @param column The column to lookup the value at.
418 * @param value An empty Glib:Value to set.
419 */
420 _WRAP_VFUNC(void get_value(const iterator& iter, int column, Glib::ValueBase& value) const, get_value)
421
422 //Called by TreeRow, which is a friend class:
423 //The comment about row_changed() in the documentation is based on my reading of the source of
424 //gtk_list_store_set_value() and gtk_tree_store_set_value(). murrayc
425 /** Override and implement this in a derived TreeModel class, so that Row::operator() and
426 * Row::set_value() work.
427 * Your implementation of set_value_impl() should call row_changed() after changing the value.
428 */
429 virtual void set_value_impl(const iterator& row, int column, const Glib::ValueBase& value);
430
431 //This might not need to be virtual, but it's not a big deal. murrayc.
432 virtual void get_value_impl(const iterator& row, int column, Glib::ValueBase& value) const;
433
434 friend class GTKMM_API Gtk::TreeModelFilter;
435 friend class GTKMM_API Gtk::TreeModelSort;
436 friend class GTKMM_API Gtk::TreeRow;
437 friend class GTKMM_API Gtk::TreeIter;
438 };
439
440 } // namespace Gtk