Debian Codegraph @

gtk/src/treemodelfilter.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 <gtk/gtk.h>
18
19
20 static gboolean SignalProxy_Visible_gtk_callback(GtkTreeModel* model, GtkTreeIter* iter, gpointer data)
21 {
22 auto the_slot = static_cast<Gtk::TreeModelFilter::SlotVisible*>(data);
23
24 try
25 {
26 return (*the_slot)( Gtk::TreeModel::const_iterator(model, iter) );
27 }
28 catch(...)
29 {
30 Glib::exception_handlers_invoke();
31 }
32
33 return FALSE; //An arbitary default, just to avoid the compiler warning.
34 }
35
36 static void SignalProxy_Visible_gtk_callback_destroy(void* data)
37 {
38 delete static_cast<Gtk::TreeModelFilter::SlotVisible*>(data);
39 }
40
41
42 static void SignalProxy_Modify_gtk_callback(GtkTreeModel* model, GtkTreeIter* iter, GValue* value, gint column, gpointer data)
43 {
44 auto the_slot = static_cast<Gtk::TreeModelFilter::SlotModify*>(data);
45
46 try
47 {
48 //Initialize the input parameter with the appropriate type for this column.
49 //Then the C++ handler can just use operator==() without calling init on the value output arg:
50 Glib::ValueBase cppValue;
51 auto column_type = gtk_tree_model_get_column_type(model, column);
52 cppValue.init(column_type);
53
54 (*the_slot)( Gtk::TreeModel::const_iterator(model, iter), cppValue, column );
55
56 //GTK+ has already done this for us: g_value_init(value, column_type);
57
58 //If the C++ handler has inited value with an inappropriate GType, then this will fail,
59 //but they should not do that because it makes no sense.
60 g_value_copy(cppValue.gobj() /* source */, value /* destination */);
61 }
62 catch(...)
63 {
64 Glib::exception_handlers_invoke();
65 }
66 }
67
68 static void SignalProxy_Modify_gtk_callback_destroy(void* data)
69 {
70 delete static_cast<Gtk::TreeModelFilter::SlotModify*>(data);
71 }
72
73
74 typedef Gtk::TreeModel::Path Path; //So that the generated method implemenations can use this a return type.
75
76 namespace Gtk
77 {
78
79 TreeModelFilter::TreeModelFilter(const Glib::RefPtr<TreeModel>& child_model)
80 :
81 _CONSTRUCT("child_model", child_model->gobj())
82 {
83 }
84
85 TreeModelFilter::TreeModelFilter(const Glib::RefPtr<TreeModel>& child_model, const TreeModel::Path& virtual_root)
86 :
87 _CONSTRUCT("child_model", child_model->gobj(), "virtual_root", (virtual_root.empty() ? nullptr : const_cast<GtkTreePath*>((virtual_root).gobj())) )
88 {
89 }
90
91 void TreeModelFilter::set_visible_func(const SlotVisible& slot)
92 {
93 // Create a copy of the slot. A pointer to this will be passed
94 // through the callback's data parameter. It will be deleted
95 // when SignalProxy_Visible_gtk_callback_destroy() is called.
96 auto slot_copy = new SlotVisible(slot);
97
98 gtk_tree_model_filter_set_visible_func(gobj(),
99 &SignalProxy_Visible_gtk_callback, slot_copy,
100 &SignalProxy_Visible_gtk_callback_destroy);
101 }
102
103
104 TreeModel::iterator TreeModelFilter::convert_child_iter_to_iter(const iterator& child_iter) const
105 {
106 TreeIter filter_iter (const_cast<TreeModelFilter*>(this));
107
108 gtk_tree_model_filter_convert_child_iter_to_iter(
109 const_cast<GtkTreeModelFilter*>(gobj()), filter_iter.gobj(),
110 const_cast<GtkTreeIter*>(child_iter.gobj()));
111
112 return filter_iter;
113 }
114
115 TreeModel::iterator TreeModelFilter::convert_iter_to_child_iter(const iterator& filter_iter) const
116 {
117 const auto child_model = gtk_tree_model_filter_get_model(const_cast<GtkTreeModelFilter*>(gobj()));
118
119 TreeIter child_iter (dynamic_cast<TreeModel*>(Glib::wrap_auto((GObject*) child_model, false)));
120
121 gtk_tree_model_filter_convert_iter_to_child_iter(
122 const_cast<GtkTreeModelFilter*>(gobj()), child_iter.gobj(),
123 const_cast<GtkTreeIter*>(filter_iter.gobj()));
124
125 return child_iter;
126 }
127
128 void TreeModelFilter::set_modify_func(const TreeModelColumnRecord& columns, const SlotModify& slot)
129 {
130 // Create a copy of the slot. A pointer to this will be passed
131 // through the callback's data parameter. It will be deleted
132 // when SignalProxy_Modify_gtk_callback_destroy() is called.
133 auto slot_copy = new SlotModify(slot);
134
135 gtk_tree_model_filter_set_modify_func(gobj(),
136 columns.size(), const_cast<GType*>(columns.types()),
137 &SignalProxy_Modify_gtk_callback, slot_copy,
138 &SignalProxy_Modify_gtk_callback_destroy);
139 }
140
141 void TreeModelFilter::set_value_impl(const iterator& row, int column, const Glib::ValueBase& value)
142 {
143 // Avoid two extra ref/unref cycles -- we don't store the child
144 // model pointer anywhere, so it's OK to do this _internally_.
145
146 const auto child_model = dynamic_cast<TreeModel*>(
147 Glib::wrap_auto((GObject*) gtk_tree_model_filter_get_model(gobj()), false));
148
149 TreeIter child_iter (child_model);
150
151 gtk_tree_model_filter_convert_iter_to_child_iter(
152 gobj(), child_iter.gobj(), const_cast<GtkTreeIter*>(row.gobj()));
153
154 child_model->set_value_impl(child_iter, column, value);
155 }
156
157 } // namespace Gtk