[Xfce4-commits] [apps/mousepad] 13/45: Refactor for view-font-name GSetting

noreply at xfce.org noreply at xfce.org
Fri Jul 11 13:03:18 CEST 2014


This is an automated email from the git hooks/post-receive script.

mbrush pushed a commit to branch master
in repository apps/mousepad.

commit 8b224a3ea10c73d094390b1f3daf4271bb958ebe
Author: Matthew Brush <mbrush at codebrainz.ca>
Date:   Sun Jul 6 17:53:54 2014 -0700

    Refactor for view-font-name GSetting
    
    Move _set_font() from MousepadDocument to MousepadView. Add glue to
    bind the setting change to update the view's font.
---
 mousepad/mousepad-document.c |   24 +------------------
 mousepad/mousepad-document.h |    3 ---
 mousepad/mousepad-view.c     |   54 ++++++++++++++++++++++++++++++++++++++++++
 mousepad/mousepad-view.h     |    2 ++
 mousepad/mousepad-window.c   |   18 --------------
 5 files changed, 57 insertions(+), 44 deletions(-)

diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index 6da62fa..40f331e 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -177,7 +177,7 @@ mousepad_document_init (MousepadDocument *document)
 {
   GtkTargetList        *target_list;
   gboolean              word_wrap, insert_spaces;
-  gchar                *font_name, *color_scheme;
+  gchar                *color_scheme;
   gint                  tab_size;
   GtkSourceStyleScheme *scheme = NULL;
 
@@ -218,14 +218,12 @@ mousepad_document_init (MousepadDocument *document)
 
   /* read all the default settings */
   word_wrap = mousepad_settings_get_boolean ("view-word-wrap");
-  font_name = mousepad_settings_get_string ("view-font-name");
   tab_size = mousepad_settings_get_int ("view-tab-size");
   insert_spaces = mousepad_settings_get_boolean ("view-insert-spaces");
   color_scheme = mousepad_settings_get_string ("view-color-scheme");
 
   /* set all the settings */
   mousepad_document_set_word_wrap (document, word_wrap);
-  mousepad_document_set_font (document, font_name);
   mousepad_view_set_tab_size (document->textview, tab_size);
   mousepad_view_set_insert_spaces (document->textview, insert_spaces);
 
@@ -235,7 +233,6 @@ mousepad_document_init (MousepadDocument *document)
   gtk_source_buffer_set_style_scheme (GTK_SOURCE_BUFFER (document->buffer), scheme);
 
   /* cleanup */
-  g_free (font_name);
   g_free (color_scheme);
 
   /* attach signals to the text view and buffer */
@@ -487,25 +484,6 @@ mousepad_document_set_word_wrap (MousepadDocument *document,
 
 
 void
-mousepad_document_set_font (MousepadDocument *document,
-                            const gchar      *font_name)
-{
-  PangoFontDescription *font_desc;
-
-  mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-
-  if (G_LIKELY (font_name))
-    {
-      /* set the widget font */
-      font_desc = pango_font_description_from_string (font_name);
-      gtk_widget_modify_font (GTK_WIDGET (document->textview), font_desc);
-      pango_font_description_free (font_desc);
-    }
-}
-
-
-
-void
 mousepad_document_focus_textview (MousepadDocument *document)
 {
   mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
diff --git a/mousepad/mousepad-document.h b/mousepad/mousepad-document.h
index f2176c1..cbe3f4f 100644
--- a/mousepad/mousepad-document.h
+++ b/mousepad/mousepad-document.h
@@ -60,9 +60,6 @@ GType             mousepad_document_get_type       (void) G_GNUC_CONST;
 
 MousepadDocument *mousepad_document_new            (void);
 
-void              mousepad_document_set_font       (MousepadDocument *document,
-                                                    const gchar      *font_name);
-
 void              mousepad_document_set_overwrite  (MousepadDocument *document,
                                                     gboolean          overwrite);
 
diff --git a/mousepad/mousepad-view.c b/mousepad/mousepad-view.c
index 6597219..80ed437 100644
--- a/mousepad/mousepad-view.c
+++ b/mousepad/mousepad-view.c
@@ -139,9 +139,52 @@ mousepad_view_class_init (MousepadViewClass *klass)
 
 
 
+void
+mousepad_view_set_font_name (MousepadView *view,
+                             const gchar  *font_name)
+{
+  PangoFontDescription *font_desc;
+
+  g_return_if_fail (MOUSEPAD_IS_VIEW (view));
+
+  if (font_name == NULL)
+    font_name = "Monospace";
+
+  font_desc = pango_font_description_from_string (font_name);
+
+  if (G_LIKELY (font_desc != NULL))
+    {
+#if GTK_CHECK_VERSION(3, 0, 0)
+      gtk_widget_override_font (GTK_WIDGET (view), font_desc);
+#else
+      gtk_widget_modify_font (GTK_WIDGET (view), font_desc);
+#endif
+      pango_font_description_free (font_desc);
+    }
+  else
+    g_critical ("Invalid font-name given: %s", font_name);
+}
+
+
+
+/* when the view-font-name setting changes, update the view to use that font */
+static void
+mousepad_view_font_name_setting_changed (MousepadView     *view,
+                                         gchar            *key,
+                                         MousepadSettings *settings)
+{
+  gchar *font_name = mousepad_settings_get_string ("view-font-name");
+  mousepad_view_set_font_name (view, font_name);
+  g_free (font_name);
+}
+
+
+
 static void
 mousepad_view_init (MousepadView *view)
 {
+  gchar *font_name;
+
   /* initialize selection variables */
   view->selection_timeout_id = 0;
   view->selection_tag = NULL;
@@ -159,6 +202,17 @@ mousepad_view_init (MousepadView *view)
   /* bind Gsettings */
   mousepad_settings_bind ("view-line-numbers", view, "show-line-numbers", G_SETTINGS_BIND_DEFAULT);
   mousepad_settings_bind ("view-auto-indent", view, "auto-indent", G_SETTINGS_BIND_DEFAULT);
+
+  /* Set the initial font-name */
+  font_name = mousepad_settings_get_string ("view-font-name");
+  mousepad_view_set_font_name (view, font_name);
+  g_free (font_name);
+
+  /* connect to Gsettings change notifications */
+  g_signal_connect_swapped (MOUSEPAD_GSETTINGS,
+                            "changed::view-font-name",
+                            G_CALLBACK (mousepad_view_font_name_setting_changed),
+                            view);
 }
 
 
diff --git a/mousepad/mousepad-view.h b/mousepad/mousepad-view.h
index aff9422..1463be4 100644
--- a/mousepad/mousepad-view.h
+++ b/mousepad/mousepad-view.h
@@ -107,6 +107,8 @@ gint            mousepad_view_get_tab_size              (MousepadView      *view
 
 gboolean        mousepad_view_get_insert_spaces         (MousepadView      *view);
 
+void            mousepad_view_set_font_name             (MousepadView      *view,
+                                                         const gchar       *font_name);
 
 G_END_DECLS
 
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index d3fd22b..990a5d5 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -4401,9 +4401,7 @@ mousepad_window_action_select_font (GtkAction      *action,
                                     MousepadWindow *window)
 {
   GtkWidget        *dialog;
-  MousepadDocument *document;
   gchar            *font_name;
-  gint              i;
 
   mousepad_return_if_fail (MOUSEPAD_IS_WINDOW (window));
   mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
@@ -4428,22 +4426,6 @@ mousepad_window_action_select_font (GtkAction      *action,
       /* store the font in the preferences */
       mousepad_settings_set_string ("view-font-name", font_name);
 
-      /* apply the font in all documents in this window */
-      for (i = 0; i < gtk_notebook_get_n_pages (GTK_NOTEBOOK (window->notebook)); i++)
-        {
-          /* get the document */
-          document = MOUSEPAD_DOCUMENT (gtk_notebook_get_nth_page (GTK_NOTEBOOK (window->notebook), i));
-
-          /* debug check */
-          mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (document));
-
-          /* set the font */
-          mousepad_document_set_font (document, font_name);
-
-          /* update the tab array */
-          mousepad_view_set_tab_size (document->textview, mousepad_view_get_tab_size (document->textview));
-        }
-
       /* cleanup */
       g_free (font_name);
     }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list