Re: Recycling ListItems.




Andreas Kostyrka <andreas@ag.or.at> writes:

> I'm having problems recycling ListItems. (As my ListItems are relativly
> expensive to build I'd like to cache them.)
> 
> The problem, is that even calling gtk_widget_reparent(listitem,0);
> it's still not working and I'm getting heaps of warnings :(

What you have to be careful about when doing this is reference
counting. Normally, a widget's parent holds the only reference
to the child, so when it is removed from its parent, it will
be destroyed.

To prevent this from happening, you have to keep a reference
count on the child yourself.

 listitem = gtk_list_item_new_with_label ("Item #1");
 /* Let GTK we are taking over the initial reference count
  * for this item 
  */
 gtk_widget_ref (listitem);
 gtk_object_sink (GTK_OBJECT (listitem));

 gtk_container_add (GTK_CONTAINER (list1), listitem)
 
 [...]
 
 gtk_container_remove (GTK_CONTAINER (list1), listitem)
 gtk_container_add (GTK_CONTAINER (list2), listitem)
 
 [...]

 gtk_container_remove (GTK_CONTAINER (list2), listitem)

 /* Finally let the item be destroyed */
 gtk_widget_unref (listitem);

(An alternate way of doing this is not to hold a reference
count, but remove the items with gtk_list_remove_items_no_unref().
I think the above way is simpler if you are repeatedly adding
and removing the items)

Regards,
                                        Owen



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