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 #include "gtkstyleproviderprivate.h"
27
G_DEFINE_TYPE(GtkCssImageUrl,_gtk_css_image_url,GTK_TYPE_CSS_IMAGE)28 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
29
30 static GtkCssImage *
31 gtk_css_image_url_load_image (GtkCssImageUrl *url,
32 GError **error)
33 {
34 GdkPixbuf *pixbuf;
35 GError *local_error = NULL;
36 GFileInputStream *input;
37
38 if (url->loaded_image)
39 return url->loaded_image;
40
41 /* We special case resources here so we can use
42 gdk_pixbuf_new_from_resource, which in turn has some special casing
43 for GdkPixdata files to avoid duplicating the memory for the pixbufs */
44 if (g_file_has_uri_scheme (url->file, "resource"))
45 {
46 char *uri = g_file_get_uri (url->file);
47 char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
48
49 pixbuf = gdk_pixbuf_new_from_resource (resource_path, &local_error);
50 g_free (resource_path);
51 g_free (uri);
52 }
53 else
54 {
55 input = g_file_read (url->file, NULL, &local_error);
56 if (input != NULL)
57 {
58 pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &local_error);
59 g_object_unref (input);
60 }
61 else
62 {
63 pixbuf = NULL;
64 }
65 }
66
67 if (pixbuf == NULL)
68 {
69 cairo_surface_t *empty;
70
71 if (error)
72 {
73 char *uri;
74
75 uri = g_file_get_uri (url->file);
76 g_set_error (error,
77 GTK_CSS_PROVIDER_ERROR,
78 GTK_CSS_PROVIDER_ERROR_FAILED,
79 "Error loading image '%s': %s", uri, local_error->message);
80 g_error_free (local_error);
81 g_free (uri);
82 }
83
84 empty = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
85 url->loaded_image = _gtk_css_image_surface_new (empty);
86 cairo_surface_destroy (empty);
87 return url->loaded_image;
88 }
89
90 url->loaded_image = _gtk_css_image_surface_new_for_pixbuf (pixbuf);
91 g_object_unref (pixbuf);
92
93 return url->loaded_image;
94 }
95
96 static int
gtk_css_image_url_get_width(GtkCssImage * image)97 gtk_css_image_url_get_width (GtkCssImage *image)
98 {
99 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
100
101 return _gtk_css_image_get_width (gtk_css_image_url_load_image (url, NULL));
102 }
103
104 static int
gtk_css_image_url_get_height(GtkCssImage * image)105 gtk_css_image_url_get_height (GtkCssImage *image)
106 {
107 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
108
109 return _gtk_css_image_get_height (gtk_css_image_url_load_image (url, NULL));
110 }
111
112 static double
gtk_css_image_url_get_aspect_ratio(GtkCssImage * image)113 gtk_css_image_url_get_aspect_ratio (GtkCssImage *image)
114 {
115 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
116
117 return _gtk_css_image_get_aspect_ratio (gtk_css_image_url_load_image (url, NULL));
118 }
119
120 static void
gtk_css_image_url_draw(GtkCssImage * image,cairo_t * cr,double width,double height)121 gtk_css_image_url_draw (GtkCssImage *image,
122 cairo_t *cr,
123 double width,
124 double height)
125 {
126 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
127
128 _gtk_css_image_draw (gtk_css_image_url_load_image (url, NULL), cr, width, height);
129 }
130
131 static GtkCssImage *
gtk_css_image_url_compute(GtkCssImage * image,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)132 gtk_css_image_url_compute (GtkCssImage *image,
133 guint property_id,
134 GtkStyleProviderPrivate *provider,
135 GtkCssStyle *style,
136 GtkCssStyle *parent_style)
137 {
138 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
139 GtkCssImage *copy;
140 GError *error = NULL;
141
142 copy = gtk_css_image_url_load_image (url, &error);
143 if (error)
144 {
145 GtkCssSection *section = gtk_css_style_get_section (style, property_id);
146 _gtk_style_provider_private_emit_error (provider, section, error);
147 g_error_free (error);
148 }
149
150 return g_object_ref (copy);
151 }
152
153 static gboolean
gtk_css_image_url_parse(GtkCssImage * image,GtkCssParser * parser)154 gtk_css_image_url_parse (GtkCssImage *image,
155 GtkCssParser *parser)
156 {
157 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
158
159 url->file = _gtk_css_parser_read_url (parser);
160 if (url->file == NULL)
161 return FALSE;
162
163 return TRUE;
164 }
165
166 static void
gtk_css_image_url_print(GtkCssImage * image,GString * string)167 gtk_css_image_url_print (GtkCssImage *image,
168 GString *string)
169 {
170 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
171
172 _gtk_css_image_print (gtk_css_image_url_load_image (url, NULL), string);
173 }
174
175 static void
gtk_css_image_url_dispose(GObject * object)176 gtk_css_image_url_dispose (GObject *object)
177 {
178 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
179
180 g_clear_object (&url->file);
181 g_clear_object (&url->loaded_image);
182
183 G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
184 }
185
186 static void
_gtk_css_image_url_class_init(GtkCssImageUrlClass * klass)187 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
188 {
189 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
190 GObjectClass *object_class = G_OBJECT_CLASS (klass);
191
192 image_class->get_width = gtk_css_image_url_get_width;
193 image_class->get_height = gtk_css_image_url_get_height;
194 image_class->get_aspect_ratio = gtk_css_image_url_get_aspect_ratio;
195 image_class->compute = gtk_css_image_url_compute;
196 image_class->draw = gtk_css_image_url_draw;
197 image_class->parse = gtk_css_image_url_parse;
198 image_class->print = gtk_css_image_url_print;
199
200 object_class->dispose = gtk_css_image_url_dispose;
201 }
202
203 static void
_gtk_css_image_url_init(GtkCssImageUrl * image_url)204 _gtk_css_image_url_init (GtkCssImageUrl *image_url)
205 {
206 }
207
208