Custom container with ::clear function, how should it behave?



I've got a gtkmm program that includes a custom container (MyContainer)
including a function MyContainer::Clear, that needs to remove all
widgets from the container.

The implementation I have come up, which seems to work correctly for
Gtk::Widgets that are both Gtk::manage'd and otherwise, with looks like
this:

void MyContainer::Clear()
{
    for (vector<Gtk::Widget *>::iterator i = mWidgets.begin();
         i != mWidgets.end();
         ++i)
    {
        Gtk::Widget &w = (**i);
        remove(w);
        if (w.is_managed_())
        {
            w.unreference();
        }
    }
}

I've done a lot of digging to get this far. My decisions have been based
on the following assumptions:
- There is no precedent (no standard Gtk::Container has a function to
clear its child widgets) for this in gtkmm, I can't copy from anywhere.
- Widgets which are not manage()'d are never destroyed by
Gtk::Containers anywhere else, so they shouldn't be here either.
- Widgets which are manage()'d are explicitly reset to their initial
state by Gtk::Container::remove, and so should be explicitly unref'd.
- Gtk::Container::add and ::remove will not, under ordinary
circumstances, ever delete a widget (because widgets are instantiated
with an initial reference count of 1, and because of special removal of
manage()'d widgets)

For the record, my container's destructor is implemented this way:

MyContainer::~MyContainer()
{
    forall_vfunc(false, (GtkCallback) gtk_widget_unparent, NULL);
}

I unparent each widget because that's what the custom container example
does:
https://developer.gnome.org/gtkmm-tutorial/stable/sec-custom-containers.html.en

Just for kicks, I changed the call 'remove(w)' in ::Clear to
'on_remove(w)' and removing the explicit unreference, and this seems to
work to maintain operational consistency with other gtkmm containers,
but I'm not sure that bypassing the call to ::remove is safe or correct,
though I understand why the call to ::on_remove (which unparents the
widget) is having the desired effect.  Is it always safe to do this, or
are there circumstances where I accidentally bypass something important
this way?

Thanks,
Ryan


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]