Re: gtk_main nesting, and how to free/destroy widgets



On Mon, 2 Feb 2009 17:05:40 +1000 Dan Ium wrote:

Does there always need to be at least one gtk_main active, or is it ok
to do call gtk_main after gtk_main_quit is called on the inner most
loop? And if so, do you have to call gtk_init again before you call
gtk_main after the inner most loop has quit?

You can certainly restart the main loop after quitting it if you want,
though that's a little unusual.  No, you should not call gtk_init()
more than once.

Also. what is the proper way to free/destroy widgets?

gtk_widget_destroy().

For example I have a widget from gtk_window_new , and I add a vbox to
its container, and to that vbox I add buttons etc.
I dont want the window anymore but still want the vbox. So should I do
gtk_container_remove to remove the box and then gtk_widget_destroy on
the window?

Almost...  when you create a widget, you don't own its reference
count (at that point it has what we call a 'floating reference' that
no one owns). When you add a widget to a container, that container takes
ownership of the widget (it 'sinks' the floating reference and takes a
real reference).  So if you want to remove a widget from a container
without losing it, you need to call g_object_ref() on it first, and
then, after you add it to the new container, call g_object_unref() to
release your reference to it.  So:

g_object_ref(child_widget);
gtk_container_remove(parent_container, child_widget);  /* calls unref */
gtk_widget_destroy(old_parent_window);
/* do whatever.... */
gtk_container_add(new_parent_conatiner, child_widget);  /* calls ref */
g_object_unref(child_widget);

Or, if you already have the new parent ready, you can avoid all the
extra refcounting with a simple:

gtk_widget_reparent(child_widget, new_parent_container);
gtk_widget_destroy(old_parent_window);

And when I am finished with the box, do I call gtk_widget_destroy on
it? Will this destory all its sub widgets, or do I have to call
gtk_widget_destroy under it (I mean buttons packed in the box)?

Yes, gtk_widget_destroy() will always destroy all child widgets in
addition to the widget you pass it.

        -brian



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