1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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 of the License, or (at your option) any later version.
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.
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/>.
19 #include "gtkmodifierstyle.h"
20 #include "gtkstyleproviderprivate.h"
23 typedef struct StylePropertyValue StylePropertyValue;
25 struct _GtkModifierStylePrivate
27 GtkStyleProperties *style;
28 GHashTable *color_properties;
36 static guint signals [LAST_SIGNAL] = { 0 };
38 static void gtk_modifier_style_provider_init (GtkStyleProviderIface *iface);
39 static void gtk_modifier_style_provider_private_init (GtkStyleProviderPrivateInterface *iface);
40 static void gtk_modifier_style_finalize (GObject *object);
42 G_DEFINE_TYPE_EXTENDED (GtkModifierStyle, _gtk_modifier_style, G_TYPE_OBJECT, 0,
43 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
44 gtk_modifier_style_provider_init)
45 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
46 gtk_modifier_style_provider_private_init));
49 _gtk_modifier_style_class_init (GtkModifierStyleClass *klass)
51 GObjectClass *object_class;
53 object_class = G_OBJECT_CLASS (klass);
55 object_class->finalize = gtk_modifier_style_finalize;
58 g_signal_new (I_("changed"),
59 G_TYPE_FROM_CLASS (object_class),
62 g_cclosure_marshal_VOID__VOID,
65 g_type_class_add_private (object_class, sizeof (GtkModifierStylePrivate));
69 _gtk_modifier_style_init (GtkModifierStyle *modifier_style)
71 GtkModifierStylePrivate *priv;
73 priv = modifier_style->priv = G_TYPE_INSTANCE_GET_PRIVATE (modifier_style,
74 GTK_TYPE_MODIFIER_STYLE,
75 GtkModifierStylePrivate);
77 priv->color_properties = g_hash_table_new_full (g_str_hash,
79 (GDestroyNotify) g_free,
80 (GDestroyNotify) gdk_rgba_free);
81 priv->style = gtk_style_properties_new ();
85 gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
91 GtkModifierStylePrivate *priv;
96 /* Reject non-color types for now */
97 if (pspec->value_type != GDK_TYPE_COLOR)
100 priv = GTK_MODIFIER_STYLE (provider)->priv;
101 str = g_strdup_printf ("-%s-%s",
102 g_type_name (pspec->owner_type),
105 rgba = g_hash_table_lookup (priv->color_properties, str);
111 color.red = (guint) (rgba->red * 65535.) + 0.5;
112 color.green = (guint) (rgba->green * 65535.) + 0.5;
113 color.blue = (guint) (rgba->blue * 65535.) + 0.5;
115 g_value_set_boxed (value, &color);
120 gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
122 iface->get_style_property = gtk_modifier_style_get_style_property;
126 gtk_modifier_style_provider_get_color (GtkStyleProviderPrivate *provider,
129 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
131 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style), name);
135 gtk_modifier_style_provider_lookup (GtkStyleProviderPrivate *provider,
136 const GtkCssMatcher *matcher,
137 GtkCssLookup *lookup)
139 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
141 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style),
147 gtk_modifier_style_provider_get_change (GtkStyleProviderPrivate *provider,
148 const GtkCssMatcher *matcher)
150 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
152 return _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style),
157 gtk_modifier_style_provider_private_init (GtkStyleProviderPrivateInterface *iface)
159 iface->get_color = gtk_modifier_style_provider_get_color;
160 iface->lookup = gtk_modifier_style_provider_lookup;
161 iface->get_change = gtk_modifier_style_provider_get_change;
165 gtk_modifier_style_finalize (GObject *object)
167 GtkModifierStylePrivate *priv;
169 priv = GTK_MODIFIER_STYLE (object)->priv;
170 g_hash_table_destroy (priv->color_properties);
171 g_object_unref (priv->style);
173 G_OBJECT_CLASS (_gtk_modifier_style_parent_class)->finalize (object);
177 _gtk_modifier_style_new (void)
179 return g_object_new (GTK_TYPE_MODIFIER_STYLE, NULL);
183 modifier_style_set_color (GtkModifierStyle *style,
186 const GdkRGBA *color)
188 GtkModifierStylePrivate *priv;
190 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
195 gtk_style_properties_set (priv->style, state,
199 gtk_style_properties_unset_property (priv->style, prop, state);
201 g_signal_emit (style, signals[CHANGED], 0);
202 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (style));
206 _gtk_modifier_style_set_background_color (GtkModifierStyle *style,
208 const GdkRGBA *color)
210 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
212 modifier_style_set_color (style, "background-color", state, color);
216 _gtk_modifier_style_set_color (GtkModifierStyle *style,
218 const GdkRGBA *color)
220 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
222 modifier_style_set_color (style, "color", state, color);
226 _gtk_modifier_style_set_font (GtkModifierStyle *style,
227 const PangoFontDescription *font_desc)
229 GtkModifierStylePrivate *priv;
231 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
236 gtk_style_properties_set (priv->style, 0,
240 gtk_style_properties_unset_property (priv->style, "font", 0);
242 g_signal_emit (style, signals[CHANGED], 0);
243 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (style));
247 _gtk_modifier_style_map_color (GtkModifierStyle *style,
249 const GdkRGBA *color)
251 GtkModifierStylePrivate *priv;
252 GtkSymbolicColor *symbolic_color = NULL;
254 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
255 g_return_if_fail (name != NULL);
259 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
262 symbolic_color = gtk_symbolic_color_new_literal (color);
264 gtk_style_properties_map_color (priv->style,
265 name, symbolic_color);
267 G_GNUC_END_IGNORE_DEPRECATIONS;
269 g_signal_emit (style, signals[CHANGED], 0);
270 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (style));
274 _gtk_modifier_style_set_color_property (GtkModifierStyle *style,
276 const gchar *prop_name,
277 const GdkRGBA *color)
279 GtkModifierStylePrivate *priv;
280 const GdkRGBA *old_color;
283 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
284 g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
285 g_return_if_fail (prop_name != NULL);
288 str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);
290 old_color = g_hash_table_lookup (priv->color_properties, str);
292 if ((!color && !old_color) ||
293 (color && old_color && gdk_rgba_equal (color, old_color)))
301 g_hash_table_insert (priv->color_properties, str,
302 gdk_rgba_copy (color));
306 g_hash_table_remove (priv->color_properties, str);
310 g_signal_emit (style, signals[CHANGED], 0);
311 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (style));