Debian Codegraph @

gtk/src/treeiter.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 _CONFIGINCLUDE(gtkmmconfig.h)
18 _DEFS(gtkmm,gtk)
19
20 #include <gtkmm/treemodelcolumn.h>
21 #include <iterator>
22 #include <gtk/gtk.h> /* for GtkTreeIter */
23 #ifdef GLIBMM_HAVE_SUN_REVERSE_ITERATOR
24 #include <cstddef> /* for std::ptrdiff_t */
25 #endif
26
27 namespace Gtk
28 {
29
30 class GTKMM_API TreeModel;
31 class GTKMM_API TreeRow;
32 class GTKMM_API TreeNodeChildren;
33
34 /**
35 * @ingroup TreeView
36 */
37 class GTKMM_API TreeIterBase
38 {
39 _CLASS_BOXEDTYPE_STATIC(TreeIterBase, GtkTreeIter, GTKMM_API)
40 _IGNORE(gtk_tree_iter_copy, gtk_tree_iter_free)
41 _NO_WRAP_FUNCTION() //A wrap() for TreeIterBase* wouldn't be very helpful.
42 };
43
44
45 // In order to offer STL-like iterator functionality, we cannot wrap
46 // GtkTreeIter directly. Most GTK+ functions that operate on GtkTreeIter
47 // are virtual functions in GtkTreeModel. Therefore, the C++ TreeIter
48 // must store a pointer to the Gtk::TreeModel to which it belongs.
49 //
50 // Another problem, which is much worse, is that the GTK+ tree iterator
51 // doesn't support the STL-style half-open interval [begin,end). Instead,
52 // it uses a [first,last] interval, and functions return FALSE to indicate
53 // the end was reached. Also, some functions accept a NULL GtkTreeIter*,
54 // which will be interpreted as the end() iterator.
55 //
56 // Most of the immense complexity in the Gtk::TreeIter implementation is
57 // needed for proper emulation of [begin,end) intervals. Unfortunately,
58 // it's not even possible to encapsulate everything in the TreeIter
59 // class. Almost all wrapper methods dealing with GtkTreeIter must be
60 // carefully implemented by hand. TODO: document implementation details
61
62 //TODO: Implement a const_iterator too:
63 //danielk says that this ConstTreeIter class should return a ConstTreeRow, which would not allow operator=.
64
65 /** A Gtk::TreeModel::iterator is a reference to a specific node on a specific
66 * model.
67 *
68 * It is a generic structure with an integer and three generic pointers.
69 * These are filled in by the model in a model-specific way.
70 *
71 * One can convert a path to an iterator by calling Gtk::TreeModel::get_iter().
72 *
73 * These iterators are the primary way of accessing a model and are similar to the iterators
74 * used by Gtk::TextBuffer. The model interface defines a set of operations
75 * using them for navigating the model.
76 *
77 * The lifecycle of an iterator can be a little confusing at first. Iterators
78 * are expected to always be valid for as long as the model is unchanged (and
79 * doesn't emit a signal).
80 * Additionally, some models guarantee that an iterator is valid for as
81 * long as the node it refers to is valid (most notably the Gtk::TreeStore and
82 * Gtk::ListStore).
83 *
84 * Although generally uninteresting, as one always has to
85 * allow for the case where iterators do not persist beyond a signal, some very
86 * important performance enhancements were made in the sort model. As a result,
87 * the Gtk::TREE_MODEL_ITERS_PERSIST flag was added to indicate this behaviour -
88 * see Gtk::TreeModel::get_flags().
89 *
90 * Typedefed as Gtk::TreeModel::iterator.
91 * The Gtk::TreeModel iterator.
92 * @ingroup TreeView
93 */
94 class GTKMM_API TreeIter : public TreeIterBase
95 {
96 public:
97 typedef std::bidirectional_iterator_tag iterator_category;
98 typedef Gtk::TreeRow value_type;
99 typedef int difference_type;
100 typedef const Gtk::TreeRow& reference;
101 typedef const Gtk::TreeRow* pointer;
102
103 TreeIter();
104
105 TreeIter& operator++();
106 const TreeIter operator++(int);
107
108 /** Please note that this is very slow compared to operator++().
109 */
110 TreeIter& operator--();
111
112 /** Please note that this is very slow compared to operator++().
113 */
114 const TreeIter operator--(int);
115
116 inline reference operator*() const;
117 inline pointer operator->() const;
118
119 bool equal(const TreeIter& other) const;
120
121 _DEPRECATE_IFDEF_START
122 /** This typedef is just to make it more obvious that
123 * our operator const void* should be used like operator bool().
124 *
125 * @deprecated Use the explicit operator bool() instead.
126 */
127 typedef const void* BoolExpr;
128
129 /** Discover whether the iterator is valid, and not equal to end().
130 * For instance,
131 * @code
132 * if(treeiter)
133 * do_something()
134 * @endcode
135 *
136 * @deprecated Use the explicit operator bool() instead.
137 */
138 operator BoolExpr() const;
139 _DEPRECATE_IFDEF_END
140
141 /** Discover whether the iterator is valid, and not equal to end().
142 * For instance,
143 * @code
144 * if(treeiter)
145 * do_something()
146 * @endcode
147 *
148 * @newin{3,22}
149 */
150 explicit operator bool() const;
151
152 /** This is only useful when implementing a custom Gtk::TreeModel class.
153 * Compare the iterator's stamp with your model's stamp to discover whether it is valid.
154 * @see set_stamp().
155 * @result The iterator's stamp.
156 */
157 int get_stamp() const;
158
159 /** This is only useful when implementing a custom Gtk::TreeModel class.
160 * Set the stamp to be equal to your model's stamp, to mark the iterator as valid.
161 * When your model's structure changes, you should increment your model's stamp
162 * to mark all older iterators as invalid. They will be recognised as invalid because
163 * they will then have an incorrect stamp.
164 */
165 void set_stamp(int stamp);
166
167 #ifndef DOXYGEN_SHOULD_SKIP_THIS
168
169 explicit TreeIter(TreeModel* model); // used in TreeModel methods
170 TreeIter(GtkTreeModel* model, const GtkTreeIter* iter); // used by signal proxies
171
172 void set_model_refptr(const Glib::RefPtr<TreeModel>& model);
173 void set_model_gobject(GtkTreeModel* model);
174 GtkTreeModel* get_model_gobject() const;
175
176 void setup_end_iterator(const TreeIter& last_valid);
177
178 const GtkTreeIter* get_gobject_if_not_end() const
179 { return (!is_end_) ? &gobject_ : nullptr; }
180
181 const GtkTreeIter* get_parent_gobject_if_end() const
182 { return (is_end_ && gobject_.stamp) ? &gobject_ : nullptr; }
183
184 protected:
185
186 // Yes, using a simple TreeModel* rather than Glib::RefPtr<TreeModel>
187 // violates the general policy. But Gtk::TreeIter should have a trivial
188 // copy constructor and assignment operator, i.e. it must contain only
189 // POD (plain old data).
190 //
191 // Gtk::TreeIter is copied a lot, particularly often as return value from
192 // methods. Postfix ++ must return by value, and STL algorithms usually
193 // pass iterators by value, too. With a RefPtr<> as member data, copying
194 // would no longer be trivial, and even cause frequent calls to reference()
195 // and unreference(). That usually doesn't matter much for GUI stuff, but
196 // Gtk::TreeModel is used as a generic container. Imagine a for-loop that
197 // checks whether iter != children.end() on each iteration.
198
199 TreeModel* model_;
200 bool is_end_;
201
202 friend class GTKMM_API Gtk::TreeRow;
203 friend class GTKMM_API Gtk::TreeNodeChildren;
204 friend class GTKMM_API Gtk::TreeModel;
205
206 #endif // DOXYGEN_SHOULD_SKIP_THIS
207 };
208
209 /** @relates Gtk::TreeIter */
210 inline bool operator==(const TreeIter& lhs, const TreeIter& rhs)
211 { return lhs.equal(rhs); }
212
213 /** @relates Gtk::TreeIter */
214 inline bool operator!=(const TreeIter& lhs, const TreeIter& rhs)
215 { return !lhs.equal(rhs); }
216
217
218 template <class ColumnType>
219 class TreeValueProxy
220 {
221 public:
222 #ifndef DOXYGEN_SHOULD_SKIP_THIS
223 inline TreeValueProxy(const TreeRow& row, const TreeModelColumn<ColumnType>& column);
224 TreeValueProxy(const TreeValueProxy& other) = default;
225 #endif
226
227 inline TreeValueProxy<ColumnType>& operator=(const ColumnType& data);
228 inline operator ColumnType() const;
229
230 private:
231 const TreeRow& row_;
232 const TreeModelColumn<ColumnType>& column_;
233
234 // no copy assignment
235 TreeValueProxy<ColumnType>& operator=(const TreeValueProxy<ColumnType>&) = delete;
236 };
237
238
239 /** Typedefed as TreeModel::Row.
240 *
241 * Dereference a TreeModel::iterator to get the Row. Use operator[] or set_value() and get_value() to access the
242 * values in the columns of this row.
243 *
244 * If the model contains a hierarchy of rows (such as Gtk::TreeStore), then you can access the child rows with
245 * children().
246 *
247 * You can use a const TreeModel::Row& for any parameter that takes a const TreeModel::iterator&.
248 * @ingroup TreeView
249 */
250 class GTKMM_API TreeRow : public TreeIter //We use public inheritance so that we can cast from a TreeRow to a TreeIter.
251 {
252 public:
253
254 /** Use this to set and get the value of this @a column of this row.
255 * This is a templated method, so the compiler will not allow you to provide an inappropriate type
256 * of data for the model column.
257 *
258 * This is just a more convenient syntax that does the same thing as set_value() and get_value().
259 *
260 * @param column The model column..
261 */
262 template <class ColumnType> inline
263 TreeValueProxy<ColumnType> operator[](const TreeModelColumn<ColumnType>& column) const;
264
265 /** Sets the value of this @a column of this row.
266 * This is a templated method, so the compiler will not allow you to provide an inappropriate type
267 * of @a data for the model column.
268 *
269 * See also operator[].
270 *
271 * @param column The model column.
272 * @param data The new value to use for this column of this row.
273 */
274 template <class ColumnType>
275 void set_value(const TreeModelColumn<ColumnType>& column, const ColumnType& data) const;
276
277 /** Use set_value(const TreeModelColumn<>& column, const ColumnType& data) unless
278 * you do not know the column type at compile-time.
279 * If the @a data is of an inappropriate C++ type then this might fail at runtime.
280 * @param column The number of the column whose value you want to change.
281 * @param data The new value to use for this column of this row.
282 */
283 template <class ColumnType>
284 void set_value(int column, const ColumnType& data) const;
285
286 /** Gets the value of this @a column of this row.
287 * This is a templated method, so the compiler will not allow you to provide an inappropriate type
288 * of data for the model column.
289 *
290 * See also operator[].
291 *
292 * @param column The model column.
293 * @result The value of this column of this row
294 */
295 template <class ColumnType>
296 ColumnType get_value(const TreeModelColumn<ColumnType>& column) const;
297
298 /** Use get_value(const TreeModelColumn<>& column) unless
299 * you do not know the column type at compile-time.
300 * If the @a data output argument is of an inappropriate C++ type then this might fail at runtime.
301 * @param column The number of the column whose value you want to query.
302 * @param[out] data An output argument which will contain the value of this column of this row.
303 */
304 template <class ColumnType>
305 void get_value(int column, ColumnType& data) const;
306
307 /** This returns an STL-like container API, for iterating over the rows.
308 * See also Gtk::TreeModel::children() for the top-level children.
309 */
310 const TreeNodeChildren& children() const;
311
312 /** Gets an iterator to the parent row of this row.
313 * @result An iterator to the parent row.
314 */
315 TreeIter parent() const;
316
317 _DEPRECATE_IFDEF_START
318 /** This typedef is just to make it more obvious that
319 * our operator const void* should be used like operator bool().
320 *
321 * @deprecated Use the explicit operator bool() instead.
322 */
323 typedef const void* BoolExpr;
324
325 /** Discover whether this is a valid row.
326 * For instance,
327 * @code
328 * if(treeiter)
329 * do_something()
330 * @endcode
331 *
332 * @deprecated Use the explicit operator bool() instead.
333 */
334 operator BoolExpr() const;
335 _DEPRECATE_IFDEF_END
336
337 /** Discover whether this is a valid row.
338 * For instance,
339 * @code
340 * if(treeiter)
341 * do_something()
342 * @endcode
343 *
344 * @newin{3,22}
345 */
346 explicit operator bool() const;
347
348 /// Provides access to the underlying C GObject.
349 GtkTreeIter* gobj() { return TreeIter::gobj(); }
350
351 /// Provides access to the underlying C GObject.
352 const GtkTreeIter* gobj() const { return TreeIter::gobj(); }
353
354 private:
355 // Forwarders to Gtk::TreeModel virtual methods.
356 void set_value_impl(int column, const Glib::ValueBase& value) const;
357 void get_value_impl(int column, Glib::ValueBase& value) const;
358 };
359
360
361 //TODO: Document begin(), end(), size(), etc, in an STL-style way. murrayc.
362
363 /** typedefed as TreeModel::Children.
364 * Virtual container of TreeModel::Row items.
365 * @ingroup TreeView
366 */
367 class GTKMM_API TreeNodeChildren : public TreeIter
368 {
369 public:
370 typedef Gtk::TreeRow value_type;
371 typedef unsigned int size_type;
372 typedef int difference_type;
373 typedef Gtk::TreeIter iterator;
374 typedef Gtk::TreeIter const_iterator; //TODO: Make it a real const_iterator.
375
376 // Reverse iterator: https://bugzilla.gnome.org/show_bug.cgi?id=554889
377 _DEPRECATE_IFDEF_START
378 #ifndef GLIBMM_HAVE_SUN_REVERSE_ITERATOR
379 /** @deprecated This reverse_iterator has never worked,
380 * use the normal (forward) iterator instead.
381 */
382 typedef std::reverse_iterator<iterator> reverse_iterator;
383 #else
384 /** @deprecated This reverse_iterator has never worked,
385 * use the normal (forward) iterator instead.
386 */
387 typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
388 int, int&, int*, std::ptrdiff_t> reverse_iterator;
389 #endif
390
391 #ifndef GLIBMM_HAVE_SUN_REVERSE_ITERATOR
392 /** @deprecated This reverse_iterator has never worked,
393 * use the normal (forward) iterator instead.
394 */
395 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
396 #else
397 /** @deprecated This reverse_iterator has never worked,
398 * use the normal (forward) iterator instead.
399 */
400 typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
401 int, const int&, const int*, std::ptrdiff_t> const_reverse_iterator;
402 #endif
403 _DEPRECATE_IFDEF_END
404
405 iterator begin();
406 const_iterator begin() const;
407 iterator end();
408 const_iterator end() const;
409
410 _DEPRECATE_IFDEF_START
411 // Note: there is no advantage in not inlining these methods.
412 // We can't change them without breaking ABI anyway.
413 /** @deprecated Use begin() or end() instead.
414 */
415 reverse_iterator rbegin() { return reverse_iterator(end()); }
416 /** @deprecated Use begin() or end() instead.
417 */
418 reverse_iterator rend() { return reverse_iterator(begin()); }
419 /** @deprecated Use begin() or end() instead.
420 */
421 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
422 /** @deprecated Use begin() or end() instead.
423 */
424 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
425 _DEPRECATE_IFDEF_END
426
427 value_type operator[](size_type index) const;
428
429 size_type size() const;
430 bool empty() const;
431
432 _DEPRECATE_IFDEF_START
433 /** This typedef is just to make it more obvious that
434 * our operator const void* should be used like operator bool().
435 *
436 * @deprecated Use the explicit operator bool() instead.
437 */
438 typedef const void* BoolExpr;
439
440 /** Discover whether this is a valid TreeNodeChildren.
441 * For instance,
442 * @code
443 * if(children)
444 * do_something()
445 * @endcode
446 *
447 * @deprecated Use the explicit operator bool() instead.
448 */
449 operator BoolExpr() const
450 {
451 return !empty() ? GINT_TO_POINTER(1) : nullptr;
452 }
453 _DEPRECATE_IFDEF_END
454
455 explicit operator bool() const
456 {
457 return !empty();
458 }
459
460 #ifndef DOXYGEN_SHOULD_SKIP_THIS
461
462 explicit TreeNodeChildren(TreeModel* model)
463 : TreeIter(model) {}
464
465 const GtkTreeIter* get_parent_gobject() const
466 { return (gobject_.stamp != 0) ? &gobject_ : nullptr; }
467
468 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
469 };
470
471
472 #ifndef DOXYGEN_SHOULD_SKIP_THIS
473
474 /**** Gtk::TreeIter ********************************************************/
475
476 inline
477 TreeIter::reference TreeIter::operator*() const
478 {
479 return static_cast<const TreeRow&>(*this);
480 }
481
482 inline
483 TreeIter::pointer TreeIter::operator->() const
484 {
485 return static_cast<const TreeRow*>(this);
486 }
487
488
489 /**** Gtk::TreeValueProxy<> ************************************************/
490
491 template <class ColumnType> inline
492 TreeValueProxy<ColumnType>::TreeValueProxy(const TreeRow& row, const TreeModelColumn<ColumnType>& column)
493 :
494 row_ (row),
495 column_ (column)
496 {}
497
498 template <class ColumnType> inline
499 TreeValueProxy<ColumnType>& TreeValueProxy<ColumnType>::operator=(const ColumnType& data)
500 {
501 row_.set_value(column_, data);
502 return *this;
503 }
504
505 template <class ColumnType> inline
506 TreeValueProxy<ColumnType>::operator ColumnType() const
507 {
508 return row_.get_value(column_);
509 }
510
511
512 /**** Gtk::TreeRow *********************************************************/
513
514 template <class ColumnType> inline
515 TreeValueProxy<ColumnType> TreeRow::operator[](const TreeModelColumn<ColumnType>& column) const
516 {
517 return TreeValueProxy<ColumnType>(*this, column);
518 }
519
520 template <class ColumnType>
521 void TreeRow::set_value(const TreeModelColumn<ColumnType>& column, const ColumnType& data) const
522 {
523 typedef typename Gtk::TreeModelColumn<ColumnType>::ValueType ValueType;
524
525 ValueType value;
526 value.init(column.type());
527
528 value.set(data);
529 this->set_value_impl(column.index(), value);
530 }
531
532 template <class ColumnType>
533 void TreeRow::set_value(int column, const ColumnType& data) const
534 {
535 //This could fail at run-time, because the wrong ColumnType might be used.
536 //It's only for dynamically generated model columns.
537
538 typedef typename Gtk::TreeModelColumn<ColumnType> type_cppcolumn;
539 typedef typename type_cppcolumn::ValueType ValueType;
540
541 ValueType value;
542 value.init(ValueType::value_type());
543
544 value.set(data);
545 this->set_value_impl(column, value);
546 }
547
548 template <class ColumnType>
549 ColumnType TreeRow::get_value(const TreeModelColumn<ColumnType>& column) const
550 {
551 typedef typename Gtk::TreeModelColumn<ColumnType>::ValueType ValueType;
552
553 ValueType value;
554 this->get_value_impl(column.index(), value);
555
556 return value.get();
557 }
558
559 template <class ColumnType>
560 void TreeRow::get_value(int column, ColumnType& data) const
561 {
562 //This could fail at run-time, because the wrong ColumnType might be used.
563 //It's only for dynamically generated model columns.
564
565 typedef typename Gtk::TreeModelColumn<ColumnType>::ValueType ValueType;
566
567 ValueType value;
568 this->get_value_impl(column, value);
569
570 data = value.get();
571 }
572
573 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
574
575 } // namespace Gtk