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"
24 typedef struct StylePropertyValue StylePropertyValue;
26 struct _GtkModifierStylePrivate
28 GtkStyleProperties *style;
29 GHashTable *color_properties;
37 static guint signals [LAST_SIGNAL] = { 0 };
39 static void gtk_modifier_style_provider_init (GtkStyleProviderIface *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));
47 _gtk_modifier_style_class_init (GtkModifierStyleClass *klass)
49 GObjectClass *object_class;
51 object_class = G_OBJECT_CLASS (klass);
53 object_class->finalize = gtk_modifier_style_finalize;
56 g_signal_new (I_("changed"),
57 G_TYPE_FROM_CLASS (object_class),
60 g_cclosure_marshal_VOID__VOID,
63 g_type_class_add_private (object_class, sizeof (GtkModifierStylePrivate));
67 _gtk_modifier_style_init (GtkModifierStyle *modifier_style)
69 GtkModifierStylePrivate *priv;
71 priv = modifier_style->priv = G_TYPE_INSTANCE_GET_PRIVATE (modifier_style,
72 GTK_TYPE_MODIFIER_STYLE,
73 GtkModifierStylePrivate);
75 priv->color_properties = g_hash_table_new_full (g_str_hash,
77 (GDestroyNotify) g_free,
78 (GDestroyNotify) gdk_rgba_free);
79 priv->style = gtk_style_properties_new ();
82 static GtkStyleProperties *
83 gtk_modifier_style_get_style (GtkStyleProvider *provider,
86 GtkModifierStylePrivate *priv;
88 priv = GTK_MODIFIER_STYLE (provider)->priv;
89 return g_object_ref (priv->style);
93 gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
99 GtkModifierStylePrivate *priv;
104 /* Reject non-color types for now */
105 if (pspec->value_type != GDK_TYPE_COLOR)
108 priv = GTK_MODIFIER_STYLE (provider)->priv;
109 str = g_strdup_printf ("-%s-%s",
110 g_type_name (pspec->owner_type),
113 rgba = g_hash_table_lookup (priv->color_properties, str);
119 color.red = (guint) (rgba->red * 65535.) + 0.5;
120 color.green = (guint) (rgba->green * 65535.) + 0.5;
121 color.blue = (guint) (rgba->blue * 65535.) + 0.5;
123 g_value_set_boxed (value, &color);
128 gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
130 iface->get_style = gtk_modifier_style_get_style;
131 iface->get_style_property = gtk_modifier_style_get_style_property;
135 gtk_modifier_style_finalize (GObject *object)
137 GtkModifierStylePrivate *priv;
139 priv = GTK_MODIFIER_STYLE (object)->priv;
140 g_hash_table_destroy (priv->color_properties);
141 g_object_unref (priv->style);
143 G_OBJECT_CLASS (_gtk_modifier_style_parent_class)->finalize (object);
147 _gtk_modifier_style_new (void)
149 return g_object_new (GTK_TYPE_MODIFIER_STYLE, NULL);
153 modifier_style_set_color (GtkModifierStyle *style,
156 const GdkRGBA *color)
158 GtkModifierStylePrivate *priv;
160 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
165 gtk_style_properties_set (priv->style, state,
169 gtk_style_properties_unset_property (priv->style, prop, state);
171 g_signal_emit (style, signals[CHANGED], 0);
175 _gtk_modifier_style_set_background_color (GtkModifierStyle *style,
177 const GdkRGBA *color)
179 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
181 modifier_style_set_color (style, "background-color", state, color);
185 _gtk_modifier_style_set_color (GtkModifierStyle *style,
187 const GdkRGBA *color)
189 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
191 modifier_style_set_color (style, "color", state, color);
195 _gtk_modifier_style_set_font (GtkModifierStyle *style,
196 const PangoFontDescription *font_desc)
198 GtkModifierStylePrivate *priv;
200 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
205 gtk_style_properties_set (priv->style, 0,
209 gtk_style_properties_unset_property (priv->style, "font", 0);
211 g_signal_emit (style, signals[CHANGED], 0);
215 _gtk_modifier_style_map_color (GtkModifierStyle *style,
217 const GdkRGBA *color)
219 GtkModifierStylePrivate *priv;
220 GtkSymbolicColor *symbolic_color = NULL;
222 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
223 g_return_if_fail (name != NULL);
228 symbolic_color = gtk_symbolic_color_new_literal (color);
230 gtk_style_properties_map_color (priv->style,
231 name, symbolic_color);
233 g_signal_emit (style, signals[CHANGED], 0);
237 _gtk_modifier_style_set_color_property (GtkModifierStyle *style,
239 const gchar *prop_name,
240 const GdkRGBA *color)
242 GtkModifierStylePrivate *priv;
243 const GdkRGBA *old_color;
246 g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
247 g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
248 g_return_if_fail (prop_name != NULL);
251 str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);
253 old_color = g_hash_table_lookup (priv->color_properties, str);
255 if ((!color && !old_color) ||
256 (color && old_color && gdk_rgba_equal (color, old_color)))
264 g_hash_table_insert (priv->color_properties, str,
265 gdk_rgba_copy (color));
269 g_hash_table_remove (priv->color_properties, str);
273 g_signal_emit (style, signals[CHANGED], 0);