Re: [gtkmm] Gtk::Container::remove and a question



> I have noticed something odd (IMO) in the remove method of Gtk::Container.
>
> When a managed widget is removed from a Gtk::Container, the remove
> method adds a reference to it, and the result is that the widget is not
> deleted.

Yes, it would be silly if the docs for Gtk::Container::remove() said
"also, the child widget will be deleted when you call this method".

> The programmer have to call my_widget->unreference () after the
> remove call to delete it.

No, if you want to delete a widget, like any class, use the "delete" C++
keyword.

> I thought that the fact to set a widget "Gtk managed" means that it
> would be deleted by gtk when necessary.

manage() means that the child widget will be deleted when its parent
container is deleted. That in no way means "and when you call remove().".

http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch18.html#id2747840

> There is a comment in the remove
> method explaining that the reference is here to prevent deleting the
> widget in case the widget will be added to another Gtk::Container.
>
> IMO, when a widget is set managed, this reference shouldn't be added
> because if the programmer wants to remove the widget and then add it to
> another container, _he_ has to put calls to reference () and unreference
> () around the operation.

That would be rather awkward. gtkmm should never require you to call
reference() and unreference(). If you need to call them sometime then
please file a bug and we'll fix that.

Note that I only "fixed" Gtk::remove() in gtkmm 2.4. Previously, you
needed to call reference() to work around the bug. You seem to have
started to think of the bug as a feature.

[snip]

> My other question is more a c++ related question but I can't figure it
> out ...
>
> In fact, the example of custom container found in the gtkmm tutorial
> (http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch21.html#id2749211)
> doesn't compile on my box.
> It fails with this :
>
> /usr/include/gtkmm-2.4/gtkmm/widget.h: In member function « virtual void
>     MyContainer::set_child_widgets(Gtk::Widget&, Gtk::Widget&) »:
> /usr/include/gtkmm-2.4/gtkmm/widget.h:2434: error: `void
>     Gtk::Widget::set_parent(Gtk::Widget&)' is protected
> mycontainer.cc:20: error: within this context
[snip]

1. This is a very new example. I uploaded it a week or so ago.
2. You are looking at the gtkmm 2.5 version, though that's not clear. Look
at the example source in your gtkmm 2.4 tarball [1] for a version with
workarounds for gtkmm 2.4.

Note that most people never need to write a custom container from scratch.
It's almost always enough to derive from an existing container.

[1] Or here, if I haven't released that yet:
http://cvs.gnome.org/viewcvs/gtkmm/examples/book/custom/custom_container/mycontainer.cc?rev=1.1.2.2&only_with_tag=gtkmm-2-4&view=markup

Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com



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