1 /*
2  * Copyright © 2012 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 "gtkcssimageiconthemeprivate.h"
23 
24 #include <math.h>
25 
26 #include "gtkcssiconthemevalueprivate.h"
27 #include "gtkcssrgbavalueprivate.h"
28 #include "gtksettingsprivate.h"
29 #include "gtkstyleproviderprivate.h"
30 #include "gtkiconthemeprivate.h"
31 
G_DEFINE_TYPE(GtkCssImageIconTheme,_gtk_css_image_icon_theme,GTK_TYPE_CSS_IMAGE)32 G_DEFINE_TYPE (GtkCssImageIconTheme, _gtk_css_image_icon_theme, GTK_TYPE_CSS_IMAGE)
33 
34 static double
35 gtk_css_image_icon_theme_get_aspect_ratio (GtkCssImage *image)
36 {
37   /* icon theme icons only take a single size when requesting, so we insist on being square */
38   return 1.0;
39 }
40 
41 static void
gtk_css_image_icon_theme_draw(GtkCssImage * image,cairo_t * cr,double width,double height)42 gtk_css_image_icon_theme_draw (GtkCssImage        *image,
43                                cairo_t            *cr,
44                                double              width,
45                                double              height)
46 {
47   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
48   GError *error = NULL;
49   GtkIconInfo *icon_info;
50   GdkPixbuf *pixbuf;
51   gint size;
52 
53   size = floor (MIN (width, height));
54   if (size <= 0)
55     return;
56 
57   icon_info = gtk_icon_theme_lookup_icon_for_scale (icon_theme->icon_theme,
58                                                     icon_theme->name,
59                                                     size,
60                                                     icon_theme->scale,
61                                                     GTK_ICON_LOOKUP_USE_BUILTIN);
62   if (icon_info == NULL)
63     {
64       /* XXX: render missing icon image here? */
65       return;
66     }
67 
68   pixbuf = gtk_icon_info_load_symbolic (icon_info,
69                                         &icon_theme->color,
70                                         &icon_theme->success,
71                                         &icon_theme->warning,
72                                         &icon_theme->error,
73                                         NULL,
74                                         &error);
75   if (pixbuf == NULL)
76     {
77       /* XXX: render missing icon image here? */
78       g_error_free (error);
79       return;
80     }
81 
82   cairo_translate (cr, width / 2.0, height / 2.0);
83   cairo_scale (cr, 1.0 / icon_theme->scale, 1.0 / icon_theme->scale);
84   gdk_cairo_set_source_pixbuf (cr,
85                                pixbuf,
86                                - gdk_pixbuf_get_width (pixbuf) / 2.0,
87                                - gdk_pixbuf_get_height (pixbuf) / 2.0);
88   cairo_paint (cr);
89 
90   g_object_unref (pixbuf);
91   g_object_unref (icon_info);
92 }
93 
94 
95 static gboolean
gtk_css_image_icon_theme_parse(GtkCssImage * image,GtkCssParser * parser)96 gtk_css_image_icon_theme_parse (GtkCssImage  *image,
97                                 GtkCssParser *parser)
98 {
99   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
100 
101   if (!_gtk_css_parser_try (parser, "-gtk-icontheme(", TRUE))
102     {
103       _gtk_css_parser_error (parser, "Expected '-gtk-icontheme('");
104       return FALSE;
105     }
106 
107   icon_theme->name = _gtk_css_parser_read_string (parser);
108   if (icon_theme->name == NULL)
109     return FALSE;
110 
111   if (!_gtk_css_parser_try (parser, ")", TRUE))
112     {
113       _gtk_css_parser_error (parser, "Missing closing bracket at end of '-gtk-icontheme'");
114       return FALSE;
115     }
116 
117   return TRUE;
118 }
119 
120 static void
gtk_css_image_icon_theme_print(GtkCssImage * image,GString * string)121 gtk_css_image_icon_theme_print (GtkCssImage *image,
122                                 GString     *string)
123 {
124   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
125 
126   g_string_append (string, "-gtk-icontheme(");
127   _gtk_css_print_string (string, icon_theme->name);
128   g_string_append (string, ")");
129 }
130 
131 static GtkCssImage *
gtk_css_image_icon_theme_compute(GtkCssImage * image,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)132 gtk_css_image_icon_theme_compute (GtkCssImage             *image,
133                                   guint                    property_id,
134                                   GtkStyleProviderPrivate *provider,
135                                   GtkCssStyle             *style,
136                                   GtkCssStyle             *parent_style)
137 {
138   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
139   GtkCssImageIconTheme *copy;
140 
141   copy = g_object_new (GTK_TYPE_CSS_IMAGE_ICON_THEME, NULL);
142   copy->name = g_strdup (icon_theme->name);
143   copy->icon_theme = gtk_css_icon_theme_value_get_icon_theme (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_THEME));
144   copy->scale = _gtk_style_provider_private_get_scale (provider);
145   gtk_icon_theme_lookup_symbolic_colors (style, &copy->color, &copy->success, &copy->warning, &copy->error);
146 
147   return GTK_CSS_IMAGE (copy);
148 }
149 
150 static gboolean
gtk_css_image_icon_theme_equal(GtkCssImage * image1,GtkCssImage * image2)151 gtk_css_image_icon_theme_equal (GtkCssImage *image1,
152                                 GtkCssImage *image2)
153 {
154   GtkCssImageIconTheme *icon_theme1 = GTK_CSS_IMAGE_ICON_THEME (image1);
155   GtkCssImageIconTheme *icon_theme2 = GTK_CSS_IMAGE_ICON_THEME (image2);
156 
157   return g_str_equal (icon_theme1->name, icon_theme2->name);
158 }
159 
160 static void
gtk_css_image_icon_theme_dispose(GObject * object)161 gtk_css_image_icon_theme_dispose (GObject *object)
162 {
163   GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (object);
164 
165   g_free (icon_theme->name);
166   icon_theme->name = NULL;
167 
168   G_OBJECT_CLASS (_gtk_css_image_icon_theme_parent_class)->dispose (object);
169 }
170 
171 static void
_gtk_css_image_icon_theme_class_init(GtkCssImageIconThemeClass * klass)172 _gtk_css_image_icon_theme_class_init (GtkCssImageIconThemeClass *klass)
173 {
174   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
175   GObjectClass *object_class = G_OBJECT_CLASS (klass);
176 
177   image_class->get_aspect_ratio = gtk_css_image_icon_theme_get_aspect_ratio;
178   image_class->draw = gtk_css_image_icon_theme_draw;
179   image_class->parse = gtk_css_image_icon_theme_parse;
180   image_class->print = gtk_css_image_icon_theme_print;
181   image_class->compute = gtk_css_image_icon_theme_compute;
182   image_class->equal = gtk_css_image_icon_theme_equal;
183 
184   object_class->dispose = gtk_css_image_icon_theme_dispose;
185 }
186 
187 static void
_gtk_css_image_icon_theme_init(GtkCssImageIconTheme * icon_theme)188 _gtk_css_image_icon_theme_init (GtkCssImageIconTheme *icon_theme)
189 {
190   icon_theme->icon_theme = gtk_icon_theme_get_default ();
191   icon_theme->scale = 1;
192 }
193 
194