[yelp/yelp-3-0] [libyelp] Get fonts from GtkSettings and apply to WebKitWebSettings



commit 091177fb0da5593e9dc4582d6d98336e44f1cfc2
Author: Shaun McCance <shaunm gnome org>
Date:   Thu Mar 11 21:28:26 2010 -0600

    [libyelp] Get fonts from GtkSettings and apply to WebKitWebSettings

 libyelp/yelp-settings.c |   79 +++++++++++++++++++++++++++++++++++++++--------
 libyelp/yelp-view.c     |   48 ++++++++++++++++++++++++++++
 tests/test-view.c       |    1 -
 3 files changed, 114 insertions(+), 14 deletions(-)
---
diff --git a/libyelp/yelp-settings.c b/libyelp/yelp-settings.c
index ad02b3d..d95d4f1 100644
--- a/libyelp/yelp-settings.c
+++ b/libyelp/yelp-settings.c
@@ -31,6 +31,7 @@ struct _YelpSettingsPriv {
     GMutex       *mutex;
 
     gchar         colors[YELP_SETTINGS_NUM_COLORS][8];
+    gchar        *setfonts[YELP_SETTINGS_NUM_FONTS];
     gchar        *fonts[YELP_SETTINGS_NUM_FONTS];
     gchar        *icons[YELP_SETTINGS_NUM_ICONS];
     gint          icon_size;
@@ -39,6 +40,7 @@ struct _YelpSettingsPriv {
     GtkIconTheme *gtk_icon_theme;
 
     gulong        gtk_theme_changed;
+    gulong        gtk_font_changed;
     gulong        icon_theme_changed;
 };
 
@@ -77,6 +79,9 @@ static void           yelp_settings_set_property (GObject              *object,
 static void           gtk_theme_changed          (GtkSettings          *gtk_settings,
 						  GParamSpec           *pspec,
 					          YelpSettings         *settings);
+static void           gtk_font_changed           (GtkSettings          *gtk_settings,
+						  GParamSpec           *pspec,
+					          YelpSettings         *settings);
 static void           icon_theme_changed         (GtkIconTheme         *theme,
 						  YelpSettings         *settings);
 
@@ -239,6 +244,8 @@ yelp_settings_set_property (GObject      *object,
 	if (settings->priv->gtk_settings) {
 	    g_signal_handler_disconnect (settings->priv->gtk_settings,
 					 settings->priv->gtk_theme_changed);
+	    g_signal_handler_disconnect (settings->priv->gtk_settings,
+					 settings->priv->gtk_font_changed);
 	    g_object_unref (settings->priv->gtk_settings);
 	}
 	settings->priv->gtk_settings = g_value_get_object (value);
@@ -249,10 +256,17 @@ yelp_settings_set_property (GObject      *object,
 				  "notify::gtk-theme-name",
 				  (GCallback) gtk_theme_changed,
 				  settings);
+	    settings->priv->gtk_font_changed =
+		g_signal_connect (settings->priv->gtk_settings,
+				  "notify::gtk-font-name",
+				  (GCallback) gtk_font_changed,
+				  settings);
 	    gtk_theme_changed (settings->priv->gtk_settings, NULL, settings);
+	    gtk_font_changed (settings->priv->gtk_settings, NULL, settings);
 	}
 	else {
 	    settings->priv->gtk_theme_changed = 0;
+	    settings->priv->gtk_font_changed = 0;
 	}
 	break;
     case PROP_GTK_ICON_THEME:
@@ -400,7 +414,10 @@ yelp_settings_get_font (YelpSettings     *settings,
     g_return_val_if_fail (font < YELP_SETTINGS_NUM_FONTS, NULL);
 
     g_mutex_lock (settings->priv->mutex);
-    ret = g_strdup (settings->priv->fonts[font]);
+    if (settings->priv->setfonts[font])
+	ret = g_strdup (settings->priv->setfonts[font]);
+    else
+	ret = g_strdup (settings->priv->fonts[font]);
     g_mutex_unlock (settings->priv->mutex);
 
     return ret;
@@ -411,24 +428,29 @@ yelp_settings_get_font_family (YelpSettings     *settings,
 			       YelpSettingsFont  font)
 {
     const gchar *def = (font == YELP_SETTINGS_FONT_VARIABLE) ? "Sans" : "Monospace";
-    gchar *ret, *c;
+    gchar *desc, *ret, *c; /* do not free */
     g_return_val_if_fail (font < YELP_SETTINGS_NUM_FONTS, NULL);
 
     g_mutex_lock (settings->priv->mutex);
 
-    if (settings->priv->fonts[font] == NULL) {
+    if (settings->priv->setfonts[font])
+	desc = g_strdup (settings->priv->setfonts[font]);
+    else
+	desc = g_strdup (settings->priv->fonts[font]);
+
+    if (desc == NULL) {
 	ret = g_strdup (def);
 	goto done;
     }
 
-    c = strrchr (settings->priv->fonts[font], ' ');
+    c = strrchr (desc, ' ');
     if (c == NULL) {
-	g_warning ("Cannot parse font %s", settings->priv->fonts[font]);
+	g_warning ("Cannot parse font %s", desc);
 	ret = g_strdup (def);
 	goto done;
     }
 
-    ret = g_strndup (settings->priv->fonts[font], c - settings->priv->fonts[font]);
+    ret = g_strndup (desc, c - desc);
 
  done:
     g_mutex_unlock (settings->priv->mutex);
@@ -439,20 +461,25 @@ gint
 yelp_settings_get_font_size (YelpSettings     *settings,
 			     YelpSettingsFont  font)
 {
-    gchar *c;
+    gchar *desc, *c; /* do not free */
     gint ret;
     g_return_val_if_fail (font < YELP_SETTINGS_NUM_FONTS, 0);
 
     g_mutex_lock (settings->priv->mutex);
 
-    if (settings->priv->fonts[font] == NULL) {
+    if (settings->priv->setfonts[font])
+	desc = g_strdup (settings->priv->setfonts[font]);
+    else
+	desc = g_strdup (settings->priv->fonts[font]);
+
+    if (desc == NULL) {
 	ret = 10;
 	goto done;
     }
 
-    c = strrchr (settings->priv->fonts[font], ' ');
+    c = strrchr (desc, ' ');
     if (c == NULL) {
-	g_warning ("Cannot parse font %s", settings->priv->fonts[font]);
+	g_warning ("Cannot parse font %s", desc);
 	ret = 10;
 	goto done;
     }
@@ -478,9 +505,9 @@ yelp_settings_set_fonts (YelpSettings     *settings,
     font = first_font;
     while ((gint) font >= 0) {
 	gchar *fontname = va_arg (args, gchar *);
-	if (settings->priv->fonts[font] != NULL)
-	    g_free (settings->priv->fonts[font]);
-	settings->priv->fonts[font] = g_strdup (fontname);
+	if (settings->priv->setfonts[font] != NULL)
+	    g_free (settings->priv->setfonts[font]);
+	settings->priv->setfonts[font] = g_strdup (fontname);
 	font = va_arg (args, YelpSettingsFont);
     }
 
@@ -732,6 +759,32 @@ gtk_theme_changed (GtkSettings  *gtk_settings,
 }
 
 static void
+gtk_font_changed (GtkSettings  *gtk_settings,
+		  GParamSpec   *pspec,
+		  YelpSettings *settings)
+{
+    gchar *font, *c;
+
+    g_free (settings->priv->fonts[YELP_SETTINGS_FONT_VARIABLE]);
+    g_object_get (gtk_settings, "gtk-font-name", &font, NULL);
+    settings->priv->fonts[YELP_SETTINGS_FONT_VARIABLE] = font;
+
+    c = strrchr (font, ' ');
+    if (c == NULL) {
+	g_warning ("Cannot parse font %s", font);
+	font = g_strdup ("Monospace 10");
+    }
+    else {
+	font = g_strconcat ("Monospace", c, NULL);
+    }
+
+    g_free (settings->priv->fonts[YELP_SETTINGS_FONT_FIXED]);
+    settings->priv->fonts[YELP_SETTINGS_FONT_FIXED] = font;
+
+    g_signal_emit (settings, settings_signals[FONTS_CHANGED], 0);
+}
+
+static void
 icon_theme_changed (GtkIconTheme *theme,
 		    YelpSettings *settings)
 {
diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c
index 98ccba9..e3c8501 100644
--- a/libyelp/yelp-view.c
+++ b/libyelp/yelp-view.c
@@ -25,6 +25,7 @@
 #endif
 
 #include <glib/gi18n.h>
+#include <glib-object.h>
 #include <gio/gio.h>
 #include <gtk/gtk.h>
 #include <webkit/webkit.h>
@@ -69,6 +70,8 @@ static void        view_load_page                 (YelpView           *view);
 static void        view_show_error_page           (YelpView           *view,
                                                    GError             *error);
 
+static void        view_set_fonts                 (YelpView           *view);
+
 static void        uri_resolved                   (YelpUri            *uri,
                                                    YelpView           *view);
 static void        document_callback              (YelpDocument       *document,
@@ -122,6 +125,12 @@ yelp_view_init (YelpView *view)
                           G_CALLBACK (view_navigation_requested), NULL);
     g_signal_connect (view, "resource-request-starting",
                       G_CALLBACK (view_resource_request), NULL);
+
+    g_signal_connect_swapped (yelp_settings_get_default (),
+                              "fonts-changed",
+                              view_set_fonts,
+                              view);
+    view_set_fonts (view);
 }
 
 static void
@@ -458,6 +467,45 @@ view_show_error_page (YelpView *view,
     g_free (page);
 }
 
+
+static void
+view_set_fonts (YelpView *view)
+{
+    YelpSettings *settings;
+    WebKitWebSettings *websettings;
+    gchar *family;
+    gint size;
+
+    settings = yelp_settings_get_default ();
+    websettings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view));
+
+    g_object_set (websettings,
+                  "default-encoding", "utf-8",
+                  "enable-private-browsing", TRUE,
+                  NULL);
+
+    family = yelp_settings_get_font_family (settings,
+                                            YELP_SETTINGS_FONT_VARIABLE);
+    size = yelp_settings_get_font_size (settings,
+                                        YELP_SETTINGS_FONT_VARIABLE);
+    g_object_set (websettings,
+                  "default-font-family", family,
+                  "sans-serif-font-family", family,
+                  "default-font-size", size,
+                  NULL);
+    g_free (family);
+
+    family = yelp_settings_get_font_family (settings,
+                                            YELP_SETTINGS_FONT_FIXED);
+    size = yelp_settings_get_font_size (settings,
+                                        YELP_SETTINGS_FONT_FIXED);
+    g_object_set (websettings,
+                  "monospace-font-family", family,
+                  "default-monospace-font-size", size,
+                  NULL);
+    g_free (family);
+}
+
 /******************************************************************************/
 
 static void
diff --git a/tests/test-view.c b/tests/test-view.c
index 3858e37..984546c 100644
--- a/tests/test-view.c
+++ b/tests/test-view.c
@@ -63,7 +63,6 @@ main (int argc, char **argv)
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     gtk_window_set_type_hint ((GtkWindow *) window,
 			      GDK_WINDOW_TYPE_HINT_UTILITY);
-    gtk_window_set_keep_above ((GtkWindow *) window, TRUE);
     gtk_window_set_default_size (GTK_WINDOW (window), 520, 580);
     g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
 



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