97c61e7
From 0b452d4735c6ef35299e24ffb3faa0cabc5559c3 Mon Sep 17 00:00:00 2001
97c61e7
From: Vincent Untz <vuntz@gnome.org>
97c61e7
Date: Mon, 4 Apr 2011 19:32:56 +0200
97c61e7
Subject: [PATCH] Use the right color for text in GkbdStatus
97c61e7
97c61e7
This is really a big hack: instead of getting the information about how
97c61e7
to draw the font from a GTK+ style, we get it from the GtkStyleContext
97c61e7
from the tray icon widget embedded in the GtkStatusIcon.
97c61e7
97c61e7
To make this happen, we have to fake the GtkStatusIconPrivate structure,
97c61e7
and we rely on the fact that the tray icon widget is the first element
97c61e7
in this structure.
97c61e7
97c61e7
https://bugzilla.gnome.org/show_bug.cgi?id=642703
97c61e7
---
97c61e7
 libgnomekbd/gkbd-indicator-config.c |   98 ++++++++++++++++++-----------------
97c61e7
 libgnomekbd/gkbd-indicator-config.h |   10 ++++
97c61e7
 libgnomekbd/gkbd-status.c           |   65 ++++++++++++++---------
97c61e7
 3 files changed, 100 insertions(+), 73 deletions(-)
97c61e7
97c61e7
diff --git a/libgnomekbd/gkbd-indicator-config.c b/libgnomekbd/gkbd-indicator-config.c
97c61e7
index fdf008b..c678643 100644
97c61e7
--- a/libgnomekbd/gkbd-indicator-config.c
97c61e7
+++ b/libgnomekbd/gkbd-indicator-config.c
97c61e7
@@ -37,7 +37,6 @@
97c61e7
 /**
97c61e7
  * GkbdIndicatorConfig
97c61e7
  */
97c61e7
-#define GTK_STYLE_PATH "*PanelWidget*"
97c61e7
 
97c61e7
 const gchar GKBD_INDICATOR_CONFIG_KEY_SHOW_FLAGS[] = "show-flags";
97c61e7
 const gchar GKBD_INDICATOR_CONFIG_KEY_ENABLED_PLUGINS[] =
97c61e7
@@ -65,26 +64,8 @@ gkbd_indicator_config_load_font (GkbdIndicatorConfig * ind_config)
97c61e7
 	    g_settings_get_int (ind_config->settings,
97c61e7
 				GKBD_INDICATOR_CONFIG_KEY_FONT_SIZE);
97c61e7
 
97c61e7
-	if (ind_config->font_family == NULL ||
97c61e7
-	    ind_config->font_family[0] == '\0') {
97c61e7
-		PangoFontDescription *fd = NULL;
97c61e7
-		GtkStyle *style =
97c61e7
-		    gtk_rc_get_style_by_paths (gtk_settings_get_default (),
97c61e7
-					       GTK_STYLE_PATH,
97c61e7
-					       GTK_STYLE_PATH,
97c61e7
-					       GTK_TYPE_LABEL);
97c61e7
-		if (style != NULL)
97c61e7
-			fd = style->font_desc;
97c61e7
-		if (fd != NULL) {
97c61e7
-			ind_config->font_family =
97c61e7
-			    g_strdup (pango_font_description_get_family
97c61e7
-				      (fd));
97c61e7
-			ind_config->font_size =
97c61e7
-			    pango_font_description_get_size (fd) /
97c61e7
-			    PANGO_SCALE;
97c61e7
-		}
97c61e7
-	}
97c61e7
-	xkl_debug (150, "font: [%s], size %d\n", ind_config->font_family,
97c61e7
+	xkl_debug (150, "font: [%s], size %d\n",
97c61e7
+		   ind_config->font_family ? ind_config->font_family : "(null)",
97c61e7
 		   ind_config->font_size);
97c61e7
 
97c61e7
 }
