]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimageurl.c
css: Split out GtkCssImageSurface
[~andy/gtk] / gtk / gtkcssimageurl.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23
24 #include "gtkcssimageurlprivate.h"
25 #include "gtkcssimagesurfaceprivate.h"
26
27 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
28
29 static int
30 gtk_css_image_url_get_width (GtkCssImage *image)
31 {
32   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
33
34   return _gtk_css_image_get_width (url->loaded_image);
35 }
36
37 static int
38 gtk_css_image_url_get_height (GtkCssImage *image)
39 {
40   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
41
42   return _gtk_css_image_get_height (url->loaded_image);
43 }
44
45 static double
46 gtk_css_image_url_get_aspect_ratio (GtkCssImage *image)
47 {
48   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
49
50   return _gtk_css_image_get_aspect_ratio (url->loaded_image);
51 }
52
53 static void
54 gtk_css_image_url_draw (GtkCssImage        *image,
55                         cairo_t            *cr,
56                         double              width,
57                         double              height)
58 {
59   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
60
61   _gtk_css_image_draw (url->loaded_image, cr, width, height);
62 }
63
64 static gboolean
65 gtk_css_image_url_parse (GtkCssImage  *image,
66                          GtkCssParser *parser)
67 {
68   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
69   GdkPixbuf *pixbuf;
70   GError *error = NULL;
71   GFileInputStream *input;
72
73   url->file = _gtk_css_parser_read_url (parser);
74   if (url->file == NULL)
75     return FALSE;
76
77   /* We special case resources here so we can use
78      gdk_pixbuf_new_from_resource, which in turn has some special casing
79      for GdkPixdata files to avoid duplicating the memory for the pixbufs */
80   if (g_file_has_uri_scheme (url->file, "resource"))
81     {
82       char *uri = g_file_get_uri (url->file);
83       char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
84
85       pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
86       g_free (resource_path);
87       g_free (uri);
88     }
89   else
90     {
91       input = g_file_read (url->file, NULL, &error);
92       if (input == NULL)
93         {
94           _gtk_css_parser_take_error (parser, error);
95           return FALSE;
96         }
97
98       pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
99       g_object_unref (input);
100     }
101
102   if (pixbuf == NULL)
103     {
104       _gtk_css_parser_take_error (parser, error);
105       return FALSE;
106     }
107
108   url->loaded_image = _gtk_css_image_surface_new_for_pixbuf (pixbuf);
109   g_object_unref (pixbuf);
110
111   return TRUE;
112 }
113
114 static void
115 gtk_css_image_url_print (GtkCssImage *image,
116                          GString     *string)
117 {
118   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
119
120   _gtk_css_image_print (url->loaded_image, string);
121 }
122
123 static void
124 gtk_css_image_url_dispose (GObject *object)
125 {
126   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
127
128   g_clear_object (&url->file);
129   g_clear_object (&url->loaded_image);
130
131   G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
132 }
133
134 static void
135 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
136 {
137   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
138   GObjectClass *object_class = G_OBJECT_CLASS (klass);
139
140   image_class->get_width = gtk_css_image_url_get_width;
141   image_class->get_height = gtk_css_image_url_get_height;
142   image_class->get_aspect_ratio = gtk_css_image_url_get_aspect_ratio;
143   image_class->draw = gtk_css_image_url_draw;
144   image_class->parse = gtk_css_image_url_parse;
145   image_class->print = gtk_css_image_url_print;
146
147   object_class->dispose = gtk_css_image_url_dispose;
148 }
149
150 static void
151 _gtk_css_image_url_init (GtkCssImageUrl *image_url)
152 {
153 }
154