[gnome-logs/wip/monospace] Apply the system monospace font to log messages



commit e04bb9e4a85ab300435998972a6826ffddc0fbf1
Author: David King <davidk gnome org>
Date:   Wed Oct 9 16:45:22 2013 +0100

    Apply the system monospace font to log messages
    
    Monitor the monospace-font-name key from gsettings-desktop-schemas and
    update the font if it changes.

 README               |    3 ++
 configure.ac         |    5 +++
 src/gl-application.c |   97 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/gl-application.h |    2 +-
 src/gl-eventview.c   |   14 ++++++--
 5 files changed, 116 insertions(+), 5 deletions(-)
---
diff --git a/README b/README
index 650289e..05f8134 100644
--- a/README
+++ b/README
@@ -1,3 +1,6 @@
 GNOME Logs is a log viewer for the systemd journal
 
 https://wiki.gnome.org/Apps/Logs
+
+It depends of GTK+ 3.10 or above and libsystemd-journal at build time, and
+gsettings-desktop-schemas at runtime.
diff --git a/configure.ac b/configure.ac
index 8fbfdbf..4e2d29c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -34,6 +34,11 @@ IT_PROG_INTLTOOL([0.50.0])
 AC_SUBST([GETTEXT_PACKAGE], [AC_PACKAGE_TARNAME])
 AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"], [gettext package name])
 
+dnl GSettings schemas
+PKG_CHECK_EXISTS([gsettings-desktop-schemas],
+                 [],
+                 [AC_MSG_WARN([gsettings-desktop-schemas is required at runtime])])
+
 dnl Libraries
 GTK_REQUIRED="gtk+-3.0 >= 3.9.6"
 SYSTEMD_JOURNAL_REQUIRED="libsystemd-journal"
diff --git a/src/gl-application.c b/src/gl-application.c
index 99544cf..0bff914 100644
--- a/src/gl-application.c
+++ b/src/gl-application.c
@@ -26,7 +26,16 @@
 #include "gl-eventview.h"
 #include "gl-window.h"
 
