gtk+ r22269 - in trunk: . gtk



Author: behdad
Date: Sun Feb  1 05:26:54 2009
New Revision: 22269
URL: http://svn.gnome.org/viewvc/gtk+?rev=22269&view=rev

Log:
2009-02-01  Behdad Esfahbod  <behdad gnome org>

        Bug 569635 â fontchooser should reload list of families/styles on
        theme change

        * gtk/gtkfontsel.c (gtk_font_selection_class_init),
        (gtk_font_selection_init), (gtk_font_selection_finalize),
        (gtk_font_selection_ref_family), (gtk_font_selection_ref_face),
        (gtk_font_selection_reload_fonts),
        (gtk_font_selection_screen_changed),
        (gtk_font_selection_style_set),
        (gtk_font_selection_scroll_to_selection),
        (gtk_font_selection_scroll_on_map),
        (gtk_font_selection_select_font),
        (gtk_font_selection_show_available_fonts),
        (gtk_font_selection_show_available_styles),
        (gtk_font_selection_select_style),
        (gtk_font_selection_select_font_desc),
        (gtk_font_selection_set_font_name):
        Reload Pango families and faces in style_set.  Installing/uninstalling
        fonts shows up immediately in an open font chooser now.  



Modified:
   trunk/ChangeLog
   trunk/gtk/gtkfontsel.c

Modified: trunk/gtk/gtkfontsel.c
==============================================================================
--- trunk/gtk/gtkfontsel.c	(original)
+++ trunk/gtk/gtkfontsel.c	Sun Feb  1 05:26:54 2009
@@ -124,9 +124,11 @@
 						      guint            prop_id,
 						      GValue          *value,
 						      GParamSpec      *pspec);
-static void    gtk_font_selection_finalize	     (GObject               *object);
-static void    gtk_font_selection_screen_changed     (GtkWidget		    *widget,
-						      GdkScreen             *previous_screen);
+static void    gtk_font_selection_finalize	     (GObject         *object);
+static void    gtk_font_selection_screen_changed     (GtkWidget	      *widget,
+						      GdkScreen       *previous_screen);
+static void    gtk_font_selection_style_set          (GtkWidget      *widget,
+						      GtkStyle       *prev_style);
 
 /* These are the callbacks & related functions. */
 static void     gtk_font_selection_select_font           (GtkTreeSelection *selection,
@@ -155,12 +157,24 @@
 
 static void     gtk_font_selection_preview_changed       (GtkWidget        *entry,
 							  GtkFontSelection *fontsel);
+static void     gtk_font_selection_scroll_to_selection   (GtkFontSelection *fontsel);
+
 
 /* Misc. utility functions. */
 static void    gtk_font_selection_load_font          (GtkFontSelection *fs);
 static void    gtk_font_selection_update_preview     (GtkFontSelection *fs);
 
 static GdkFont* gtk_font_selection_get_font_internal (GtkFontSelection *fontsel);
+static PangoFontDescription *gtk_font_selection_get_font_description (GtkFontSelection *fontsel);
+static gboolean gtk_font_selection_select_font_desc  (GtkFontSelection      *fontsel,
+						      PangoFontDescription  *new_desc,
+						      PangoFontFamily      **pfamily,
+						      PangoFontFace        **pface);
+static void     gtk_font_selection_reload_fonts          (GtkFontSelection *fontsel);
+static void     gtk_font_selection_ref_family            (GtkFontSelection *fontsel,
+							  PangoFontFamily  *family);
+static void     gtk_font_selection_ref_face              (GtkFontSelection *fontsel,
+							  PangoFontFace    *face);
 
 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
 
@@ -174,6 +188,7 @@
   gobject_class->get_property = gtk_font_selection_get_property;
 
   widget_class->screen_changed = gtk_font_selection_screen_changed;
+  widget_class->style_set = gtk_font_selection_style_set;
    
   g_object_class_install_property (gobject_class,
                                    PROP_FONT_NAME,
@@ -534,7 +549,6 @@
 			       -1, INITIAL_PREVIEW_HEIGHT);
   gtk_box_pack_start (GTK_BOX (text_box), fontsel->preview_entry,
 		      TRUE, TRUE, 0);
-
   gtk_widget_pop_composite_child();
 }
 
@@ -567,24 +581,68 @@
   if (fontsel->font)
     gdk_font_unref (fontsel->font);
 
+  gtk_font_selection_ref_family (fontsel, NULL);
+  gtk_font_selection_ref_face (fontsel, NULL);
+
   G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
 }
 
 static void