97c61e7
@@ -96,38 +77,61 @@ gkbd_indicator_config_load_colors (GkbdIndicatorConfig * ind_config)
97c61e7
 	    g_settings_get_string (ind_config->settings,
97c61e7
 				   GKBD_INDICATOR_CONFIG_KEY_FOREGROUND_COLOR);
97c61e7
 
97c61e7
-	if (ind_config->foreground_color == NULL ||
97c61e7
-	    ind_config->foreground_color[0] == '\0') {
97c61e7
-		GtkStyle *style =
97c61e7
-		    gtk_rc_get_style_by_paths (gtk_settings_get_default (),
97c61e7
-					       GTK_STYLE_PATH,
97c61e7
-					       GTK_STYLE_PATH,
97c61e7
-					       GTK_TYPE_LABEL);
97c61e7
-		if (style != NULL) {
97c61e7
-			ind_config->foreground_color =
97c61e7
-			    g_strdup_printf ("%g %g %g", ((double)
97c61e7
-							  style->fg
97c61e7
-							  [GTK_STATE_NORMAL].
97c61e7
-							  red)
97c61e7
-					     / 0x10000, ((double)
97c61e7
-							 style->fg
97c61e7
-							 [GTK_STATE_NORMAL].
97c61e7
-							 green)
97c61e7
-					     / 0x10000, ((double)
97c61e7
-							 style->fg
97c61e7
-							 [GTK_STATE_NORMAL].
97c61e7
-							 blue)
97c61e7
-					     / 0x10000);
97c61e7
-		}
97c61e7
-
97c61e7
-	}
97c61e7
-
97c61e7
 	ind_config->background_color =
97c61e7
 	    g_settings_get_string (ind_config->settings,
97c61e7
 				   GKBD_INDICATOR_CONFIG_KEY_BACKGROUND_COLOR);
97c61e7
 }
97c61e7
 
97c61e7
 void
97c61e7
+gkbd_indicator_config_get_font_for_widget (GkbdIndicatorConfig * ind_config,
97c61e7
+					   GtkWidget           * widget,
97c61e7
+					   gchar               ** font_family,
97c61e7
+					   int                  * font_size)
97c61e7
+{
97c61e7
+	GtkStyleContext *context;
97c61e7
+	const PangoFontDescription *fd = NULL;
97c61e7
+
97c61e7
+	g_return_if_fail (GTK_IS_WIDGET (widget));
97c61e7
+
97c61e7
+	if (ind_config->font_family != NULL &&
97c61e7
+	    ind_config->font_family[0] != '\0') {
97c61e7
+		if (font_family)
97c61e7
+			*font_family = g_strdup (ind_config->font_family);
97c61e7
+		if (font_size)
97c61e7
+			*font_size = ind_config->font_size;
97c61e7
+
97c61e7
+		return;
97c61e7
+	}
97c61e7
+
97c61e7
+	context = gtk_widget_get_style_context (widget);
97c61e7
+	fd = gtk_style_context_get_font (context, GTK_STATE_FLAG_NORMAL);
97c61e7
+
97c61e7
+	if (font_family)
97c61e7
+		*font_family = g_strdup (pango_font_description_get_family (fd));
97c61e7
+	if (font_size)
97c61e7
+		*font_size = pango_font_description_get_size (fd) / PANGO_SCALE;
97c61e7
+}
97c61e7
+
97c61e7
+gchar *
97c61e7
+gkbd_indicator_config_get_fg_color_for_widget (GkbdIndicatorConfig * ind_config,
97c61e7
+					       GtkWidget           * widget)
97c61e7
+{
97c61e7
+	GtkStyleContext *context;
97c61e7
+	GdkRGBA          rgba;
97c61e7
+
97c61e7
+	g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
97c61e7
+
97c61e7
+	if (ind_config->foreground_color != NULL &&
97c61e7
+	    ind_config->foreground_color[0] != '\0')
97c61e7
+		return g_strdup (ind_config->foreground_color);
97c61e7
+
97c61e7
+	context = gtk_widget_get_style_context (widget);
97c61e7
+	gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, &rgba);
97c61e7
+
97c61e7
+	return g_strdup_printf ("%g %g %g", rgba.red, rgba.green, rgba.blue);
97c61e7
+}
97c61e7
+
97c61e7
+void
97c61e7
 gkbd_indicator_config_refresh_style (GkbdIndicatorConfig * ind_config)