-G_DEFINE_TYPE (GlApplication, gl_application, GTK_TYPE_APPLICATION)
+typedef struct
+{
+    GSettings *desktop;
+    gchar *monospace_font;
+} GlApplicationPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GlApplication, gl_application, GTK_TYPE_APPLICATION)
+
+static const gchar DESKTOP_SCHEMA[] = "org.gnome.desktop.interface";
+static const gchar DESKTOP_MONOSPACE_FONT_NAME[] = "monospace-font-name";
 
 static void
 on_new_window (GSimpleAction *action,
@@ -78,6 +87,55 @@ on_quit (GSimpleAction *action,
     g_application_quit (application);
 }
 
+static void
+on_provider_parsing_error (GtkCssProvider *provider,
+                           GtkCssSection *section,
+                           GError *error,
+                           gpointer user_data)
+{
+    g_critical ("Error while parsing CSS style (line: %u, character: %u): %s",
+                gtk_css_section_get_end_line (section) + 1,
+                gtk_css_section_get_end_position (section) + 1,
+                error->message);
+}
+
+static void
+on_monospace_font_name_changed (GSettings *settings,
+                                const gchar *key,
+                                GlApplicationPrivate *priv)
+{
+    gchar *font_name;
+
+    font_name = g_settings_get_string (settings, key);
+
+    if (g_strcmp0 (font_name, priv->monospace_font) != 0)
+    {
+        GtkCssProvider *provider;
+        gchar *css_fragment;
+
+        g_free (priv->monospace_font);
+        priv->monospace_font = font_name;
+
+        css_fragment = g_strconcat (".event-monospace { font: ", font_name,
+                                    "; }", NULL);
+        provider = gtk_css_provider_new ();
+        g_signal_connect (provider, "parsing-error",
+                          G_CALLBACK (on_provider_parsing_error), NULL);
+        gtk_css_provider_load_from_data (provider, css_fragment, -1, NULL);
+
+        gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
+                                                   GTK_STYLE_PROVIDER (provider),
+                                                   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+
+        g_free (css_fragment);
+        g_object_unref (provider);
+    }
+    else
+    {
+        g_free (font_name);
+    }
+}
+
 static GActionEntry actions[] = {
     { "new-window", on_new_window },
     { "about", on_about },
@@ -123,6 +181,7 @@ static void
 gl_application_activate (GApplication *application)
 {
     GtkWidget *window;
+    GlApplicationPrivate *priv;
 
     window = gl_window_new (GTK_APPLICATION (application));
     gtk_widget_show (window);
@@ -130,18 +189,54 @@ gl_application_activate (GApplication *application)
                                      "<Primary>w", "win.close", NULL);
     gtk_application_add_accelerator (GTK_APPLICATION (application),
                                      "<Primary>f", "win.search", NULL);
+
+    priv = gl_application_get_instance_private (GL_APPLICATION (application));
+
+    on_monospace_font_name_changed (priv->desktop, DESKTOP_MONOSPACE_FONT_NAME,
+                                    priv);
+}
+
+static void
+gl_application_finalize (GObject *object)
+{
+    GlApplication *application;
+    GlApplicationPrivate *priv;
+
+    application = GL_APPLICATION (object);
+    priv = gl_application_get_instance_private (application);
+
+    g_clear_object (&priv->desktop);
+    g_clear_pointer (&priv->monospace_font, g_free);
 }
 
 static void
 gl_application_init (GlApplication *application)
 {
+    GlApplicationPrivate *priv;
+    gchar *changed_font;
+
+    priv = gl_application_get_instance_private (application);
+
+    priv->monospace_font = NULL;
+    priv->desktop = g_settings_new (DESKTOP_SCHEMA);
+
+    changed_font = g_strconcat ("changed::", DESKTOP_MONOSPACE_FONT_NAME, NULL);
+    g_signal_connect (priv->desktop, changed_font,
+                      G_CALLBACK (on_monospace_font_name_changed),
+                      priv);
+
+    g_free (changed_font);
 }
 
 static void
 gl_application_class_init (GlApplicationClass *klass)
 {
+    GObjectClass *gobject_class;
     GApplicationClass *app_class;
 
+    gobject_class = G_OBJECT_CLASS (klass);
+    gobject_class->finalize = gl_application_finalize;
+
     app_class = G_APPLICATION_CLASS (klass);
     app_class->activate = gl_application_activate;
     app_class->startup = gl_application_startup;
diff --git a/src/gl-application.h b/src/gl-application.h
index 3ba5004..d853812 100644
--- a/src/gl-application.h
+++ b/src/gl-application.h
@@ -36,7 +36,7 @@ typedef struct
 } GlApplicationClass;
 
 #define GL_TYPE_APPLICATION (gl_application_get_type ())
-#define GL_APPLICATION(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GL_TYPE_APPLICATION, GlApplication)
+#define GL_APPLICATION(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GL_TYPE_APPLICATION, GlApplication))
 
 GType gl_application_get_type (void);
 GtkApplication * gl_application_new (void);
diff --git a/src/gl-eventview.c b/src/gl-eventview.c
index 13349f1..52e5988 100644
--- a/src/gl-eventview.c
+++ b/src/gl-eventview.c
@@ -414,6 +414,8 @@ insert_journal_query_devices (GlJournal *journal,
         gtk_container_add (GTK_CONTAINER (row), grid);
 
         label = gtk_label_new (result.message);
+        context = gtk_widget_get_style_context (GTK_WIDGET (row));
+        gtk_style_context_add_class (context, "event-monospace");
         gtk_widget_set_hexpand (label, TRUE);
         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
@@ -486,6 +488,8 @@ insert_journal_query_security (GlJournal *journal,
         gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
 
         label = gtk_label_new (result.message);
+        context = gtk_widget_get_style_context (GTK_WIDGET (row));
+        gtk_style_context_add_class (context, "event-monospace");
         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
         gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
@@ -540,6 +544,8 @@ insert_journal_query_simple (GlJournal *journal,
         gtk_container_add (GTK_CONTAINER (row), grid);
 
         label = gtk_label_new (result.message);
+        context = gtk_widget_get_style_context (GTK_WIDGET (row));
+        gtk_style_context_add_class (context, "event-monospace");
         gtk_widget_set_hexpand (label, TRUE);
         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
@@ -578,7 +584,7 @@ insert_journal_query_cmdline (GlJournal *journal,
     for (l = results; l != NULL; l = g_list_next (l))
     {
         GtkWidget *row;
-        GtkStyleContext *style;
+        GtkStyleContext *context;
         GtkWidget *grid;
         gchar *markup;
         GtkWidget *label;
@@ -587,8 +593,8 @@ insert_journal_query_cmdline (GlJournal *journal,
         GlJournalResult result = *(GlJournalResult *)(l->data);
 
         row = gtk_list_box_row_new ();
-        style = gtk_widget_get_style_context (GTK_WIDGET (row));
-        gtk_style_context_add_class (style, "event");
+        context = gtk_widget_get_style_context (GTK_WIDGET (row));
+        gtk_style_context_add_class (context, "event");
         g_object_set_data_full (G_OBJECT (row), "cursor",
                                 g_strdup (result.cursor), g_free);
         grid = gtk_grid_new ();
@@ -605,6 +611,8 @@ insert_journal_query_cmdline (GlJournal *journal,
         gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
 
         label = gtk_label_new (result.message);
+        context = gtk_widget_get_style_context (GTK_WIDGET (row));
+        gtk_style_context_add_class (context, "event-monospace");
         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
         gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
         gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);


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