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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkmodifierstyle.h"
22 #include "gtkstyleproviderprivate.h"
25 typedef struct StylePropertyValue StylePropertyValue;
27 struct _GtkModifierStylePrivate
29 GtkStyleProperties *style;
30 GHashTable *color_properties;
38 static guint signals [LAST_SIGNAL] = { 0 };
40 static void gtk_modifier_style_provider_init (GtkStyleProviderIface *iface);
41 static void gtk_modifier_style_provider_private_init (GtkStyleProviderPrivateInterface *iface);
42 static void gtk_modifier_style_finalize (GObject *object);
44 G_DEFINE_TYPE_EXTENDED (GtkModifierStyle, _gtk_modifier_style, G_TYPE_OBJECT, 0,
45 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
46 gtk_modifier_style_provider_init)
47 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
48 gtk_modifier_style_provider_private_init));
51 _gtk_modifier_style_class_init (GtkModifierStyleClass *klass)
53 GObjectClass *object_class;
55 object_class = G_OBJECT_CLASS (klass);
57 object_class->finalize = gtk_modifier_style_finalize;
60 g_signal_new (I_("changed"),
61 G_TYPE_FROM_CLASS (object_class),
64 g_cclosure_marshal_VOID__VOID,
67 g_type_class_add_private (object_class, sizeof (GtkModifierStylePrivate));
71 _gtk_modifier_style_init (GtkModifierStyle *modifier_style)
73 GtkModifierStylePrivate *priv;
75 priv = modifier_style->priv = G_TYPE_INSTANCE_GET_PRIVATE (modifier_style,
76 GTK_TYPE_MODIFIER_STYLE,
77 GtkModifierStylePrivate);
79 priv->color_properties = g_hash_table_new_full (g_str_hash,
81 (GDestroyNotify) g_free,
82 (GDestroyNotify) gdk_rgba_free);
83 priv->style = gtk_style_properties_new ();
86 static GtkStyleProperties *
87 gtk_modifier_style_get_style (GtkStyleProvider *provider,
90 GtkModifierStylePrivate *priv;
92 priv = GTK_MODIFIER_STYLE (provider)->priv;
93 return g_object_ref (priv->style);
97 gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
103 GtkModifierStylePrivate *priv;
108 /* Reject non-color types for now */
109 if (pspec->value_type != GDK_TYPE_COLOR)
112 priv = GTK_MODIFIER_STYLE (provider)->priv;
113 str = g_strdup_printf ("-%s-%s",
114 g_type_name (pspec->owner_type),
117 rgba = g_hash_table_lookup (priv->color_properties, str);
123 color.red = (guint) (rgba->red * 65535.) + 0.5;
124 color.green = (guint) (rgba->green * 65535.) + 0.5;
125 color.blue = (guint) (rgba->blue * 65535.) + 0.5;
127 g_value_set_boxed (value, &color);
132 gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
134 iface->get_style = gtk_modifier_style_get_style;
135 iface->get_style_property = gtk_modifier_style_get_style_property;
138 static GtkSymbolicColor *
139 gtk_modifier_style_provider_get_color (GtkStyleProviderPrivate *provider,
142 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
144 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style), name);
148 gtk_modifier_style_provider_lookup (GtkStyleProviderPrivate *provider,
151 GtkCssLookup *lookup)
153 GtkModifierStyle *style = GTK_MODIFIER_STYLE (provider);
155 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (style->priv->style),
162 gtk_modifier_style_provider_private_init (GtkStyleProviderPrivateInterface *iface)
164 iface->get_color = gtk_modifier_style_provider_get_color;
165 iface->lookup = gtk_modifier_style_provider_lookup;
169 gtk_modifier_style_finalize (GObject *object)
171 GtkModifierStylePrivate *priv;
173 priv = GTK_MODIFIER_STYLE (object)->priv;
174 g_hash_table_destroy (priv->color_properties);
175 g_object_unref (priv->style);
177 G_OBJECT_CLASS (_gtk_modifier_style_parent_class)->finalize (object);
181 _gtk_modifier_style_new (void)
183 return g_object_new (GTK_TYPE_MODIFIER_STYLE, NULL);
187 modifier_style_set_color (GtkModifierStyle *style,
190 const GdkRGBA *color)
192 GtkModifierStylePrivate *priv;
194 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
199 gtk_style_properties_set (priv->style, state,
203 gtk_style_properties_unset_property (priv->style, prop, state);
205 g_signal_emit (style, signals[CHANGED], 0);
209 _gtk_modifier_style_set_background_color (GtkModifierStyle *style,
211 const GdkRGBA *color)
213 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
215 modifier_style_set_color (style, "background-color", state, color);
219 _gtk_modifier_style_set_color (GtkModifierStyle *style,
221 const GdkRGBA *color)
223 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
225 modifier_style_set_color (style, "color", state, color);
229 _gtk_modifier_style_set_font (GtkModifierStyle *style,
230 const PangoFontDescription *font_desc)
232 GtkModifierStylePrivate *priv;
234 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
239 gtk_style_properties_set (priv->style, 0,
243 gtk_style_properties_unset_property (priv->style, "font", 0);
245 g_signal_emit (style, signals[CHANGED], 0);
249 _gtk_modifier_style_map_color (GtkModifierStyle *style,
251 const GdkRGBA *color)
253 GtkModifierStylePrivate *priv;
254 GtkSymbolicColor *symbolic_color = NULL;
256 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
257 g_return_if_fail (name != NULL);
262 symbolic_color = gtk_symbolic_color_new_literal (color);
264 gtk_style_properties_map_color (priv->style,
265 name, symbolic_color);
267 g_signal_emit (style, signals[CHANGED], 0);
271 _gtk_modifier_style_set_color_property (GtkModifierStyle *style,
273 const gchar *prop_name,
274 const GdkRGBA *color)
276 GtkModifierStylePrivate *priv;
277 const GdkRGBA *old_color;
280 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
281 g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
282 g_return_if_fail (prop_name != NULL);
285 str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);
287 old_color = g_hash_table_lookup (priv->color_properties, str);
289 if ((!color && !old_color) ||
290 (color && old_color && gdk_rgba_equal (color, old_color)))
298 g_hash_table_insert (priv->color_properties, str,
299 gdk_rgba_copy (color));
303 g_hash_table_remove (priv->color_properties, str);
307 g_signal_emit (style, signals[CHANGED], 0);