[gtk+/gtk-style-context: 536/540] Add GtkModifierStyle as a private object



commit 19f752958871e578a497950edb6543dc2b55fc06
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Dec 2 20:28:09 2010 +0100

    Add GtkModifierStyle as a private object
    
    This object backs up gtk_widget_override_* operations. This object
    is not meant to be public because any intention to modify widgets'
    style in a themeable way should involve using regions/classes, so
    they're modifiable through CSS. As such, the API is really
    short-scoped.

 gtk/Makefile.am        |    2 +
 gtk/gtkmodifierstyle.c |  287 ++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkmodifierstyle.h |   74 ++++++++++++
 gtk/gtkwidget.c        |  131 ++++++++--------------
 4 files changed, 412 insertions(+), 82 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index e69d293..d95f3de 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -397,6 +397,7 @@ gtk_private_h_sources =		\
 	gtkkeyhash.h		\
 	gtkmenuprivate.h	\
 	gtkmnemonichash.h	\
+	gtkmodifierstyle.h	\
 	gtkmountoperationprivate.h \
 	gtkappchooserprivate.h	\
 	gtkappchoosermodule.h 	\
@@ -552,6 +553,7 @@ gtk_base_c_sources =            \
 	gtkmessagedialog.c	\
 	gtkmisc.c		\
 	gtkmnemonichash.c	\
+	gtkmodifierstyle.c	\
 	gtkmodules.c		\
 	gtkmountoperation.c	\
 	gtknotebook.c		\
