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 ();
84 static GtkStyleProperties *
85 gtk_modifier_style_get_style (GtkStyleProvider *provider,
88 GtkModifierStylePrivate *priv;
90 priv = GTK_MODIFIER_STYLE (provider)->priv;
91 return g_object_ref (priv->style);
95 gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
101 GtkModifierStylePrivate *priv;
106 /* Reject non-color types for now */
107 if (pspec->value_type != GDK_TYPE_COLOR)
110 priv = GTK_MODIFIER_STYLE (provider)->priv;
111 str = g_strdup_printf ("-%s-%s",
112 g_type_name (pspec->owner_type),
115 rgba = g_hash_table_lookup (priv->color_properties, str);
121 color.red = (guint) (rgba->red * 65535.) + 0.5;
122 color.green = (guint) (rgba->green * 65535.) + 0.5;
123 color.blue = (guint) (rgba->blue * 65535.) + 0.5;
125 g_value_set_boxed (value, &color);
130 gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
132 iface->get_style = gtk_modifier_style_get_style;
133 iface->get_style_property = gtk_modifier_style_get_style_property;
136 static GtkSymbolicColor *
137 gtk_modifier_style_provider_get_color (GtkStyleProviderPrivate *provider,
140 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
142 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style), name);
146 gtk_modifier_style_provider_lookup (GtkStyleProviderPrivate *provider,
147 const GtkCssMatcher *matcher,
148 GtkCssLookup *lookup)
150 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
152 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style),
158 gtk_modifier_style_provider_private_init (GtkStyleProviderPrivateInterface *iface)
160 iface->get_color = gtk_modifier_style_provider_get_color;
161 iface->lookup = gtk_modifier_style_provider_lookup;
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);
205 _gtk_modifier_style_set_background_color (GtkModifierStyle *style,
207 const GdkRGBA *color)
209 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
211 modifier_style_set_color (style, "background-color", state, color);
215 _gtk_modifier_style_set_color (GtkModifierStyle *style,
217 const GdkRGBA *color)
219 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
221 modifier_style_set_color (style, "color", state, color);
225 _gtk_modifier_style_set_font (GtkModifierStyle *style,
226 const PangoFontDescription *font_desc)
228 GtkModifierStylePrivate *priv;
230 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
235 gtk_style_properties_set (priv->style, 0,
239 gtk_style_properties_unset_property (priv->style, "font", 0);
241 g_signal_emit (style, signals[CHANGED], 0);
245 _gtk_modifier_style_map_color (GtkModifierStyle *style,
247 const GdkRGBA *color)
249 GtkModifierStylePrivate *priv;
250 GtkSymbolicColor *symbolic_color = NULL;
252 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
253 g_return_if_fail (name != NULL);
258 symbolic_color = gtk_symbolic_color_new_literal (color);
260 gtk_style_properties_map_color (priv->style,
261 name, symbolic_color);
263 g_signal_emit (style, signals[CHANGED], 0);
267 _gtk_modifier_style_set_color_property (GtkModifierStyle *style,
269 const gchar *prop_name,
270 const GdkRGBA *color)
272 GtkModifierStylePrivate *priv;
273 const GdkRGBA *old_color;
276 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
277 g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
278 g_return_if_fail (prop_name != NULL);
281 str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);
283 old_color = g_hash_table_lookup (priv->color_properties, str);
285 if ((!color && !old_color) ||
286 (color && old_color && gdk_rgba_equal (color, old_color)))
294 g_hash_table_insert (priv->color_properties, str,
295 gdk_rgba_copy (color));
299 g_hash_table_remove (priv->color_properties, str);
303 g_signal_emit (style, signals[CHANGED], 0);