97c61e7
 {
97c61e7
 	g_free (ind_config->font_family);
97c61e7
diff --git a/libgnomekbd/gkbd-indicator-config.h b/libgnomekbd/gkbd-indicator-config.h
97c61e7
index 2638895..9b9d935 100644
97c61e7
--- a/libgnomekbd/gkbd-indicator-config.h
97c61e7
+++ b/libgnomekbd/gkbd-indicator-config.h
97c61e7
@@ -62,6 +62,16 @@ extern void gkbd_indicator_config_load (GkbdIndicatorConfig
97c61e7
 extern void gkbd_indicator_config_save (GkbdIndicatorConfig *
97c61e7
 					applet_config);
97c61e7
 
97c61e7
+extern void
97c61e7
+gkbd_indicator_config_get_font_for_widget (GkbdIndicatorConfig * ind_config,
97c61e7
+					   GtkWidget           * widget,
97c61e7
+					   gchar               ** font_family,
97c61e7
+					   int                  * font_size);
97c61e7
+
97c61e7
+extern gchar *
97c61e7
+gkbd_indicator_config_get_fg_color_for_widget (GkbdIndicatorConfig * ind_config,
97c61e7
+					       GtkWidget           * widget);
97c61e7
+
97c61e7
 extern void gkbd_indicator_config_refresh_style (GkbdIndicatorConfig *
97c61e7
 						 applet_config);
97c61e7
 
97c61e7
diff --git a/libgnomekbd/gkbd-status.c b/libgnomekbd/gkbd-status.c
97c61e7
index 5785e4c..276fe54 100644
97c61e7
--- a/libgnomekbd/gkbd-status.c
97c61e7
+++ b/libgnomekbd/gkbd-status.c
97c61e7
@@ -59,22 +59,26 @@ static gki_globals globals;
97c61e7
 
97c61e7
 G_DEFINE_TYPE (GkbdStatus, gkbd_status, GTK_TYPE_STATUS_ICON)
97c61e7
 
97c61e7
+typedef struct {
97c61e7
+	GtkWidget *tray_icon;
97c61e7
+} GkbdStatusPrivHack;
97c61e7
+
97c61e7
 static void
97c61e7
 gkbd_status_global_init (void);
97c61e7
 static void
97c61e7
 gkbd_status_global_term (void);
97c61e7
 static GdkPixbuf *
97c61e7
-gkbd_status_prepare_drawing (int group);
97c61e7
+gkbd_status_prepare_drawing (GkbdStatus * gki, int group);
97c61e7
 static void
97c61e7
 gkbd_status_set_current_page_for_group (GkbdStatus * gki, int group);
97c61e7
 static void
97c61e7
 gkbd_status_set_current_page (GkbdStatus * gki);
97c61e7
 static void
97c61e7
-gkbd_status_reinit_globals (void);
97c61e7
+gkbd_status_reinit_globals (GkbdStatus * gki);
97c61e7
 static void
97c61e7
 gkbd_status_cleanup_icons (void);
97c61e7
 static void
97c61e7
-gkbd_status_fill_icons (void);
97c61e7
+gkbd_status_fill_icons (GkbdStatus * gki);
97c61e7
 static void
97c61e7
 gkbd_status_set_tooltips (GkbdStatus * gki, const char *str);
97c61e7
 
97c61e7
@@ -97,8 +101,8 @@ gkbd_status_cleanup_icons ()
97c61e7
 	}
97c61e7
 }
97c61e7
 
