[gtk+/gtk-style-context] GtkSettings: implement GtkStyleProvider



commit 688d1a7d7017ccd88a446b10ea519d78243d1574
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Aug 30 22:41:12 2010 +0200

    GtkSettings: implement GtkStyleProvider

 gtk/gtksettings.c      |   76 +++++++++++++++++++++++++++++++++++++++++++++++-
 gtk/gtkstyleprovider.h |    5 ++-
 gtk/gtkwidget.c        |    6 ++++
 3 files changed, 84 insertions(+), 3 deletions(-)
---
diff --git a/gtk/gtksettings.c b/gtk/gtksettings.c
index 5818069..b77a2ab 100644
--- a/gtk/gtksettings.c
+++ b/gtk/gtksettings.c
@@ -130,6 +130,8 @@ enum {
 
 
 /* --- prototypes --- */
+static void     gtk_settings_provider_iface_init (GtkStyleProviderIface *iface);
+
 static void	gtk_settings_finalize		 (GObject		*object);
 static void	gtk_settings_get_property	 (GObject		*object,
 						  guint			 property_id,
@@ -168,7 +170,9 @@ static GSList           *object_list = NULL;
 static guint		 class_n_properties = 0;
 
 
-G_DEFINE_TYPE (GtkSettings, gtk_settings, G_TYPE_OBJECT)
+G_DEFINE_TYPE_EXTENDED (GtkSettings, gtk_settings, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
+                                               gtk_settings_provider_iface_init));
 
 /* --- functions --- */
 static void
@@ -1045,6 +1049,76 @@ gtk_settings_class_init (GtkSettingsClass *class)
   g_assert (result == PROP_APPLICATION_PREFER_DARK_THEME);
 }
 
+static GtkStyleSet *
+gtk_settings_get_style (GtkStyleProvider *provider,
+                        GtkWidgetPath    *path)
+{
+  PangoFontDescription *font_desc;
+  gchar *font_name, *color_scheme;
+  GtkSettings *settings;
+  GtkStyleSet *set;
+  gchar **colors;
+  guint i;
+
+  settings = GTK_SETTINGS (provider);
+  set = gtk_style_set_new ();
+
+  g_object_get (settings,
+                "gtk-font-name", &font_name,
+                "gtk-color-scheme", &color_scheme,
+                NULL);
+
+  colors = g_strsplit_set (color_scheme, "\n;", -1);
+
+  for (i = 0; colors[i]; i++)
+    {
+      gchar *name, *pos;
+      GdkColor col;
+
+      if (!*colors[i])
+        continue;
+
+      name = colors[i];
+      pos = strchr (colors[i], ':');
+
+      if (!pos)
+        continue;
+
+      /* Set NUL after color name */
+      *pos = '\0';
+      pos++;
+
+      /* Find start of color string */
+      while (*pos == ' ')
+        pos++;
+
+      if (!*pos || !gdk_color_parse (pos, &col))
+        continue;
+
+      gtk_style_set_map_color (set, name,
+                               gtk_symbolic_color_new_literal (&col));
+    }
+
+  font_desc = pango_font_description_from_string (font_name);
+
+  gtk_style_set_set (set, 0,
+                     "font", font_desc,
+                     NULL);
+
+  pango_font_description_free (font_desc);
+  g_strfreev (colors);
+  g_free (color_scheme);
+  g_free (font_name);
+
+  return set;
+}
+
+static void
+gtk_settings_provider_iface_init (GtkStyleProviderIface *iface)
+{
+  iface->get_style = gtk_settings_get_style;
+}
+
 static void
 gtk_settings_finalize (GObject *object)
 {
diff --git a/gtk/gtkstyleprovider.h b/gtk/gtkstyleprovider.h
index 6c3a575..556b257 100644
--- a/gtk/gtkstyleprovider.h
+++ b/gtk/gtkstyleprovider.h
@@ -35,8 +35,9 @@ G_BEGIN_DECLS
 
 #define GTK_STYLE_PROVIDER_PRIORITY_FALLBACK      1
 #define GTK_STYLE_PROVIDER_PRIORITY_DEFAULT     200
-#define GTK_STYLE_PROVIDER_PRIORITY_APPLICATION 400
-#define GTK_STYLE_PROVIDER_PRIORITY_USER        600
+#define GTK_STYLE_PROVIDER_PRIORITY_SETTINGS    400
+#define GTK_STYLE_PROVIDER_PRIORITY_APPLICATION 600
+#define GTK_STYLE_PROVIDER_PRIORITY_USER        800
 
 typedef struct GtkStyleProviderIface GtkStyleProviderIface;
 typedef struct GtkStyleProvider GtkStyleProvider; /* dummy typedef */
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 85dc299..926c894 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -11724,6 +11724,7 @@ gtk_widget_get_style_context (GtkWidget *widget)
       static GtkCssProvider *css_provider = NULL;
       GtkCssProvider *default_provider;
       GtkWidgetPath *path;
+      GtkSettings *settings;
 
       context = g_object_new (GTK_TYPE_STYLE_CONTEXT,
                               "direction", gtk_widget_get_direction (widget),
@@ -11763,6 +11764,11 @@ gtk_widget_get_style_context (GtkWidget *widget)
                                       GTK_STYLE_PROVIDER (default_provider),
                                       GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
 
+      settings = gtk_widget_get_settings (widget);
+      gtk_style_context_add_provider (context,
+                                      GTK_STYLE_PROVIDER (settings),
+                                      GTK_STYLE_PROVIDER_PRIORITY_SETTINGS);
+
       path = gtk_widget_get_path (widget);
       gtk_style_context_set_path (context, path);
       gtk_widget_path_free (path);



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