-gtk_font_selection_screen_changed (GtkWidget *widget,
-				   GdkScreen *previous_screen)
+gtk_font_selection_ref_family (GtkFontSelection *fontsel,
+			       PangoFontFamily  *family)
 {
-  GtkFontSelection *fontsel = GTK_FONT_SELECTION (widget);
+  if (family)
+    family = g_object_ref (family);
+  if (fontsel->family)
+    g_object_unref (fontsel->family);
+  fontsel->family = family;
+}
 
+static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
+					 PangoFontFace    *face)
+{
+  if (face)
+    face = g_object_ref (face);
+  if (fontsel->face)
+    g_object_unref (fontsel->face);
+  fontsel->face = face;
+}
+
+static void
+gtk_font_selection_reload_fonts (GtkFontSelection *fontsel)
+{
   if (gtk_widget_has_screen (GTK_WIDGET (fontsel)))
     {
+      PangoFontDescription *desc;
+      desc = gtk_font_selection_get_font_description (fontsel);
+
       gtk_font_selection_show_available_fonts (fontsel);
       gtk_font_selection_show_available_sizes (fontsel, TRUE);
       gtk_font_selection_show_available_styles (fontsel);
+
+      gtk_font_selection_select_font_desc (fontsel, desc, NULL, NULL);
+      gtk_font_selection_scroll_to_selection (fontsel);
+
+      pango_font_description_free (desc);
     }
 }
 
 static void
+gtk_font_selection_screen_changed (GtkWidget *widget,
+				   GdkScreen *previous_screen)
+{
+  gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
+}
+
+static void
+gtk_font_selection_style_set (GtkWidget *widget,
+			      GtkStyle  *prev_style)
+{
+  /* Maybe fonts where installed or removed... */
+  gtk_font_selection_reload_fonts (GTK_FONT_SELECTION (widget));
+}
+
+static void
 gtk_font_selection_preview_changed (GtkWidget        *entry,
 				    GtkFontSelection *fontsel)
 {
@@ -618,19 +676,9 @@
   gtk_tree_path_free (path);
 }
 
-/* This is called when the list is mapped. Here we scroll to the current
-   font if necessary. */
 static void
-gtk_font_selection_scroll_on_map (GtkWidget		*widget,
-                                  gpointer		 data)
+gtk_font_selection_scroll_to_selection (GtkFontSelection *fontsel)
 {
-  GtkFontSelection *fontsel;
-  
-#ifdef FONTSEL_DEBUG
-  g_message ("In expose_list\n");
-#endif
-  fontsel = GTK_FONT_SELECTION (data);
-  
   /* Try to scroll the font family list to the selected item */
   scroll_to_selection (GTK_TREE_VIEW (fontsel->family_list));
       
@@ -639,6 +687,15 @@
       
   /* Try to scroll the font family list to the selected item */
   scroll_to_selection (GTK_TREE_VIEW (fontsel->size_list));
+/* This is called when the list is mapped. Here we scroll to the current
+   font if necessary. */
+}
+
+static void
+gtk_font_selection_scroll_on_map (GtkWidget		*widget,
+                                  gpointer		 data)
+{
+  gtk_font_selection_scroll_to_selection (GTK_FONT_SELECTION (data));
 }
 
 /* This is called when a family is selected in the list. */
@@ -662,7 +719,7 @@
       gtk_tree_model_get (model, &iter, FAMILY_COLUMN, &family, -1);
       if (fontsel->family != family)
 	{
-	  fontsel->family = family;
+	  gtk_font_selection_ref_family (fontsel, family);
 	  
 #ifdef INCLUDE_FONT_ENTRIES
 	  family_name = pango_font_family_get_name (fontsel->family);
@@ -721,7 +778,7 @@
 	}
     }
 
-  fontsel->family = match_family;
+  gtk_font_selection_ref_family (fontsel, match_family);
   if (match_family)
     {
       set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &match_row);
@@ -837,7 +894,7 @@
   if (old_desc)
     pango_font_description_free (old_desc);
 
-  fontsel->face = match_face;
+  gtk_font_selection_ref_face (fontsel, match_face);
   if (match_face)
     {
 #ifdef INCLUDE_FONT_ENTRIES
@@ -890,8 +947,7 @@
       PangoFontFace *face;
       
       gtk_tree_model_get (model, &iter, FACE_COLUMN, &face, -1);
-      fontsel->face = face;
-
+      gtk_font_selection_ref_face (fontsel, face);
       g_object_unref (face);
     }
 
@@ -1329,48 +1385,26 @@
   return result;
 }
 
-
-/* This sets the current font, selecting the appropriate list rows.
+/* This selects the appropriate list rows.
    First we check the fontname is valid and try to find the font family
    - i.e. the name in the main list. If we can't find that, then just return.
    Next we try to set each of the properties according to the fontname.
    Finally we select the font family & style in the lists. */
