Re: Dynamic modification of widgets




Landshark <lndshark@megsinet.net> writes:

> > > 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 )  ??

I probably shouldn't have said anything. :-)

  gtk_widget_destroy (box) 

is safe, no ifs ands or buts.


Where you need to ref/unref is if you want to do:

  gtk_container_remove (box, child);
  gtk_widget_destroy (box);
  box2 = gtk_hbox_new (...)
  gtk_box_pack_start (GTK_BOX(box2), child, ....);
  
That needs to be

  gtk_widget_ref (child);

  gtk_container_remove (box, child);
  gtk_widget_destroy (box);
  box2 = gtk_hbox_new (...)
  gtk_box_pack_start (GTK_BOX(box2), child, ....);

  gtk_widget_unref (child);
 
The idea is that GTK needs to know that somebody cares about the
child. If not, it will destroy it.

Normally, a widget's parent takes care of this. But when you
remove the widget from its parent, you have to tell GTK you
are taking on that responsibility. After you have added
the widget back to the second box, you call gtk_widget_unref(),
because the new parent now has the responsibility.

Regards,
                                        Owen



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