Re: [gtk-list] Re: Dynamic modification of widgets



On Thu, 12 Feb 1998, Landshark wrote:

> > > I have a window with a horizontally split pane in it right now.  I want
> > > to, based on user input, modify it so it becomes a vertical split,
> > > verticle + horizontal, etc.
> > > 
> > > How do I go about safely deleting the pane and everything in it?  Could I
> > > just call gtk_widget_delete on the box holding it, or would that not quite
> > > do the job?
> >                        ^^^^^^
> > s/delete/destroy/
> > 
> > That will work fine. (If, however, you remove some of the widgets it
> > holds with gtk_container_remove() before destroying it, remember to
> > gtk_widget_ref() them before removing them and gtk_widget_unref()
> > after you've added them back in a new location)
> 
> So I have to gtk_widget_ref( "Everything in that box" ), before I
> gtk_container_remove( box )  ??

you need only to put a reference on the widget that you want to remove
and later reuse, that is:

gtk_conatiner_remove(box, button);
will destroy button because normaly its reference copunt drops to zero
because its container no longer holds a reference to it.

if you want to reuse the button by e.g. adding it to another container
you would want to do:

gtk_widget_ref(button);
gtk_container_remove(box, button);
/* the button is not destroyed because you are holding a reerence count on
 * it and therefore the GTK_OBJECT(buttons)->ref_count field is > 0 which
 * prevents the button from destruction.
 */
gtk_container_add(other_box, button);
/* GTK_OBJECT(buttons)->ref_count==2 now
 */
gtk_widget_unref (button);
/* GTK_OBJECT(buttons)->ref_count==1 now, which is the normal state for a
 * widget that's been added to a parent.
 */


> I'm not quite following this.  I have never used ref/unref before.  Does
> this manage a counter to see if the widget is still in use? 

exactly. you might want to read up on gtk+/docs/refcounting.txt on this,
currently in CVS only (as is the reference counting stuff).

> I noticed in the gtkcontainer.h file, there is a function called
> gtk_container_children which returns a list of items, the children widgets
> I'd assume.

yep, correctly.

> Is it safe to just step through that list destroying them all, or would I
> have to mess with unref/ref still?

hm, that is save (assuming you are running a recent CVS version of Gtk+),
don't forget to make a g_list_free (child_list) after this ;)

> | -Dave                                | // Name that OS                |
> | lndshark@megsinet.net                | #include <slow_crap.h>         |

---
ciaoTJ



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