diff --git a/gtk/gtkmodifierstyle.c b/gtk/gtkmodifierstyle.c
new file mode 100644
index 0000000..a8f4d3d
--- /dev/null
+++ b/gtk/gtkmodifierstyle.c
@@ -0,0 +1,287 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2010 Carlos Garnacho <carlosg gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include "gtkmodifierstyle.h"
+#include "gtkintl.h"
+
+typedef struct GtkModifierStylePrivate GtkModifierStylePrivate;
+typedef struct StylePropertyValue StylePropertyValue;
+
+struct GtkModifierStylePrivate
+{
+  GtkStyleProperties *style;
+  GHashTable *color_properties;
+};
+
+enum {
+  CHANGED,
+  LAST_SIGNAL
+};
+
+static guint signals [LAST_SIGNAL] = { 0 };
+
+static void gtk_modifier_style_provider_init (GtkStyleProviderIface *iface);
+static void gtk_modifier_style_finalize      (GObject      *object);
+
+G_DEFINE_TYPE_EXTENDED (GtkModifierStyle, gtk_modifier_style, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
+                                               gtk_modifier_style_provider_init));
+
+static void
+gtk_modifier_style_class_init (GtkModifierStyleClass *klass)
+{
+  GObjectClass *object_class;
+
+  object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gtk_modifier_style_finalize;
+
+  signals[CHANGED] =
+    g_signal_new (I_("changed"),
+                  G_TYPE_FROM_CLASS (object_class),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE, 0);
+
+  g_type_class_add_private (object_class, sizeof (GtkModifierStylePrivate));
+}
+
+static void
+gtk_modifier_style_init (GtkModifierStyle *modifier_style)
+{
+  GtkModifierStylePrivate *priv;
+
+  priv = modifier_style->priv = G_TYPE_INSTANCE_GET_PRIVATE (modifier_style,
+                                                             GTK_TYPE_MODIFIER_STYLE,
+                                                             GtkModifierStylePrivate);
+
+  priv->color_properties = g_hash_table_new_full (g_str_hash,
+                                                  g_str_equal,
+                                                  (GDestroyNotify) g_free,
+                                                  (GDestroyNotify) gdk_rgba_free);
+  priv->style = gtk_style_properties_new ();
+}
+
+static GtkStyleProperties *
+gtk_modifier_style_get_style (GtkStyleProvider *provider,
+                              GtkWidgetPath    *path)
+{
+  GtkModifierStylePrivate *priv;
+
+  priv = GTK_MODIFIER_STYLE (provider)->priv;
+  return g_object_ref (priv->style);
+}
+
+static gboolean
+gtk_modifier_style_get_style_property (GtkStyleProvider *provider,
+                                       GtkWidgetPath    *path,
+                                       GtkStateFlags     state,
+                                       GParamSpec       *pspec,
+                                       GValue           *value)
+{
+  GtkModifierStylePrivate *priv;
+  GdkColor *color;
+  gchar *str;
+
+  /* Reject non-color types for now */
+  if (pspec->value_type != GDK_TYPE_COLOR)
+    return FALSE;
+
+  priv = GTK_MODIFIER_STYLE (provider)->priv;
+  str = g_strdup_printf ("-%s-%s",
+                         g_type_name (pspec->owner_type),
+                         pspec->name);
+
+  color = g_hash_table_lookup (priv->color_properties, str);
+  g_free (str);
+
+  if (!color)
+    return FALSE;
+
+  g_value_set_boxed (value, color);
+  return TRUE;
+}
+
+static void
+gtk_modifier_style_provider_init (GtkStyleProviderIface *iface)
+{
+  iface->get_style = gtk_modifier_style_get_style;
+  iface->get_style_property = gtk_modifier_style_get_style_property;
+}
+
+static void
+gtk_modifier_style_finalize (GObject *object)
+{
+  GtkModifierStylePrivate *priv;
+
+  priv = GTK_MODIFIER_STYLE (object)->priv;
+  g_hash_table_destroy (priv->color_properties);
+  g_object_unref (priv->style);
+
+  G_OBJECT_CLASS (gtk_modifier_style_parent_class)->finalize (object);
+}
+
+GtkModifierStyle *
+gtk_modifier_style_new (void)
+{
+  return g_object_new (GTK_TYPE_MODIFIER_STYLE, NULL);
+}
+
+static void
+modifier_style_set_color (GtkModifierStyle *style,
+                          const gchar      *prop,
+                          GtkStateFlags     state,
+                          const GdkRGBA    *color)
+{
+  GtkModifierStylePrivate *priv;
+  GdkRGBA *old_color;
+
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+
+  priv = style->priv;
+  gtk_style_properties_get (priv->style, state,
+                            prop, &old_color,
+                            NULL);
+
+  if ((!color && !old_color) ||
+      (color && old_color && gdk_rgba_equal (color, old_color)))
+    {
+      gdk_rgba_free (old_color);
+      return;
+    }
+
+  if (color)
+    gtk_style_properties_set (priv->style, state,
+                              prop, color,
+                              NULL);
+  else
+    gtk_style_properties_unset_property (priv->style, prop, state);
+
+  g_signal_emit (style, signals[CHANGED], 0);
+  gdk_rgba_free (old_color);
+}
+
+void
+gtk_modifier_style_set_background_color (GtkModifierStyle *style,
+                                         GtkStateFlags     state,
+                                         const GdkRGBA    *color)
+{
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+
+  modifier_style_set_color (style, "background-color", state, color);
+}
+
+void
+gtk_modifier_style_set_color (GtkModifierStyle *style,
+                              GtkStateFlags     state,
+                              const GdkRGBA    *color)
+{
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+
+  modifier_style_set_color (style, "color", state, color);
+}
+
+void
+gtk_modifier_style_set_font (GtkModifierStyle           *style,
+                             const PangoFontDescription *font_desc)
+{
+  GtkModifierStylePrivate *priv;
+  PangoFontDescription *old_font;
+
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+
+  priv = style->priv;
+  gtk_style_properties_get (priv->style, 0,
+                            "font", &old_font,
+                            NULL);
+
+  if ((!old_font && !font_desc) ||
+      (old_font && font_desc &&
+       pango_font_description_equal (old_font, font_desc)))
+    return;
+
+  if (font_desc)
+    gtk_style_properties_set (priv->style, 0,
+                              "font", font_desc,
+                              NULL);
+  else
+    gtk_style_properties_unset_property (priv->style, "font", 0);
+
+  g_signal_emit (style, signals[CHANGED], 0);
+}
+
+void
+gtk_modifier_style_map_color (GtkModifierStyle *style,
+                              const gchar      *name,
+                              const GdkRGBA    *color)
+{
+  GtkModifierStylePrivate *priv;
+  GtkSymbolicColor *symbolic_color = NULL;
+
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+  g_return_if_fail (name != NULL);
+
+  priv = style->priv;
+
+  if (color)
+    symbolic_color = gtk_symbolic_color_new_literal (color);
+
+  gtk_style_properties_map_color (priv->style,
+                                  name, symbolic_color);
+
+  g_signal_emit (style, signals[CHANGED], 0);
+}
+
+void
+gtk_modifier_style_set_color_property (GtkModifierStyle *style,
+                                       GType             widget_type,
+                                       const gchar      *prop_name,
+                                       const GdkColor   *color)
+{
+  GtkModifierStylePrivate *priv;
+  const GdkColor *old_color;
+  gchar *str;
+
+  g_return_if_fail (GTK_IS_MODIFIER_STYLE (style));
+  g_return_if_fail (g_type_is_a (widget_type, GTK_TYPE_WIDGET));
+  g_return_if_fail (prop_name != NULL);
+
+  priv = style->priv;
+  str = g_strdup_printf ("-%s-%s", g_type_name (widget_type), prop_name);
+
+  old_color = g_hash_table_lookup (priv->color_properties, str);
+
+  if ((!color && !old_color) ||
+      (color && old_color && gdk_color_equal (color, old_color)))
+    {
+      g_free (str);
+      return;
+    }
+
+  if (color)
+    g_hash_table_insert (priv->color_properties, str,
+                         gdk_color_copy (color));
+  else
+    g_hash_table_remove (priv->color_properties, str);
+
+  g_signal_emit (style, signals[CHANGED], 0);
+  g_free (str);
+}
diff --git a/gtk/gtkmodifierstyle.h b/gtk/gtkmodifierstyle.h
new file mode 100644
index 0000000..6275b7c
--- /dev/null
+++ b/gtk/gtkmodifierstyle.h
@@ -0,0 +1,74 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2010 Carlos Garnacho <carlosg gnome org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GTK_MODIFIER_STYLE_H__
+#define __GTK_MODIFIER_STYLE_H__
+
+#include <glib-object.h>
+#include <gdk/gdk.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_MODIFIER_STYLE         (gtk_modifier_style_get_type ())
+#define GTK_MODIFIER_STYLE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_MODIFIER_STYLE, GtkModifierStyle))
+#define GTK_MODIFIER_STYLE_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST    ((c), GTK_TYPE_MODIFIER_STYLE, GtkModifierStyleClass))
+#define GTK_IS_MODIFIER_STYLE(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_MODIFIER_STYLE))
+#define GTK_IS_MODIFIER_STYLE_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE    ((c), GTK_TYPE_MODIFIER_STYLE))
+#define GTK_MODIFIER_STYLE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS  ((o), GTK_TYPE_MODIFIER_STYLE, GtkModifierStyleClass))
+
+typedef struct _GtkModifierStyle GtkModifierStyle;
+typedef struct _GtkModifierStyleClass GtkModifierStyleClass;
+
+struct _GtkModifierStyle
+{
+  GObject parent_object;
+  gpointer priv;
+};
+
+struct _GtkModifierStyleClass
+{
+  GObjectClass parent_class;
+};
+
+GType gtk_modifier_style_get_type (void) G_GNUC_CONST;
+
+GtkModifierStyle * gtk_modifier_style_new (void);
+
+void gtk_modifier_style_set_background_color (GtkModifierStyle *style,
+                                              GtkStateFlags     state,
+                                              const GdkRGBA    *color);
+void gtk_modifier_style_set_color            (GtkModifierStyle *style,
+                                              GtkStateFlags     state,
+                                              const GdkRGBA    *color);
+void gtk_modifier_style_set_font             (GtkModifierStyle           *style,
+                                              const PangoFontDescription *font_desc);
+
+void gtk_modifier_style_map_color            (GtkModifierStyle *style,
+                                              const gchar      *name,
+                                              const GdkRGBA    *color);
+
+void gtk_modifier_style_set_color_property  (GtkModifierStyle *style,
+                                             GType             widget_type,
+                                             const gchar      *prop_name,
+                                             const GdkColor   *color);
+
+G_END_DECLS
+
+#endif /* __GTK_MODIFIER_STYLE_H__ */
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 7c748c4..1139770 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -62,6 +62,7 @@
 #include "gtksymboliccolor.h"
 #include "gtkcssprovider.h"
 #include "gtkanimationdescription.h"
