Debian Codegraph @

gtk/src/treemodel.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 <glibmm/vectorutils.h>
18
19 #include <gtkmm/treeiter.h>
20 #include <gtkmm/treepath.h>
21 #include <gtk/gtk.h>
22
23 namespace
24 {
25
26 static gboolean proxy_foreach_iter_callback(GtkTreeModel* model, GtkTreePath*, GtkTreeIter* iter, void* data)
27 {
28 typedef Gtk::TreeModel::SlotForeachIter SlotType;
29 auto& slot = *static_cast<SlotType*>(data);
30
31 try
32 {
33 return slot(Gtk::TreeModel::iterator(model, iter));
34 }
35 catch(...)
36 {
37 Glib::exception_handlers_invoke();
38 }
39
40 return 1;
41 }
42
43 static gboolean proxy_foreach_path_callback(GtkTreeModel*, GtkTreePath* path, GtkTreeIter*, void* data)
44 {
45 typedef Gtk::TreeModel::SlotForeachPath SlotType;
46 auto& slot = *static_cast<SlotType*>(data);
47
48 try
49 {
50 return slot(Gtk::TreeModel::Path(path, true));
51 }
52 catch(...)
53 {
54 Glib::exception_handlers_invoke();
55 }
56
57 return 1;
58 }
59
60 static gboolean proxy_foreach_path_and_iter_callback(GtkTreeModel* model, GtkTreePath* path,
61 GtkTreeIter* iter, void* data)
62 {
63 typedef Gtk::TreeModel::SlotForeachPathAndIter SlotType;
64 auto& slot = *static_cast<SlotType*>(data);
65
66 try
67 {
68 return slot(Gtk::TreeModel::Path(path, true), Gtk::TreeModel::iterator(model, iter));
69 }
70 catch(...)
71 {
72 Glib::exception_handlers_invoke();
73 }
74
75 return 1;
76 }
77
78 } //anonymous namespace
79
80
81 //Help the code generator, so that it doesn't have to fully qualify this type:
82 typedef Gtk::TreeModel::iterator iterator;
83
84 namespace Gtk
85 {
86
87 TreeModel::iterator TreeModel::get_iter(const Path& path)
88 {
89 Gtk::TreeModel::iterator iter(this);
90 gtk_tree_model_get_iter(gobj(), iter.gobj(), const_cast<GtkTreePath*>(path.gobj()));
91 return iter;
92 }
93
94 TreeModel::iterator TreeModel::get_iter(const Glib::ustring& path_string)
95 {
96 Gtk::TreeModel::iterator iter (this);
97 gtk_tree_model_get_iter_from_string(gobj(), iter.gobj(), path_string.c_str());
98 return iter;
99 }
100
101 TreeModel::Children TreeModel::children()
102 {
103 return TreeNodeChildren(this);
104 }
105
106 TreeModel::Children TreeModel::children() const
107 {
108 //TODO: Remove the const when we have a real const TreeNodeChildren, when we have a real const_iterator.
109 return TreeNodeChildren(const_cast<TreeModel*>(this));
110 }
111
112 void TreeModel::set_value_impl(const iterator&, int, const Glib::ValueBase&)
113 {
114 g_assert_not_reached();
115 }
116
117 void TreeModel::get_value_impl(const iterator& row, int column, Glib::ValueBase& value) const
118 {
119 gtk_tree_model_get_value(
120 const_cast<GtkTreeModel*>(gobj()),
121 const_cast<GtkTreeIter*>(row.gobj()),
122 column, value.gobj());
123 }
124
125 void TreeModel::foreach_iter(const SlotForeachIter& slot)
126 {
127 SlotForeachIter slot_copy (slot);
128 gtk_tree_model_foreach(gobj(), &proxy_foreach_iter_callback, &slot_copy);
129 }
130
131 void TreeModel::foreach_path(const SlotForeachPath& slot)
132 {
133 SlotForeachPath slot_copy (slot);
134 gtk_tree_model_foreach(gobj(), &proxy_foreach_path_callback, &slot_copy);
135 }
136
137 void TreeModel::foreach(const SlotForeachPathAndIter& slot)
138 {
139 SlotForeachPathAndIter slot_copy (slot);
140 gtk_tree_model_foreach(gobj(), &proxy_foreach_path_and_iter_callback, &slot_copy);
141 }
142
143 // Custom vfunc callbacks, because the C++ vfuncs have different parameters and return types
144 // that can not be generated automatically:
145 gboolean TreeModel_Class::iter_next_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter)
146 {
147 const auto obj = dynamic_cast<CppObjectType*>(
148 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
149
150 // Non-gtkmmproc-generated custom classes implicitly call the default
151 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
152 // generated classes can use this optimisation, which avoids the unnecessary
153 // parameter conversions if there is no possibility of the virtual function
154 // being overridden:
155 if(obj && obj->is_derived_())
156 {
157 try // Trap C++ exceptions which would normally be lost because this is a C callback.
158 {
159 // Call the virtual member method, which derived classes might override.
160 auto iter_input = TreeModel::iterator(self, iter);
161 TreeModel::iterator iter_next( self, iter ); //Copies iter by value.
162 gboolean test = obj->iter_next_vfunc(iter_input, iter_next);
163
164 //Copy the new iter value to the C output parameter:
165 if(test)
166 *iter = *(iter_next.gobj());
167
168 return test;
169 }
170 catch(...)
171 {
172 Glib::exception_handlers_invoke();
173 }
174 }
175 else
176 {
177 const auto base = static_cast<BaseClassType*>(
178 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
179 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
180 )
181 );
182
183 // Call the original underlying C function:
184 if(base && base->iter_next)
185 return (*base->iter_next)(self, iter);
186 }
187
188 typedef gboolean RType;
189 return RType();
190 }
191
192
193 bool TreeModel::iter_next_vfunc(const iterator& iter, iterator& iter_next) const
194 {
195 //Call the default C implementation:
196
197 const auto base = static_cast<BaseClassType*>(
198 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
199 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
200 )
201 );
202
203 if(base && base->iter_next)
204 {
205 auto iter_copy = iter;
206 gboolean test = (*base->iter_next)(const_cast<GtkTreeModel*>(gobj()), iter_copy.gobj());
207 if(test)
208 iter_next = iter_copy;
209
210 return test;
211 }
212
213 return bool();
214 }
215
216 gboolean TreeModel_Class::get_iter_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreePath* path)
217 {
218 const auto obj = dynamic_cast<CppObjectType*>(
219 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
220
221 // Non-gtkmmproc-generated custom classes implicitly call the default
222 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
223 // generated classes can use this optimisation, which avoids the unnecessary
224 // parameter conversions if there is no possibility of the virtual function
225 // being overridden:
226 if(obj && obj->is_derived_())
227 {
228 try // Trap C++ exceptions which would normally be lost because this is a C callback.
229 {
230 // Call the virtual member method, which derived classes might override.
231 Gtk::TreeModel::iterator iter_out(self, iter); //copies the iter by value.
232 gboolean test = obj->get_iter_vfunc(Gtk::TreePath(path, true), iter_out);
233
234 //Copy the new iter value to the C output parameter:
235 if(test)
236 *iter = *(iter_out.gobj());
237
238 return test;
239 }
240 catch(...)
241 {
242 Glib::exception_handlers_invoke();
243 }
244 }
245 else
246 {
247 const auto base = static_cast<BaseClassType*>(
248 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
249 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
250 ) );
251
252 // Call the original underlying C function:
253 if(base && base->get_iter)
254 return (*base->get_iter)(self, iter, const_cast<GtkTreePath*>(path));
255 }
256
257 typedef gboolean RType;
258 return RType();
259 }
260
261 bool TreeModel::get_iter_vfunc(const Path& path, iterator& iter) const
262 {
263 //Call the default C implementation:
264
265 const auto base = static_cast<BaseClassType*>(
266 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
267 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
268 )
269 );
270
271 if(base && base->get_iter)
272 {
273 return (*base->get_iter)( const_cast<GtkTreeModel*>(gobj()), iter.gobj(), const_cast<GtkTreePath*>(path.gobj()) );
274 }
275
276 return bool();
277 }
278
279 gboolean TreeModel_Class::iter_children_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* parent)
280 {
281 const auto obj = dynamic_cast<CppObjectType*>(
282 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
283
284 // Non-gtkmmproc-generated custom classes implicitly call the default
285 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
286 // generated classes can use this optimisation, which avoids the unnecessary
287 // parameter conversions if there is no possibility of the virtual function
288 // being overridden:
289 if(obj && obj->is_derived_())
290 {
291 try // Trap C++ exceptions which would normally be lost because this is a C callback.
292 {
293 // Call the virtual member method, which derived classes might override.
294 Gtk::TreeModel::iterator iter_out(self, iter); //copies the iter by value.
295
296 gboolean test = false;
297 if(!parent)
298 {
299 //Deal with this case, which is documented in the C docs as:
300 //" If @parent is %NULL returns the first node, equivalent to
301 // <literal>gtk_tree_model_get_iter_first (tree_model, iter);</literal> "
302 test = obj->iter_nth_root_child_vfunc(0, iter_out);
303 }
304 else
305 {
306 //Normal case:
307 Gtk::TreeModel::iterator parent_cpp(self, parent);
308 test = obj->iter_children_vfunc(parent_cpp, iter_out);
309 }
310
311 //Copy the new iter value to the C output parameter:
312 if(test)
313 *iter = *(iter_out.gobj());
314
315 return test;
316 }
317 catch(...)
318 {
319 Glib::exception_handlers_invoke();
320 }
321 }
322 else
323 {
324 const auto base = static_cast<BaseClassType*>(
325 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
326 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
327 ) );
328
329 // Call the original underlying C function:
330 if(base && base->iter_children)
331 return (*base->iter_children)(self, iter, parent);
332 }
333
334 typedef gboolean RType;
335 return RType();
336 }
337
338 bool TreeModel::iter_children_vfunc(const iterator& parent, iterator& iter) const
339 {
340 //Call the default C implementation:
341
342 const auto base = static_cast<BaseClassType*>(
343 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
344 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
345 )
346 );
347
348 if(base && base->iter_children)
349 {
350 return (*base->iter_children)( const_cast<GtkTreeModel*>(gobj()), iter.gobj(), const_cast<GtkTreeIter*>(parent.gobj()) );
351 }
352
353 return bool();
354 }
355
356 gboolean TreeModel_Class::iter_parent_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* child)
357 {
358 const auto obj = dynamic_cast<CppObjectType*>(
359 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
360
361 // Non-gtkmmproc-generated custom classes implicitly call the default
362 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
363 // generated classes can use this optimisation, which avoids the unnecessary
364 // parameter conversions if there is no possibility of the virtual function
365 // being overridden:
366 if(obj && obj->is_derived_())
367 {
368 try // Trap C++ exceptions which would normally be lost because this is a C callback.
369 {
370 // Call the virtual member method, which derived classes might override.
371 Gtk::TreeModel::iterator iter_out(self, iter);
372 Gtk::TreeModel::iterator child_cpp(self, child);
373 gboolean test = obj->iter_parent_vfunc(child_cpp, iter_out);
374
375 //Copy the new iter value to the C output parameter:
376 if(test)
377 *iter = *(iter_out.gobj());
378
379 return test;
380 }
381 catch(...)
382 {
383 Glib::exception_handlers_invoke();
384 }
385 }
386 else
387 {
388 const auto base = static_cast<BaseClassType*>(
389 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
390 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
391 ) );
392
393 // Call the original underlying C function:
394 if(base && base->iter_parent)
395 return (*base->iter_parent)(self, iter, child);
396 }
397
398 typedef gboolean RType;
399 return RType();
400 }
401
402 bool TreeModel::iter_parent_vfunc(const iterator& child, iterator& iter) const
403 {
404 //Call the default C implementation:
405
406 const auto base = static_cast<BaseClassType*>(
407 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
408 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
409 )
410 );
411
412 if(base && base->iter_parent)
413 {
414 return (*base->iter_parent)( const_cast<GtkTreeModel*>(gobj()), iter.gobj(), const_cast<GtkTreeIter*>(child.gobj()) );
415 }
416
417 return bool();
418 }
419
420 gboolean TreeModel_Class::iter_nth_child_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter, GtkTreeIter* parent, int n)
421 {
422 const auto obj = dynamic_cast<CppObjectType*>(
423 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
424
425 // Non-gtkmmproc-generated custom classes implicitly call the default
426 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
427 // generated classes can use this optimisation, which avoids the unnecessary
428 // parameter conversions if there is no possibility of the virtual function
429 // being overridden:
430 if(obj && obj->is_derived_())
431 {
432 try // Trap C++ exceptions which would normally be lost because this is a C callback.
433 {
434 // Call the virtual member method, which derived classes might override.
435 Gtk::TreeModel::iterator iter_out(self, iter);
436
437 gboolean test = false;
438 if(!parent)
439 {
440 // Deal with this special case, docuemnted in the C docs as:
441 // "As a special case, if @parent is %NULL, then the nth root node is set.":
442 test = obj->iter_nth_root_child_vfunc(n, iter_out);
443 }
444 else
445 {
446 //The normal case:
447 Gtk::TreeModel::iterator parent_cpp(self, parent);
448 test = obj->iter_nth_child_vfunc(parent_cpp, n, iter_out);
449 }
450
451 //Copy the new iter value to the C output parameter:
452 if(test)
453 *iter = *(iter_out.gobj());
454
455 return test;
456 }
457 catch(...)
458 {
459 Glib::exception_handlers_invoke();
460 }
461 }
462 else
463 {
464 const auto base = static_cast<BaseClassType*>(
465 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
466 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
467 ) );
468
469 // Call the original underlying C function:
470 if(base && base->iter_nth_child)
471 return (*base->iter_nth_child)(self, iter, parent, n);
472 }
473
474 typedef gboolean RType;
475 return RType();
476 }
477
478 bool TreeModel::iter_nth_child_vfunc(const iterator& parent, int n, iterator& iter) const
479 {
480 //Call the default C implementation:
481
482 const auto base = static_cast<BaseClassType*>(
483 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
484 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
485 )
486 );
487
488 if(base && base->iter_nth_child)
489 {
490 return (*base->iter_nth_child)( const_cast<GtkTreeModel*>(gobj()), iter.gobj(), const_cast<GtkTreeIter*>(parent.gobj()), n );
491 }
492
493 return bool();
494 }
495
496 //See the implementation of iter_children_vfunc_callback() and iter_nth_child_vfunc_callback()
497 //to find out why this virtual function exists:
498 bool TreeModel::iter_nth_root_child_vfunc(int n, iterator& iter) const
499 {
500 //Call the default C implementation:
501
502 const auto base = static_cast<BaseClassType*>(
503 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
504 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
505 )
506 );
507
508 if(base && base->iter_nth_child)
509 {
510 //This means that iter_nth_child(0) might be called by iter_children_vfunc_callback(),
511 //but hopefully that's exactly the same thing. TODO: Ask why both vfuncs exist. murrayc.
512 return (*base->iter_nth_child)( const_cast<GtkTreeModel*>(gobj()), iter.gobj(), nullptr /* the null parent */, n );
513 }
514
515 return bool();
516 }
517
518 gboolean TreeModel_Class::iter_n_children_vfunc_callback(GtkTreeModel* self, GtkTreeIter* iter)
519 {
520 const auto obj = dynamic_cast<CppObjectType*>(
521 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
522
523 // Non-gtkmmproc-generated custom classes implicitly call the default
524 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
525 // generated classes can use this optimisation, which avoids the unnecessary
526 // parameter conversions if there is no possibility of the virtual function
527 // being overridden:
528 if(obj && obj->is_derived_())
529 {
530 try // Trap C++ exceptions which would normally be lost because this is a C callback.
531 {
532 // Call the virtual member method, which derived classes might override.
533
534 //Deal with the case that iter is null, as described in the C docs:
535 if(iter)
536 return obj->iter_n_children_vfunc( Gtk::TreeModel::iterator(self, iter) );
537 else
538 return obj->iter_n_root_children_vfunc();
539 }
540 catch(...)
541 {
542 Glib::exception_handlers_invoke();
543 }
544 }
545 else
546 {
547 const auto base = static_cast<BaseClassType*>(
548 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
549 g_type_interface_peek(G_OBJECT_GET_CLASS(self), CppObjectType::get_type()) // Get the interface.
550 ) );
551
552 // Call the original underlying C function:
553 if(base && base->iter_n_children)
554 return (*base->iter_n_children)(self, iter);
555 }
556
557 typedef gboolean RType;
558 return RType();
559 }
560
561 int TreeModel::iter_n_children_vfunc(const iterator& iter) const
562 {
563 const auto base = static_cast<BaseClassType*>(
564 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
565 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
566 ) );
567
568 if(base && base->iter_n_children)
569 return (*base->iter_n_children)(const_cast<GtkTreeModel*>(gobj()), const_cast<GtkTreeIter*>((iter).gobj()));
570
571 typedef int RType;
572 return RType();
573 }
574
575 int TreeModel::iter_n_root_children_vfunc() const
576 {
577 const auto base = static_cast<BaseClassType*>(
578 g_type_interface_peek_parent( // Get the parent interface of the interface (The original underlying C interface).
579 g_type_interface_peek(G_OBJECT_GET_CLASS(gobject_), CppObjectType::get_type()) // Get the interface.
580 ) );
581
582 if(base && base->iter_n_children)
583 return (*base->iter_n_children)(const_cast<GtkTreeModel*>(gobj()), nullptr /* null iter to mean root node, as described in C docs */);
584
585 typedef int RType;
586 return RType();
587 }
588
589
590
591 void TreeModel::rows_reordered(const Path& path, const iterator& iter, const std::vector<int>& new_order)
592 {
593 //The size of the array seems to be based on the known number of children. murrayc.
594 gtk_tree_model_rows_reordered(gobj(),
595 const_cast<GtkTreePath*>((path).gobj()),
596 const_cast<GtkTreeIter*>((iter).gobj()),
597 const_cast<int*>(Glib::ArrayHandler<int>::vector_to_array(new_order).data ()));
598 }
599
600 void TreeModel::rows_reordered(const Path& path, const std::vector<int>& new_order)
601 {
602 //The size of the array seems to be based on the known number of children. murrayc.
603 gtk_tree_model_rows_reordered(gobj(),
604 const_cast<GtkTreePath*>((path).gobj()),
605 nullptr,
606 const_cast<int*>(Glib::ArrayHandler<int>::vector_to_array(new_order).data ()));
607 }
608
609 } // namespace Gtk