From 621a95f515a97ff19f62db9d0aa561ee73276bd9 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Mon, 30 Aug 2010 22:41:12 +0200 Subject: [PATCH] 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 9b622254c2..ae9cf81983 100644 --- a/gtk/gtksettings.c +++ b/gtk/gtksettings.c @@ -179,6 +179,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, @@ -221,7 +223,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 @@ -1246,6 +1250,76 @@ gtk_settings_class_init (GtkSettingsClass *class) g_assert (result == PROP_IM_STATUS_STYLE); } +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 6c3a57561e..556b257405 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 cb053fad6b..8922eb9f7a 100644 --- a/gtk/gtkwidget.c +++ b/gtk/gtkwidget.c @@ -13308,6 +13308,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), @@ -13347,6 +13348,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);