-
-/**
- * gtk_font_selection_set_font_name:
- * @fontsel: a #GtkFontSelection
- * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
- * 
- * Sets the currently-selected font. 
- *
- * Note that the @fontsel needs to know the screen in which it will appear 
- * for this to work; this can be guaranteed by simply making sure that the 
- * @fontsel is inserted in a toplevel window before you call this function.
- * 
- * Return value: %TRUE if the font could be set successfully; %FALSE if no 
- *     such font exists or if the @fontsel doesn't belong to a particular 
- *     screen yet.
- */
-gboolean
-gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
-				  const gchar      *fontname)
+static gboolean
+gtk_font_selection_select_font_desc (GtkFontSelection      *fontsel,
+				     PangoFontDescription  *new_desc,
+				     PangoFontFamily      **pfamily,
+				     PangoFontFace        **pface)
 {
   PangoFontFamily *new_family = NULL;
   PangoFontFace *new_face = NULL;
   PangoFontFace *fallback_face = NULL;
-  PangoFontDescription *new_desc;
   GtkTreeModel *model;
   GtkTreeIter iter;
   GtkTreeIter match_iter;
   gboolean valid;
   const gchar *new_family_name;
-  
-  g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
 
-  if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
-    return FALSE;
-
-  new_desc = pango_font_description_from_string (fontname);
   new_family_name = pango_font_description_get_family (new_desc);
 
   if (!new_family_name)
@@ -1389,8 +1423,8 @@
       
       if (g_ascii_strcasecmp (pango_font_family_get_name (family),
 			      new_family_name) == 0)
-	new_family = family;
-      
+	new_family = g_object_ref (family);
+
       g_object_unref (family);
       
       if (new_family)
@@ -1400,7 +1434,10 @@
   if (!new_family)
     return FALSE;
 
-  fontsel->family = new_family;
+  if (pfamily)
+    *pfamily = new_family;
+  else
+    g_object_unref (new_family);
   set_cursor_to_iter (GTK_TREE_VIEW (fontsel->family_list), &iter);
   gtk_font_selection_show_available_styles (fontsel);
 
@@ -1416,11 +1453,11 @@
       tmp_desc = pango_font_face_describe (face);
       
       if (font_description_style_equal (tmp_desc, new_desc))
-	new_face = face;
+	new_face = g_object_ref (face);
       
       if (!fallback_face)
 	{
-	  fallback_face = face;
+	  fallback_face = g_object_ref (face);
 	  match_iter = iter;
 	}
       
@@ -1436,19 +1473,71 @@
 
   if (!new_face)
     new_face = fallback_face;
+  else if (fallback_face)
+    g_object_unref (fallback_face);
 
-  fontsel->face = new_face;
+  if (pface)
+    *pface = new_face;
+  else if (new_face)
+    g_object_unref (new_face);
   set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_iter);  
 
   gtk_font_selection_set_size (fontsel, pango_font_description_get_size (new_desc));
+
+  return TRUE;
+}
+
+
+/* This sets the current font, then selecting the appropriate list rows. */
+
+/**
+ * gtk_font_selection_set_font_name:
+ * @fontsel: a #GtkFontSelection
+ * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
+ * 
+ * Sets the currently-selected font. 
+ *
+ * Note that the @fontsel needs to know the screen in which it will appear 
+ * for this to work; this can be guaranteed by simply making sure that the 
+ * @fontsel is inserted in a toplevel window before you call this function.
+ * 
+ * Return value: %TRUE if the font could be set successfully; %FALSE if no 
+ *     such font exists or if the @fontsel doesn't belong to a particular 
+ *     screen yet.
+ */
+gboolean
+gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
+				  const gchar      *fontname)
+{
+  PangoFontFamily *family = NULL;
+  PangoFontFace *face = NULL;
+  PangoFontDescription *new_desc;
+  
+  g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
+
+  if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
+    return FALSE;
+
+  new_desc = pango_font_description_from_string (fontname);
+
+  if (gtk_font_selection_select_font_desc (fontsel, new_desc, &family, &face))
+    {
+      gtk_font_selection_ref_family (fontsel, family);
+      if (family)
+        g_object_unref (family);
+
+      gtk_font_selection_ref_face (fontsel, face);
+      if (face)
+        g_object_unref (face);
+    }
+
+  pango_font_description_free (new_desc);
   
   g_object_freeze_notify (G_OBJECT (fontsel));
   g_object_notify (G_OBJECT (fontsel), "font-name");
   g_object_notify (G_OBJECT (fontsel), "font");
   g_object_thaw_notify (G_OBJECT (fontsel));
 
-  pango_font_description_free (new_desc);
-
   return TRUE;
 }
 



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