1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #ifndef INKSCAPE_UI_CONTAINERIZE_H
3 #define INKSCAPE_UI_CONTAINERIZE_H
5 #include <gtkmm/widget.h>
7 namespace Inkscape::UI
{
10 * Make a custom widget implement sensible memory management for its children.
12 * This frees the implementer of a custom widget from having to manually unparent()
13 * children added with set_parent() both in the destructor and on signal_destroy(),
14 * a memory management detail from C that has no business leaking into C++.
16 * Upon destruction, or for managed widgets just before, all children are unparented.
17 * Managed children are also deleted if they have no other references.
19 * This function is typically called in the constructor of a custom widget that derives
20 * from an intrinsically childless Gtk widget, e.g. Gtk::Widget, Gtk::DrawingArea.
22 * It must not be used with any intrinsically child-containing Gtk widget, e.g.
23 * Gtk::Box, Gtk::SpinButton.
25 inline void containerize(Gtk::Widget
&widget
)
27 g_signal_connect(widget
.gobj(), "destroy", G_CALLBACK(+[] (GtkWidget
*gobj
, void *) {
28 for (auto c
= gtk_widget_get_first_child(gobj
); c
; ) {
29 auto cnext
= gtk_widget_get_next_sibling(c
);
30 gtk_widget_unparent(c
);
36 } // namespace Inkscape::UI
38 #endif // INKSCAPE_UI_CONTAINERIZE_H