]> Pileus Git - ~andy/gtk/commitdiff
Add GtkImage constructors from resources
authorAlexander Larsson <alexl@redhat.com>
Thu, 12 Jan 2012 21:11:41 +0000 (22:11 +0100)
committerAlexander Larsson <alexl@redhat.com>
Mon, 16 Jan 2012 13:19:18 +0000 (14:19 +0100)
Atm you can't read back the resource path like
you can with filenames. Maybe we should add that.

gtk/gtkimage.c
gtk/gtkimage.h

index 057e7505233d6568a8b822f5cb4799ed793796d2..e7fbf09fa59dcce6bb938f07f00e32844d9fdbef 100644 (file)
@@ -512,6 +512,43 @@ gtk_image_new_from_file   (const gchar *filename)
   return GTK_WIDGET (image);
 }
 
+/**
+ * gtk_image_new_from_resource:
+ * @resource_path: a resource path
+ *
+ * Creates a new #GtkImage displaying the resource file @resource_path. If the file
+ * isn't found or can't be loaded, the resulting #GtkImage will
+ * display a "broken image" icon. This function never returns %NULL,
+ * it always returns a valid #GtkImage widget.
+ *
+ * If the file contains an animation, the image will contain an
+ * animation.
+ *
+ * If you need to detect failures to load the file, use
+ * gdk_pixbuf_new_from_file() to load the file yourself, then create
+ * the #GtkImage from the pixbuf. (Or for animations, use
+ * gdk_pixbuf_animation_new_from_file()).
+ *
+ * The storage type (gtk_image_get_storage_type()) of the returned
+ * image is not defined, it will be whatever is appropriate for
+ * displaying the file.
+ *
+ * Return value: a new #GtkImage
+ *
+ * Since: 3.4
+ **/
+GtkWidget*
+gtk_image_new_from_resource (const gchar *resource_path)
+{
+  GtkImage *image;
+
+  image = g_object_new (GTK_TYPE_IMAGE, NULL);
+
+  gtk_image_set_from_resource (image, resource_path);
+
+  return GTK_WIDGET (image);
+}
+
 /**
  * gtk_image_new_from_pixbuf:
  * @pixbuf: (allow-none): a #GdkPixbuf, or %NULL
@@ -740,6 +777,59 @@ gtk_image_set_from_file   (GtkImage    *image,
   g_object_thaw_notify (G_OBJECT (image));
 }
 
+/**
+ * gtk_image_set_from_resource:
+ * @image: a #GtkImage
+ * @resource_path: (allow-none): a resource path or %NULL
+ *
+ * See gtk_image_new_from_resource() for details.
+ **/
+void
+gtk_image_set_from_resource   (GtkImage    *image,
+                              const gchar *resource_path)
+{
+  GtkImagePrivate *priv;
+  GdkPixbuf *pixbuf;
+  GInputStream *stream;
+
+  g_return_if_fail (GTK_IS_IMAGE (image));
+
+  priv = image->priv;
+
+  g_object_freeze_notify (G_OBJECT (image));
+
+  gtk_image_clear (image);
+
+  if (resource_path == NULL)
+    {
+      g_object_thaw_notify (G_OBJECT (image));
+      return;
+    }
+
+  stream = g_resources_open_stream (resource_path, 0, NULL);
+  if (stream != NULL)
+    {
+      pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
+      g_object_unref (stream);
+    }
+
+  if (pixbuf == NULL)
+    {
+      gtk_image_set_from_stock (image,
+                                GTK_STOCK_MISSING_IMAGE,
+                                DEFAULT_ICON_SIZE);
+      g_object_thaw_notify (G_OBJECT (image));
+      return;
+    }
+
+  gtk_image_set_from_pixbuf (image, pixbuf);
+
+  g_object_unref (pixbuf);
+
+  g_object_thaw_notify (G_OBJECT (image));
+}
+
+
 /**
  * gtk_image_set_from_pixbuf:
  * @image: a #GtkImage
index ec8d243f940b0813b28106ed3c9a92ae1f741685..e3b9714802edf2626a1e8ec8025aa296d909256a 100644 (file)
@@ -111,6 +111,7 @@ GType      gtk_image_get_type (void) G_GNUC_CONST;
 
 GtkWidget* gtk_image_new                (void);
 GtkWidget* gtk_image_new_from_file      (const gchar     *filename);
+GtkWidget* gtk_image_new_from_resource  (const gchar     *resource_path);
 GtkWidget* gtk_image_new_from_pixbuf    (GdkPixbuf       *pixbuf);
 GtkWidget* gtk_image_new_from_stock     (const gchar     *stock_id,
                                          GtkIconSize      size);
@@ -125,6 +126,8 @@ GtkWidget* gtk_image_new_from_gicon     (GIcon           *icon,
 void gtk_image_clear              (GtkImage        *image);
 void gtk_image_set_from_file      (GtkImage        *image,
                                    const gchar     *filename);
+void gtk_image_set_from_resource  (GtkImage        *image,
+                                   const gchar     *resource_path);
 void gtk_image_set_from_pixbuf    (GtkImage        *image,
                                    GdkPixbuf       *pixbuf);
 void gtk_image_set_from_stock     (GtkImage        *image,