Make setting fontname work, independent of a screen

This commit is contained in:
Matthias Clasen 2011-08-14 23:46:14 -04:00
parent 1207757924
commit 6877893ed5

View File

@ -91,6 +91,7 @@ struct _GtkFontChooserPrivate
GtkWidget *size_spin;
GtkWidget *size_slider;
gchar *fontname;
gint size;
PangoFontFace *face;
PangoFontFamily *family;
@ -157,6 +158,8 @@ static void gtk_font_chooser_style_updated (GtkWidget *widget);
static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
static gboolean gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser);
G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
static void
@ -718,7 +721,6 @@ populate_list (GtkFontChooser *fontchooser,
qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
/* 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);
@ -928,6 +930,9 @@ gtk_font_chooser_finalize (GObject *object)
GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
GtkFontChooserPrivate *priv = fontchooser->priv;
if (priv->fontname)
g_free (priv->fontname);
if (priv->family)
g_object_unref (priv->family);
@ -945,10 +950,14 @@ gtk_font_chooser_screen_changed (GtkWidget *widget,
GdkScreen *previous_screen)
{
GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
GtkFontChooserPrivate *priv = fontchooser->priv;
populate_list (fontchooser,
GTK_TREE_VIEW (fontchooser->priv->family_face_list),
fontchooser->priv->model);
GTK_TREE_VIEW (priv->family_face_list),
priv->model);
if (priv->fontname)
gtk_font_chooser_select_font_name (fontchooser);
}
static void
@ -1071,11 +1080,6 @@ gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
*
* Sets the currently-selected font.
*
* Note that the @fontchooser needs to know the screen in which
* it will appear for this to work; this can be guaranteed by simply
* making sure that the @fontchooser 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 @fontchooser doesn't belong
* to a particular screen yet.
@ -1085,6 +1089,27 @@ gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
gboolean
gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
const gchar *fontname)
{
GtkFontChooserPrivate *priv = fontchooser->priv;
gboolean found = FALSE;
g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
g_return_val_if_fail (fontname != NULL, FALSE);
if (priv->fontname)
g_free (priv->fontname);
priv->fontname = g_strdup (fontname);
if (gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
found = gtk_font_chooser_select_font_name (fontchooser);
g_object_notify (G_OBJECT (fontchooser), "font-name");
return found;
}
static gboolean
gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser)
{
GtkFontChooserPrivate *priv = fontchooser->priv;
GtkTreeIter iter;
@ -1093,15 +1118,12 @@ gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
PangoFontDescription *desc;
gboolean found = FALSE;
g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
g_return_val_if_fail (fontname != NULL, FALSE);
if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
return FALSE;
desc = pango_font_description_from_string (fontname);
desc = pango_font_description_from_string (priv->fontname);
family_name = (gchar*)pango_font_description_get_family (desc);
g_free (priv->fontname);
priv->fontname = NULL;
if (!family_name)
{
pango_font_description_free (desc);
@ -1172,7 +1194,6 @@ gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
}
pango_font_description_free (desc);
g_object_notify (G_OBJECT (fontchooser), "font-name");
return found;
}