97c61e7
-void
97c61e7
-gkbd_status_fill_icons ()
97c61e7
+static void
97c61e7
+gkbd_status_fill_icons (GkbdStatus * gki)
97c61e7
 {
97c61e7
 	int grp;
97c61e7
 	int total_groups =
97c61e7
@@ -106,7 +110,7 @@ gkbd_status_fill_icons ()
97c61e7
 				       (globals.config));
97c61e7
 
97c61e7
 	for (grp = 0; grp < total_groups; grp++) {
97c61e7
-		GdkPixbuf *page = gkbd_status_prepare_drawing (grp);
97c61e7
+		GdkPixbuf *page = gkbd_status_prepare_drawing (gki, grp);
97c61e7
 		globals.icons = g_slist_append (globals.icons, page);
97c61e7
 	}
97c61e7
 }
97c61e7
@@ -119,9 +123,12 @@ gkbd_status_activate (GkbdStatus * gki)
97c61e7
 }
97c61e7
 
97c61e7
 static void
97c61e7
-gkbd_status_render_cairo (cairo_t * cr, int group)
97c61e7
+gkbd_status_render_cairo (GkbdStatusPrivHack * gkh, cairo_t * cr, int group)
97c61e7
 {
97c61e7
 	double r, g, b;
97c61e7
+	GdkColor *fg_color;
97c61e7
+	gchar *font_family;
97c61e7
+	int font_size;
97c61e7
 	PangoFontDescription *pfd;
97c61e7
 	PangoContext *pcc;
97c61e7
 	PangoLayout *pl;
97c61e7
@@ -146,28 +153,30 @@ gkbd_status_render_cairo (cairo_t * cr, int group)
97c61e7
 		}
97c61e7
 	}
97c61e7
 
97c61e7
-	if (ind_cfg->foreground_color != NULL &&
97c61e7
-	    ind_cfg->foreground_color[0] != 0) {
97c61e7
-		if (sscanf
97c61e7
-		    (ind_cfg->foreground_color, "%lg %lg %lg", &r,
97c61e7
-		     &g, &b) == 3) {
97c61e7
-			cairo_set_source_rgb (cr, r, g, b);
97c61e7
-		}
97c61e7
-	}
97c61e7
+	g_object_get (gkh->tray_icon, "fg-color", &fg_color, NULL);
97c61e7
+	cairo_set_source_rgb (cr, fg_color->red, fg_color->green, fg_color->blue);
97c61e7
+	gdk_color_free (fg_color);
97c61e7
+
97c61e7
+	gkbd_indicator_config_get_font_for_widget (ind_cfg,
97c61e7
+						   gkh->tray_icon,
97c61e7
+						   &font_family,
97c61e7
+						   &font_size);
97c61e7
 
