Debian Codegraph @

gtk/src/container.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/adjustment.h>
20 #include <gtkmm/scrolledwindow.h>
21 #include <gtk/gtk.h>
22
23
24 namespace
25 {
26
27 // Marks a widget that has been automatically created and added by gtk_container_add().
28 // The only reason we must use this GQuark or a similar trick, is that we want
29 // to restore the state of a managed removed object.
30 // gtk_container_remove() does not do that.
31 GQuark quark_auto_added_widget = g_quark_from_static_string("gtkmm_Container::auto_added_widget");
32
33 static void container_foreach_callback(GtkWidget* widget_gobj, void* data)
34 {
35 try
36 {
37 const auto & slot = *static_cast<Gtk::Container::ForeachSlot*>(data);
38 const auto widget = Glib::wrap(widget_gobj);
39
40 g_return_if_fail(widget != nullptr);
41
42 slot(*widget);
43 }
44 catch(...)
45 {
46 Glib::exception_handlers_invoke();
47 }
48 }
49
50
51 //Copy of generated callback:
52 static void Container_signal_remove_callback_normal(GtkContainer* self, GtkWidget* p0,void* data)
53 {
54 using namespace Gtk;
55 typedef sigc::slot< void,Widget* > SlotType;
56
57 // Do not try to call a signal on a disassociated wrapper.
58 if(Glib::ObjectBase::_get_current_wrapper((GObject*) self))
59 {
60 try
61 {
62 if(sigc::slot_base *const slot = Glib::SignalProxyNormal::data_to_slot(data))
63 (*static_cast<SlotType*>(slot))(Glib::wrap(p0)
64 );
65 }
66 catch(...)
67 {
68 Glib::exception_handlers_invoke();
69 }
70 }
71 }
72
73 static void Container_signal_remove_callback(GtkContainer* self, GtkWidget* p0, void* data)
74 {
75 //GTKMM_LIFECYCLE
76
77 #ifdef GLIBMM_DEBUG_REFCOUNTING
78 g_warning("Container_signal_remove_callback()");
79 #endif
80
81 //Don't call wrap() on a GTK+ instance whose gtkmm instance has been deleted - just call the original C callback.
82 const bool gtkmm_child_already_deleted = Glib::_gobject_cppinstance_already_deleted((GObject*)p0);
83
84 if(!gtkmm_child_already_deleted)
85 {
86 //Call the regular, generated callback:
87 Container_signal_remove_callback_normal(self, p0, data);
88 }
89 else
90 {
91 //Do nothing.
92 }
93 }
94
95 } // anonymous namespace
96
97
98 namespace Gtk
99 {
100
101 //Copy of generated callback:
102 void Container_Class::remove_callback_normal(GtkContainer* self, GtkWidget* p0)
103 {
104 const auto obj = dynamic_cast<CppObjectType*>(
105 Glib::ObjectBase::_get_current_wrapper((GObject*)self));
106
107 // Non-gtkmmproc-generated custom classes implicitly call the default
108 // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
109 // generated classes can use this optimisation, which avoids the unnecessary
110 // parameter conversions if there is no possibility of the virtual function
111 // being overridden:
112 if(obj && obj->is_derived_())
113 {
114 try // Trap C++ exceptions which would normally be lost because this is a C callback.
115 {
116 // Call the virtual member method, which derived classes might override.
117 obj->on_remove(Glib::wrap(p0)
118 );
119 }
120 catch(...)
121 {
122 Glib::exception_handlers_invoke();
123 }
124 }
125 else
126 {
127 const auto base = static_cast<BaseClassType*>(
128 g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
129 );
130
131 // Call the original underlying C function:
132 if(base && base->remove)
133 (*base->remove)(self, p0);
134 }
135 }
136
137 //Custom hand-coded callback:
138 void Container_Class::remove_callback(GtkContainer* self, GtkWidget* p0)
139 {
140 //GTKMM_LIFECYCLE
141
142 #ifdef GLIBMM_DEBUG_REFCOUNTING
143 g_warning("Container_Class::remove_callback_custom() C self=%p: C child=%p\n", (void*)self, (void*)p0);
144 g_warning("gtypename self: %s\n, gtypename child: %s", G_OBJECT_TYPE_NAME(G_OBJECT(self)), G_OBJECT_TYPE_NAME(G_OBJECT(p0)));
145 #endif
146
147 //Don't call wrap() on a GTK+ instance whose gtkmm instance has been deleted - just call the original C callback.
148 const bool gtkmm_child_already_deleted = Glib::_gobject_cppinstance_already_deleted((GObject*)p0);
149
150 if(!gtkmm_child_already_deleted)
151 {
152 //Call the regular, generated callback:
153 Container_Class::remove_callback_normal(self, p0);
154 }
155 else
156 {
157 BaseClassType *const base =
158 static_cast<BaseClassType*>(g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)));
159
160 // Do not try to call the default implementation of the virtual function
161 // GtkContainerClass::remove(), because it unhelpfully informs us that
162 // it isn't implemented. This leaves us with no generic means to check
163 // whether it is implemented.
164 if(base && G_TYPE_FROM_CLASS(base) != GTK_TYPE_CONTAINER && base->remove)
165 (*base->remove)(self, p0);
166 }
167 }
168
169 void Container::foreach(const Container::ForeachSlot& slot)
170 {
171 ForeachSlot slot_copy (slot);
172 gtk_container_foreach(gobj(), &container_foreach_callback, &slot_copy);
173 }
174
175 void Container::forall(const Container::ForeachSlot& slot)
176 {
177 ForeachSlot slot_copy (slot);
178 gtk_container_forall(gobj(), &container_foreach_callback, &slot_copy);
179 }
180
181 // Why not let both add() and remove() be virtual, and override them in ScrolledWindow?
182 // Because a virtual add() has an unwanted side-effect in Gtk::Stack.
183 // See https://bugzilla.gnome.org/show_bug.cgi?id=724732
184 // And a virtual remove() can be problematic because ScrolledWindow and other
185 // subclasses of Bin have a remove() overload with no parameter.
186
187 void Container::add(Widget& widget)
188 {
189 auto scrolled_window = dynamic_cast<ScrolledWindow*>(this);
190 if (scrolled_window)
191 {
192 auto old_child = gtk_bin_get_child(scrolled_window->Bin::gobj()); // Normally nullptr
193 gtk_container_add(gobj(), widget.gobj());
194 auto child = gtk_bin_get_child(scrolled_window->Bin::gobj());
195 if (child && child != old_child)
196 {
197 // If the child is not the inserted widget, gtk_scrolled_window_add() has
198 // created and inserted a GtkViewport. Mark this GtkViewport as automatically
199 // added. Container::remove() needs this information.
200 const gpointer qdata_value = (child != widget.gobj()) ? child : nullptr;
201 g_object_set_qdata(G_OBJECT(child), quark_auto_added_widget, qdata_value);
202 }
203 }
204 else
205 gtk_container_add(gobj(), widget.gobj());
206 }
207
208 #ifndef GTKMM_DISABLE_DEPRECATED
209 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
210 bool Container::has_focus_chain() const
211 {
212 return gtk_container_get_focus_chain(const_cast<GtkContainer*>(gobj()), nullptr);
213 }
214
215 std::vector<Widget*> Container::get_focus_chain()
216 {
217 GList* list = nullptr;
218 gtk_container_get_focus_chain(gobj(), &list);
219
220 return Glib::ListHandler<Widget*>::list_to_vector(list, Glib::OWNERSHIP_SHALLOW);
221 }
222
223 std::vector<const Widget*> Container::get_focus_chain() const
224 {
225 GList* list = nullptr;
226 gtk_container_get_focus_chain(const_cast<GtkContainer*>(gobj()), &list);
227
228 return Glib::ListHandler<const Widget*>::list_to_vector(list, Glib::OWNERSHIP_SHALLOW);
229 }
230 G_GNUC_END_IGNORE_DEPRECATIONS
231 #endif // GTKMM_DISABLE_DEPRECATED
232
233 void Container::show_all_children(bool recursive)
234 {
235 // Plain C version if this turns out to be performance-critical:
236 //GtkCallback callback = (GtkCallback) ((recursive) ? &gtk_widget_show_all : &gtk_widget_show);
237 //gtk_container_foreach(gobj(), callback, 0);
238
239 // This could also be done with get_children() and an explicit loop,
240 // if any problems should arise. In gtkmm-1.2, foreach() is known to
241 // cause segfaults eventually, but it seems to work now.
242
243 foreach(sigc::mem_fun((recursive) ? &Widget::show_all : &Widget::show));
244 }
245
246 void Container::remove(Widget& widget)
247 {
248 auto child_to_float = &widget;
249 if (g_object_get_qdata(widget.Glib::Object::gobj(), quark_auto_added_widget))
250 {
251 // If widget was created by gtk_scrolled_window_add(),
252 // don't restore the floating state of that widget. Instead, restore the
253 // state of that widget's child, which was added by add().
254 auto scrolled_window = dynamic_cast<ScrolledWindow*>(this);
255 if (scrolled_window)
256 {
257 auto bin = dynamic_cast<Bin*>(&widget);
258 if (bin)
259 child_to_float = bin->get_child();
260 }
261 }
262
263 // If this is a managed widget, then do an extra ref so that it will not be
264 // destroyed when it is removed, and restore the floating state of the ref.
265 // This should leave it in the same state as when it was instantiated,
266 // before being added to the container.
267 if (child_to_float && child_to_float->is_managed_())
268 {
269 child_to_float->reference();
270 g_object_force_floating(child_to_float->Glib::Object::gobj());
271 }
272
273 // gtk_container_remove() removes what gtk_container_add() added, even if
274 // gtk_container_add() added an extra GtkViewport in a GtkScrolledWindow.
275 // See https://bugzilla.gnome.org/show_bug.cgi?id=710471
276 gtk_container_remove(gobj(), widget.gobj());
277 }
278
279 } // namespace Gtk