Re: Deleting a GnomeCanvasItem

Roland Roberts <roland astrofoto org> writes:

> I can't seem to find any references in the online documetation on how
> to do this.  I am loading a GdkPixbuf into a canvas as shown in the
> callback below.  This gets invoked just after a file selection widget
> is closed and the new file is added to an internal list of files.  I
> create a new GdkPixbuf and then add a new item.  The canvas will only
> ever have one item; how do I find the old one and delete it?  

You keep a pointer to it somewhere in your application and you later do

	gtk_object_destroy (GTK_OBJECT (item));

I.e. you destroy it just like if it were a GtkWidget.

[Canvas items are *NOT* GtkWidgets, but they work in pretty much the
same way in the ways of memory management.]

> I'm a little fuzzy on what gnome_canvas_item_new is doing; am I
> naming the item "pixbuf" and if so, does creating a new item with
> the same name implicitly destroy the old one?

It is not a name; "pixbuf" is the argument that you set on the canvas
item to tell it which GdkPixbuf to display in the canvas.

If you are into object patterns and pedantry and overnomenclaturification,
you could call a GnomeCanvasPixbuf an adapter so that a GdkPixbuf can
be displayed and manipulated as a canvas item.

Other arguments of the GnomeCanvasPixbuf class include "width",
"height", etc.  The "pixbuf" argument is just the most important one
that the item supports.

>       /* Add the pixbuf to the scrolled window. */
>       item = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS(canvas)),
> 				    gnome_canvas_pixbuf_get_type(),
> 				    "pixbuf", pixbuf,
> 				    NULL);

When you set the "pixbuf" argument, the item will add a reference to
the pixbuf.  When the item is destroyed or its "pixbuf" argument is
changed again, it will release the reference from the old pixbuf.  So
if your program just needs to change the GdkPixbuf used instead of
just creating a new canvas item, you can just use
gnome_canvas_item_set() to specify a new pixbuf to use on the same

>       /* Free the GdkPixbuf. */
>       gdk_pixbuf_unref (pixbuf);

This does not really free it; it just releases a reference from it.
When the reference count drops to zero, the pixbuf is actually


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