+#include "gtkmodifierstyle.h"
 #include "gtkversion.h"
 #include "gtkdebug.h"
 
@@ -7925,31 +7926,44 @@ gtk_widget_modify_color_component (GtkWidget      *widget,
   gtk_widget_modify_style (widget, rc_style);
 }
 
-static GtkStyleProperties *
+static void
+modifier_style_changed (GtkModifierStyle *style,
+                        GtkWidget        *widget)
+{
+  GtkStyleContext *context;
+
+  context = gtk_widget_get_style_context (widget);
+  gtk_style_context_invalidate (context);
+}
+
+static GtkModifierStyle *
 _gtk_widget_get_modifier_properties (GtkWidget *widget)
 {
-  GtkStyleProperties *properties;
+  GtkModifierStyle *style;
 
-  properties = g_object_get_qdata (G_OBJECT (widget), quark_modifier_style);
+  style = g_object_get_qdata (G_OBJECT (widget), quark_modifier_style);
 
-  if (G_UNLIKELY (!properties))
+  if (G_UNLIKELY (!style))
     {
       GtkStyleContext *context;
 
-      properties = gtk_style_properties_new ();
+      style = gtk_modifier_style_new ();
       g_object_set_qdata_full (G_OBJECT (widget),
                                quark_modifier_style,
-                               properties,
+                               style,
                                (GDestroyNotify) g_object_unref);
 
+      g_signal_connect (style, "changed",
+                        G_CALLBACK (modifier_style_changed), widget);
+
       context = gtk_widget_get_style_context (widget);
 
       gtk_style_context_add_provider (context,
-                                      GTK_STYLE_PROVIDER (properties),
+                                      GTK_STYLE_PROVIDER (style),
                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
     }
 
-  return properties;
+  return style;
 }
 
 /**
@@ -7995,27 +8009,12 @@ gtk_widget_override_color (GtkWidget     *widget,
                            GtkStateFlags  state,
                            const GdkRGBA *color)
 {
-  GtkStyleProperties *properties;
-  GtkStyleContext *context;
+  GtkModifierStyle *style;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  properties = _gtk_widget_get_modifier_properties (widget);
-
-  if (color)
-    gtk_style_properties_set (properties, state,
-                              "color", color,
-                              NULL);
-  else
-    gtk_style_properties_unset_property (properties, "color", state);
-
-  context = gtk_widget_get_style_context (widget);
-  gtk_style_context_invalidate (context);
-
-  g_signal_emit (widget,
-                 widget_signals[STYLE_SET],
-                 0,
-                 widget->priv->style);
+  style = _gtk_widget_get_modifier_properties (widget);
+  gtk_modifier_style_set_color (style, state, color);
 }
 
 /**
@@ -8035,29 +8034,12 @@ gtk_widget_override_background_color (GtkWidget     *widget,
                                       GtkStateFlags  state,
                                       const GdkRGBA *color)
 {
-  GtkStyleProperties *properties;
-  GtkStyleContext *context;
+  GtkModifierStyle *style;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  properties = _gtk_widget_get_modifier_properties (widget);
-
-  if (color)
-    gtk_style_properties_set (properties, state,
-                              "background-color", color,
-                              NULL);
-  else
-    gtk_style_properties_unset_property (properties,
-                                         "background-color",
-                                         state);
-
-  context = gtk_widget_get_style_context (widget);
-  gtk_style_context_invalidate (context);
-
-  g_signal_emit (widget,
-                 widget_signals[STYLE_SET],
-                 0,
-                 widget->priv->style);
+  style = _gtk_widget_get_modifier_properties (widget);
+  gtk_modifier_style_set_background_color (style, state, color);
 }
 
 /**
@@ -8076,27 +8058,12 @@ void
 gtk_widget_override_font (GtkWidget                  *widget,
                           const PangoFontDescription *font_desc)
 {
-  GtkStyleProperties *properties;
-  GtkStyleContext *context;
+  GtkModifierStyle *style;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  properties = _gtk_widget_get_modifier_properties (widget);
-
-  if (font_desc)
-    gtk_style_properties_set (properties, 0,
-                              "font", font_desc,
-                              NULL);
-  else
-    gtk_style_properties_unset_property (properties, "font", 0);
-
-  context = gtk_widget_get_style_context (widget);
-  gtk_style_context_invalidate (context);
-
-  g_signal_emit (widget,
-                 widget_signals[STYLE_SET],
-                 0,
-                 widget->priv->style);
+  style = _gtk_widget_get_modifier_properties (widget);
+  gtk_modifier_style_set_font (style, font_desc);
 }
 
 /**
@@ -8118,27 +8085,12 @@ gtk_widget_override_symbolic_color (GtkWidget     *widget,
                                     const gchar   *name,
                                     const GdkRGBA *color)
 {
-  GtkStyleProperties *properties;
-  GtkStyleContext *context;
-  GtkSymbolicColor *symbolic_color;
+  GtkModifierStyle *style;
 
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
-  properties = _gtk_widget_get_modifier_properties (widget);
-  symbolic_color = gtk_symbolic_color_new_literal (color);
-
-  gtk_style_properties_map_color (properties,
-                                  name, symbolic_color);
-
-  gtk_symbolic_color_unref (symbolic_color);
-
-  context = gtk_widget_get_style_context (widget);
-  gtk_style_context_invalidate (context);
-
-  g_signal_emit (widget,
-                 widget_signals[STYLE_SET],
-                 0,
-                 widget->priv->style);
+  style = _gtk_widget_get_modifier_properties (widget);
+  gtk_modifier_style_map_color (style, name, color);
 }
 
 /**
@@ -8196,6 +8148,11 @@ gtk_widget_modify_fg (GtkWidget      *widget,
     }
   else
     gtk_widget_override_color (widget, state, NULL);
+
+  g_signal_emit (widget,
+                 widget_signals[STYLE_SET],
+                 0,
+                 widget->priv->style);
 }
 
 /**
@@ -8261,6 +8218,11 @@ gtk_widget_modify_bg (GtkWidget      *widget,
     }
   else
     gtk_widget_override_background_color (widget, state, NULL);
+
+  g_signal_emit (widget,
+                 widget_signals[STYLE_SET],
+                 0,
+                 widget->priv->style);
 }
 
 /**
@@ -8407,6 +8369,11 @@ gtk_widget_modify_font (GtkWidget            *widget,
   g_return_if_fail (GTK_IS_WIDGET (widget));
 
   gtk_widget_override_font (widget, font_desc);
+
+  g_signal_emit (widget,
+                 widget_signals[STYLE_SET],
+                 0,
+                 widget->priv->style);
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]