1 /*
2 * Copyright © 2016 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: Matthias Clasen <mclasen@redhat.com>
18 */
19
20 #include "config.h"
21
22 #include "gtkcssimagerecolorprivate.h"
23 #include "gtkcssimageurlprivate.h"
24 #include "gtkcssimagesurfaceprivate.h"
25 #include "gtkcsspalettevalueprivate.h"
26 #include "gtkcssrgbavalueprivate.h"
27 #include "gtkiconthemeprivate.h"
28
29 #include "gtkstyleproviderprivate.h"
30
G_DEFINE_TYPE(GtkCssImageRecolor,_gtk_css_image_recolor,GTK_TYPE_CSS_IMAGE_URL)31 G_DEFINE_TYPE (GtkCssImageRecolor, _gtk_css_image_recolor, GTK_TYPE_CSS_IMAGE_URL)
32
33 static void
34 gtk_css_image_recolor_print (GtkCssImage *image,
35 GString *string)
36 {
37 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
38 GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
39 char *uri;
40
41 g_string_append (string, "-gtk-recolor(url(");
42 uri = g_file_get_uri (url->file);
43 g_string_append (string, uri);
44 g_free (uri);
45 g_string_append (string, ")");
46 if (recolor->palette)
47 {
48 g_string_append (string, ",");
49 _gtk_css_value_print (recolor->palette, string);
50 }
51 g_string_append (string, ")");
52 }
53
54 static void
gtk_css_image_recolor_dispose(GObject * object)55 gtk_css_image_recolor_dispose (GObject *object)
56 {
57 GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (object);
58
59 if (recolor->palette)
60 {
61 _gtk_css_value_unref (recolor->palette);
62 recolor->palette = NULL;
63 }
64
65 G_OBJECT_CLASS (_gtk_css_image_recolor_parent_class)->dispose (object);
66 }
67
68 static void
lookup_symbolic_colors(GtkCssStyle * style,GtkCssValue * palette,GdkRGBA * color_out,GdkRGBA * success_out,GdkRGBA * warning_out,GdkRGBA * error_out)69 lookup_symbolic_colors (GtkCssStyle *style,
70 GtkCssValue *palette,
71 GdkRGBA *color_out,
72 GdkRGBA *success_out,
73 GdkRGBA *warning_out,
74 GdkRGBA *error_out)
75 {
76 GtkCssValue *color;
77 const GdkRGBA *lookup;
78
79 color = gtk_css_style_get_value (style, GTK_CSS_PROPERTY_COLOR);
80 *color_out = *_gtk_css_rgba_value_get_rgba (color);
81
82 lookup = gtk_css_palette_value_get_color (palette, "success");
83 if (lookup)
84 *success_out = *lookup;
85 else
86 *success_out = *color_out;
87
88 lookup = gtk_css_palette_value_get_color (palette, "warning");
89 if (lookup)
90 *warning_out = *lookup;
91 else
92 *warning_out = *color_out;
93
94 lookup = gtk_css_palette_value_get_color (palette, "error");
95 if (lookup)
96 *error_out = *lookup;
97 else
98 *error_out = *color_out;
99 }
100
101 static GtkCssImage *
gtk_css_image_recolor_load(GtkCssImageRecolor * recolor,GtkCssStyle * style,GtkCssValue * palette,gint scale,GError ** gerror)102 gtk_css_image_recolor_load (GtkCssImageRecolor *recolor,
103 GtkCssStyle *style,
104 GtkCssValue *palette,
105 gint scale,
106 GError **gerror)
107 {
108 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (recolor);
109 GtkIconInfo *info;
110 GdkRGBA fg, success, warning, error;
111 GdkPixbuf *pixbuf;
112 GtkCssImage *image;
113 GError *local_error = NULL;
114
115 lookup_symbolic_colors (style, palette, &fg, &success, &warning, &error);
116
117 info = gtk_icon_info_new_for_file (url->file, 0, scale);
118 pixbuf = gtk_icon_info_load_symbolic (info, &fg, &success, &warning, &error, NULL, &local_error);
119 g_object_unref (info);
120
121 if (pixbuf == NULL)
122 {
123 cairo_surface_t *empty;
124
125 if (gerror)
126 {
127 char *uri;
128
129 uri = g_file_get_uri (url->file);
130 g_set_error (gerror,
131 GTK_CSS_PROVIDER_ERROR,
132 GTK_CSS_PROVIDER_ERROR_FAILED,
133 "Error loading image '%s': %s", uri, local_error->message);
134 g_error_free (local_error);
135 g_free (uri);
136 }
137
138 empty = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
139 image = _gtk_css_image_surface_new (empty);
140 cairo_surface_destroy (empty);
141 return image;
142 }
143
144 image = _gtk_css_image_surface_new_for_pixbuf (pixbuf);
145 g_object_unref (pixbuf);
146
147 return image;
148 }
149
150 static GtkCssImage *
gtk_css_image_recolor_compute(GtkCssImage * image,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)151 gtk_css_image_recolor_compute (GtkCssImage *image,
152 guint property_id,
153 GtkStyleProviderPrivate *provider,
154 GtkCssStyle *style,
155 GtkCssStyle *parent_style)
156 {
157 GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
158 GtkCssValue *palette;
159 GtkCssImage *img;
160 int scale;
161 GError *error = NULL;
162
163 scale = _gtk_style_provider_private_get_scale (provider);
164
165 if (recolor->palette)
166 palette = _gtk_css_value_compute (recolor->palette, property_id, provider, style, parent_style);
167 else
168 palette = _gtk_css_value_ref (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_PALETTE));
169
170 img = gtk_css_image_recolor_load (recolor, style, palette, scale, &error);
171
172 if (error)
173 {
174 GtkCssSection *section = gtk_css_style_get_section (style, property_id);
175 _gtk_style_provider_private_emit_error (provider, section, error);
176 g_error_free (error);
177 }
178
179 _gtk_css_value_unref (palette);
180
181 return img;
182 }
183
184 static gboolean
gtk_css_image_recolor_parse(GtkCssImage * image,GtkCssParser * parser)185 gtk_css_image_recolor_parse (GtkCssImage *image,
186 GtkCssParser *parser)
187 {
188 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
189 GtkCssImageRecolor *recolor = GTK_CSS_IMAGE_RECOLOR (image);
190
191 if (!_gtk_css_parser_try (parser, "-gtk-recolor", TRUE))
192 {
193 _gtk_css_parser_error (parser, "'-gtk-recolor'");
194 return FALSE;
195 }
196
197 if (!_gtk_css_parser_try (parser, "(", TRUE))
198 {
199 _gtk_css_parser_error (parser, "Expected '(' after '-gtk-recolor'");
200 return FALSE;
201 }
202
203 url->file = _gtk_css_parser_read_url (parser);
204 if (url->file == NULL)
205 {
206 _gtk_css_parser_error (parser, "Expected a url here");
207 return FALSE;
208 }
209
210 if ( _gtk_css_parser_try (parser, ",", TRUE))
211 {
212 recolor->palette = gtk_css_palette_value_parse (parser);
213 if (recolor->palette == NULL)
214 {
215 _gtk_css_parser_error (parser, "A palette is required here");
216 return FALSE;
217 }
218 }
219
220 if (!_gtk_css_parser_try (parser, ")", TRUE))
221 {
222 _gtk_css_parser_error (parser,
223 "Expected ')' at end of '-gtk-recolor'");
224 return FALSE;
225 }
226
227 return TRUE;
228 }
229
230 static void
_gtk_css_image_recolor_class_init(GtkCssImageRecolorClass * klass)231 _gtk_css_image_recolor_class_init (GtkCssImageRecolorClass *klass)
232 {
233 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
234 GObjectClass *object_class = G_OBJECT_CLASS (klass);
235
236 image_class->parse = gtk_css_image_recolor_parse;
237 image_class->compute = gtk_css_image_recolor_compute;
238 image_class->print = gtk_css_image_recolor_print;
239
240 object_class->dispose = gtk_css_image_recolor_dispose;
241 }
242
243 static void
_gtk_css_image_recolor_init(GtkCssImageRecolor * image_recolor)244 _gtk_css_image_recolor_init (GtkCssImageRecolor *image_recolor)
245 {
246 }
247