+ gl_text_node_free (&old_filename);
+
+
+ /* Now set the new file type and the pixbuf or svg_handle. */
+ if ( !filename->field_flag && (filename->data != NULL) )
+ {
+
+ if ( gl_file_util_is_extension (filename->data, ".svg") )
+ {
+ svg_handle = gl_svg_cache_get_handle (svg_cache, filename->data);
+
+ if (svg_handle != NULL)
+ {
+ this->priv->type = FILE_TYPE_SVG;
+ this->priv->pixbuf = NULL;
+ this->priv->svg_handle = svg_handle;
+ }
+ else
+ {
+ this->priv->type = FILE_TYPE_NONE;
+ this->priv->pixbuf = NULL;
+ this->priv->svg_handle = NULL;
+ }
+
+ }
+ else
+ {
+
+ pixbuf = gl_pixbuf_cache_get_pixbuf (pixbuf_cache, filename->data);
+
+ if (pixbuf != NULL)
+ {
+ this->priv->type = FILE_TYPE_PIXBUF;
+ this->priv->pixbuf = pixbuf;
+ this->priv->svg_handle = NULL;
+ }
+ else
+ {
+ this->priv->type = FILE_TYPE_NONE;
+ this->priv->pixbuf = NULL;
+ this->priv->svg_handle = NULL;
+ }
+
+ }
+ }
+ else
+ {
+ this->priv->type = FILE_TYPE_NONE;
+ this->priv->pixbuf = NULL;
+ this->priv->svg_handle = NULL;
+ }
+
+
+ /* Treat current size as a bounding box, scale image to maintain aspect
+ * ratio while fitting it in this bounding box. */
+ switch (this->priv->type)
+ {
+
+ case FILE_TYPE_PIXBUF:
+ image_w = gdk_pixbuf_get_width (this->priv->pixbuf);
+ image_h = gdk_pixbuf_get_height (this->priv->pixbuf);
+ break;
+
+ case FILE_TYPE_SVG:
+ rsvg_handle_get_dimensions (this->priv->svg_handle, &svg_dim);
+ image_w = svg_dim.width;
+ image_h = svg_dim.height;
+ break;
+
+ default:
+ image_w = gdk_pixbuf_get_width (default_pixbuf);
+ image_h = gdk_pixbuf_get_height (default_pixbuf);
+ break;
+
+ }
+ aspect_ratio = image_h / image_w;
+ gl_label_object_get_size (GL_LABEL_OBJECT(this), &w, &h);
+ if ( h > w*aspect_ratio ) {
+ h = w * aspect_ratio;
+ } else {
+ w = h / aspect_ratio;
+ }
+ gl_label_object_set_size (GL_LABEL_OBJECT(this), w, h, FALSE);
+
+ gl_label_object_emit_changed (GL_LABEL_OBJECT(this));
+
+ gl_debug (DEBUG_LABEL, "END");
+}
+
+
+void
+gl_label_image_set_pixbuf (glLabelImage *this,
+ GdkPixbuf *pixbuf,
+ gboolean checkpoint)
+{
+ glTextNode *old_filename;
+ glLabel *label;
+ GHashTable *pixbuf_cache;
+ GHashTable *svg_cache;
+ gchar *cs;
+ gchar *name;
+ gdouble image_w, image_h;
+
+ gl_debug (DEBUG_LABEL, "START");
+
+ g_return_if_fail (this && GL_IS_LABEL_IMAGE (this));
+ g_return_if_fail (pixbuf && GDK_IS_PIXBUF (pixbuf));
+
+ old_filename = this->priv->filename;
+
+ label = gl_label_object_get_parent (GL_LABEL_OBJECT (this));
+
+ if ( checkpoint )
+ {
+ gl_label_checkpoint (label, _("Set image"));
+ }
+
+ pixbuf_cache = gl_label_get_pixbuf_cache (label);
+ svg_cache = gl_label_get_svg_cache (label);
+
+ /* Remove reference to previous item. */
+ switch (this->priv->type)
+ {
+
+ case FILE_TYPE_PIXBUF:
+ if ( !old_filename->field_flag && (old_filename->data != NULL) ) {
+ gl_pixbuf_cache_remove_pixbuf (pixbuf_cache, old_filename->data);
+ }
+ break;
+
+ case FILE_TYPE_SVG:
+ if ( !old_filename->field_flag && (old_filename->data != NULL) ) {
+ gl_svg_cache_remove_svg (svg_cache, old_filename->data);
+ }
+ break;
+
+ default:
+ break;
+
+ }
+
+ /* Set new filename. */
+ cs = g_compute_checksum_for_data (G_CHECKSUM_MD5,
+ gdk_pixbuf_get_pixels (pixbuf),
+ gdk_pixbuf_get_rowstride (pixbuf)*gdk_pixbuf_get_height (pixbuf));
+ name = g_strdup_printf ("%s.bitmap", cs);
+ this->priv->filename = gl_text_node_new_from_text(name);
+ gl_text_node_free (&old_filename);