]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcssimageurl.c
container: Split out a function
[~andy/gtk] / gtk / gtkcssimageurl.c
index 8bfd70861ab610ceaefe4d867d13dfe6df49d4ef..8fa2965a423b23b4c12bd607bfe550288a8a4e90 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  *
  * Authors: Benjamin Otte <otte@gnome.org>
  */
 
 #include "config.h"
 
+#include <string.h>
+
 #include "gtkcssimageurlprivate.h"
 
 #include "gtkcssprovider.h"
@@ -67,19 +68,40 @@ gtk_css_image_url_parse (GtkCssImage  *image,
   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
   GdkPixbuf *pixbuf;
   GFile *file;
-  char *path;
   cairo_t *cr;
   GError *error = NULL;
+  GFileInputStream *input;
 
   file = _gtk_css_parser_read_url (parser, base);
   if (file == NULL)
     return FALSE;
 
-  path = g_file_get_path (file);
+  /* We special case resources here so we can use
+     gdk_pixbuf_new_from_resource, which in turn has some special casing
+     for GdkPixdata files to avoid duplicating the memory for the pixbufs */
+  if (g_file_has_uri_scheme (file, "resource"))
+    {
+      char *uri = g_file_get_uri (file);
+      char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
+
+      pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
+      g_free (resource_path);
+      g_free (uri);
+    }
+  else
+    {
+      input = g_file_read (file, NULL, &error);
+      if (input == NULL)
+       {
+         _gtk_css_parser_take_error (parser, error);
+         return FALSE;
+       }
+
+      pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
+      g_object_unref (input);
+    }
   g_object_unref (file);
 
-  pixbuf = gdk_pixbuf_new_from_file (path, &error);
-  g_free (path);
   if (pixbuf == NULL)
     {
       _gtk_css_parser_take_error (parser, error);
@@ -92,6 +114,7 @@ gtk_css_image_url_parse (GtkCssImage  *image,
   cr = cairo_create (url->surface);
   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
   cairo_paint (cr);
+  cairo_destroy (cr);
   g_object_unref (pixbuf);
 
   return TRUE;