1/* GTK - The GIMP Toolkit
2 * Copyright (C) 2014,2015 Benjamin Otte
3 *
4 * Authors: Benjamin Otte <otte@gnome.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config.h"
21
22#include "gtkrendericonprivate.h"
23
24#include "gtkcssfiltervalueprivate.h"
25#include "gtkcssimagevalueprivate.h"
26#include "gtkcssshadowvalueprivate.h"
27#include "gtkcssstyleprivate.h"
28#include "gtkcsstransformvalueprivate.h"
29#include "gtkiconthemeprivate.h"
30#include "gtksnapshot.h"
31#include "gtksymbolicpaintable.h"
32#include "gsktransform.h"
33
34#include <math.h>
35
36void
37gtk_css_style_snapshot_icon (GtkCssStyle *style,
38 GtkSnapshot *snapshot,
39 double width,
40 double height)
41{
42 GskTransform *transform;
43 GtkCssImage *image;
44 gboolean has_shadow;
45
46 g_return_if_fail (GTK_IS_CSS_STYLE (style));
47 g_return_if_fail (snapshot != NULL);
48
49 if (width == 0.0 || height == 0.0)
50 return;
51
52 image = _gtk_css_image_value_get_image (image: style->other->icon_source);
53 if (image == NULL)
54 return;
55
56 transform = gtk_css_transform_value_get_transform (transform: style->other->icon_transform);
57
58 gtk_snapshot_push_debug (snapshot, message: "CSS Icon @ %gx%g", width, height);
59
60 gtk_css_filter_value_push_snapshot (filter: style->other->icon_filter, snapshot);
61
62 has_shadow = gtk_css_shadow_value_push_snapshot (value: style->icon->icon_shadow, snapshot);
63
64 if (transform == NULL)
65 {
66 gtk_css_image_snapshot (image, snapshot, width, height);
67 }
68 else
69 {
70 gtk_snapshot_save (snapshot);
71
72 /* XXX: Implement -gtk-icon-transform-origin instead of hardcoding "50% 50%" here */
73 gtk_snapshot_translate (snapshot, point: &GRAPHENE_POINT_INIT (width / 2.0, height / 2.0));
74 gtk_snapshot_transform (snapshot, transform);
75 gtk_snapshot_translate (snapshot, point: &GRAPHENE_POINT_INIT (- width / 2.0, - height / 2.0));
76
77 gtk_css_image_snapshot (image, snapshot, width, height);
78
79 gtk_snapshot_restore (snapshot);
80 }
81
82 if (has_shadow)
83 gtk_snapshot_pop (snapshot);
84
85 gtk_css_filter_value_pop_snapshot (filter: style->other->icon_filter, snapshot);
86
87 gtk_snapshot_pop (snapshot);
88
89 gsk_transform_unref (self: transform);
90}
91
92void
93gtk_css_style_snapshot_icon_paintable (GtkCssStyle *style,
94 GtkSnapshot *snapshot,
95 GdkPaintable *paintable,
96 double width,
97 double height)
98{
99 GskTransform *transform;
100 gboolean has_shadow;
101 gboolean is_symbolic_paintable;
102 GdkRGBA colors[4];
103
104 g_return_if_fail (GTK_IS_CSS_STYLE (style));
105 g_return_if_fail (snapshot != NULL);
106 g_return_if_fail (GDK_IS_PAINTABLE (paintable));
107 g_return_if_fail (width > 0);
108 g_return_if_fail (height > 0);
109
110 transform = gtk_css_transform_value_get_transform (transform: style->other->icon_transform);
111
112 gtk_css_filter_value_push_snapshot (filter: style->other->icon_filter, snapshot);
113
114 has_shadow = gtk_css_shadow_value_push_snapshot (value: style->icon->icon_shadow, snapshot);
115
116 is_symbolic_paintable = GTK_IS_SYMBOLIC_PAINTABLE (ptr: paintable);
117 if (is_symbolic_paintable)
118 {
119 gtk_icon_theme_lookup_symbolic_colors (style, color_out: colors);
120
121 if (gdk_rgba_is_clear (rgba: &colors[0]))
122 goto transparent;
123 }
124
125 if (transform == NULL)
126 {
127 if (is_symbolic_paintable)
128 gtk_symbolic_paintable_snapshot_symbolic (paintable: GTK_SYMBOLIC_PAINTABLE (ptr: paintable), snapshot, width, height, colors, G_N_ELEMENTS (colors));
129 else
130 gdk_paintable_snapshot (paintable, snapshot, width, height);
131 }
132 else
133 {
134 gtk_snapshot_save (snapshot);
135
136 /* XXX: Implement -gtk-icon-transform-origin instead of hardcoding "50% 50%" here */
137 gtk_snapshot_translate (snapshot, point: &GRAPHENE_POINT_INIT (width / 2.0, height / 2.0));
138 gtk_snapshot_transform (snapshot, transform);
139 gtk_snapshot_translate (snapshot, point: &GRAPHENE_POINT_INIT (- width / 2.0, - height / 2.0));
140
141 if (is_symbolic_paintable)
142 gtk_symbolic_paintable_snapshot_symbolic (paintable: GTK_SYMBOLIC_PAINTABLE (ptr: paintable), snapshot, width, height, colors, G_N_ELEMENTS (colors));
143 else
144 gdk_paintable_snapshot (paintable, snapshot, width, height);
145
146 gtk_snapshot_restore (snapshot);
147 }
148
149transparent:
150 if (has_shadow)
151 gtk_snapshot_pop (snapshot);
152
153 gtk_css_filter_value_pop_snapshot (filter: style->other->icon_filter, snapshot);
154
155 gsk_transform_unref (self: transform);
156}
157

source code of gtk/gtk/gtkrendericon.c