[gtk+] Keep the currently selected font over theme changes



commit 120775792466580b532d58eb1857a378123db7e3
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 14 23:16:45 2011 -0400

    Keep the currently selected font over theme changes
    
    When repopulating the list of fonts, don't loose the current
    selection, unless the font is no longer in the list.

 gtk/gtkfontchooser.c |   48 ++++++++++++++++++++++++++++++++++--------------
 1 files changed, 34 insertions(+), 14 deletions(-)
---
diff --git a/gtk/gtkfontchooser.c b/gtk/gtkfontchooser.c
index 1437395..7f3989a 100644
--- a/gtk/gtkfontchooser.c
+++ b/gtk/gtkfontchooser.c
@@ -695,8 +695,9 @@ populate_list (GtkFontChooser *fontchooser,
   GtkFontChooserPrivate *priv = fontchooser->priv;
   GtkStyleContext      *style_context;
   PangoFontDescription *default_font;
+  PangoFontDescription *selected_font;
 
-  gboolean selected;
+  gint match;
   GtkTreeIter   match_row;
   GtkTreePath  *path;
 
@@ -717,23 +718,25 @@ populate_list (GtkFontChooser *fontchooser,
 
   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
 
-  gtk_list_store_clear (model);
-
-  /* Get row header font color */
-  style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
-
   /* Get theme font */
+  style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
   default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
                                                                      GTK_STATE_NORMAL);
 
-  selected = FALSE;
+  if (priv->face)
+    selected_font = pango_font_face_describe (priv->face);
+  else
+    selected_font = NULL;
+
+  gtk_list_store_clear (model);
+
+  match = 0;
 
   /* Iterate over families and faces */
   for (i = 0; i < n_families; i++)
     {
       GtkTreeIter     iter;
       PangoFontFace **faces;
-
       int             j, n_faces;
       const gchar    *fam_name = pango_font_family_get_name (families[i]);
 
@@ -767,12 +770,27 @@ populate_list (GtkFontChooser *fontchooser,
                               PREVIEW_TEXT_COLUMN, tmp->str,
                               -1);
 
-          /* Select the first font or the default font/face from the style context */
-          if (!selected ||
-              (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
+          /* Select the current font,
+           * the default font/face from the theme,
+           * or the first font
+           */
+          if (match < 3 &&
+              selected_font != NULL &&
+              pango_font_description_equal (selected_font, pango_desc))
+            {
+              match_row = iter;
+              match = 3;
+            }
+          if (match < 2 &&
+              strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
+            {
+              match_row = iter;
+              match = 2;
+            }
+          if (match < 1)
             {
               match_row = iter;
-              selected = TRUE;
+              match = 1;
             }
 
           pango_font_description_free (pango_desc);
@@ -783,14 +801,16 @@ populate_list (GtkFontChooser *fontchooser,
     }
 
   path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
-
   if (path)
     {
       gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
       gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
-      gtk_tree_path_free(path);
+      gtk_tree_path_free (path);
     }
 
+  if (selected_font)
+    pango_font_description_free (selected_font);
+
   g_string_free (family_and_face, TRUE);
   g_string_free (tmp, TRUE);
   g_free (families);



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