97c61e7
-	if (ind_cfg->font_family != NULL && ind_cfg->font_family[0] != 0) {
97c61e7
-		cairo_select_font_face (cr, ind_cfg->font_family,
97c61e7
+	if (font_family != NULL && font_family[0] != 0) {
97c61e7
+		cairo_select_font_face (cr, font_family,
97c61e7
 					CAIRO_FONT_SLANT_NORMAL,
97c61e7
 					CAIRO_FONT_WEIGHT_NORMAL);
97c61e7
 	}
97c61e7
 
97c61e7
 	pfd = pango_font_description_new ();
97c61e7
-	pango_font_description_set_family (pfd, ind_cfg->font_family);
97c61e7
+	pango_font_description_set_family (pfd, font_family);
97c61e7
 	pango_font_description_set_style (pfd, PANGO_STYLE_NORMAL);
97c61e7
 	pango_font_description_set_weight (pfd, PANGO_WEIGHT_NORMAL);
97c61e7
 	pango_font_description_set_size (pfd,
97c61e7
 					 ind_cfg->font_size * PANGO_SCALE);
97c61e7
 
97c61e7
+	g_free (font_family);
97c61e7
+
97c61e7
 	pcc = pango_cairo_create_context (cr);
97c61e7
 
97c61e7
 	fo = cairo_font_options_copy (gdk_screen_get_font_options
97c61e7
@@ -261,7 +270,7 @@ convert_bgra_to_rgba (guint8 const *src, guint8 * dst, int width,
97c61e7
 }
97c61e7
 
97c61e7
 static GdkPixbuf *
97c61e7
-gkbd_status_prepare_drawing (int group)
97c61e7
+gkbd_status_prepare_drawing (GkbdStatus * gki, int group)
97c61e7
 {
97c61e7
 	GError *gerror = NULL;
97c61e7
 	char *image_filename;
97c61e7
@@ -321,7 +330,8 @@ gkbd_status_prepare_drawing (int group)
97c61e7
 						globals.current_height);
97c61e7
 		unsigned char *cairo_data;
97c61e7
 		guchar *pixbuf_data;
97c61e7
-		gkbd_status_render_cairo (cairo_create (cs), group);
97c61e7
+		gkbd_status_render_cairo ((GkbdStatusPrivHack *) GTK_STATUS_ICON (gki)->priv,
97c61e7
+					  cairo_create (cs), group);
97c61e7
 		cairo_data = cairo_image_surface_get_data (cs);
97c61e7
 #if 0
97c61e7
 		char pngfilename[20];
97c61e7
@@ -372,11 +382,11 @@ gkbd_status_update_tooltips (GkbdStatus * gki)
97c61e7
 	}
97c61e7
 }
97c61e7
 
97c61e7
-void
97c61e7
-gkbd_status_reinit_globals ()
97c61e7
+static void
97c61e7
+gkbd_status_reinit_globals (GkbdStatus * gki)
97c61e7
 {
97c61e7
 	gkbd_status_cleanup_icons ();
97c61e7
-	gkbd_status_fill_icons ();
97c61e7
+	gkbd_status_fill_icons (gki);
97c61e7
 }
97c61e7
 
97c61e7
 void
97c61e7
@@ -389,8 +399,11 @@ gkbd_status_reinit_ui (GkbdStatus * gki)
97c61e7
 static void
97c61e7
 gkbd_status_cfg_callback (GkbdConfiguration * configuration)
97c61e7
 {
97c61e7
+	GSList *objects;
97c61e7
 	xkl_debug (150, "Config changed: reinit ui\n");
97c61e7
-	gkbd_status_reinit_globals ();
97c61e7
+	objects = gkbd_configuration_get_all_objects (configuration);
97c61e7
+	if (objects)
97c61e7
+		gkbd_status_reinit_globals (objects->data);
97c61e7
 	ForAllObjects (configuration) {
97c61e7
 		gkbd_status_reinit_ui (GKBD_STATUS (gki));
97c61e7
 	} NextObject ()
97c61e7
@@ -499,7 +512,7 @@ gkbd_status_size_changed (GkbdStatus * gki, gint size)
97c61e7
 	if (globals.current_height != size) {
97c61e7
 		globals.current_height = size;
97c61e7
 		globals.current_width = size * 3 / 2;
97c61e7
-		gkbd_status_reinit_globals ();
97c61e7
+		gkbd_status_reinit_globals (gki);
97c61e7
 		gkbd_status_reinit_ui (gki);
97c61e7
 	}
97c61e7
 }
97c61e7
@@ -511,7 +524,7 @@ gkbd_status_theme_changed (GtkSettings * settings, GParamSpec * pspec,
97c61e7
 	xkl_debug (150, "Theme changed\n");
97c61e7
 	gkbd_indicator_config_refresh_style
97c61e7
 	    (gkbd_configuration_get_indicator_config (globals.config));
97c61e7
-	gkbd_status_reinit_globals ();
97c61e7
+	gkbd_status_reinit_globals (gki);
97c61e7
 	gkbd_status_reinit_ui (gki);
97c61e7
 }
97c61e7
 
97c61e7
-- 
97c61e7
1.7.4.1