[gtk+] GtkFontChooser: implement gtk_font_selection_set/get_preview_text



commit 31776e2a2558f066ac2429b1e21ee7ac517d9632
Author: Alberto Ruiz <aruiz gnome org>
Date:   Tue May 3 19:59:57 2011 +0100

    GtkFontChooser: implement gtk_font_selection_set/get_preview_text

 gtk/gtkfontchooser.c      |   43 +++++++++++++++++++++++++++----------------
 tests/testfontselection.c |   25 ++++++++++++++++---------
 2 files changed, 43 insertions(+), 25 deletions(-)
---
diff --git a/gtk/gtkfontchooser.c b/gtk/gtkfontchooser.c
index 8a98b1e..eb47183 100644
--- a/gtk/gtkfontchooser.c
+++ b/gtk/gtkfontchooser.c
@@ -92,14 +92,16 @@ struct _GtkFontSelectionPrivate
   GtkWidget *size_spin;
   GtkWidget *preview;
 
+  gboolean         ignore_slider;
+
   GtkListStore *model;  
   GtkTreeModel *filter;
 
   gint             size;
   PangoFontFace   *face;
   PangoFontFamily *family;
-  
-  gboolean         ignore_slider;
+
+  gchar           *preview_text;
 
 #ifndef GTK_DISABLE_DEPRECATED
   GtkWidget    *size_list;
@@ -597,6 +599,9 @@ gtk_font_selection_init (GtkFontSelection *fontsel)
   priv->ignore_font = FALSE;
 #endif /* GTK_DISABLE_DEPRECATED */
 
+  /* Default preview string  */
+  priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
+
   /* Getting the default size */
   font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
   priv->size = pango_font_description_get_size (font_desc);
@@ -747,7 +752,7 @@ cmp_families (const void *a, const void *b)
 }
 
 static void 
-populate_list (GtkTreeView* treeview, GtkListStore* model)
+populate_list (GtkFontSelection *fontsel, GtkTreeView* treeview, GtkListStore* model)
 {
   GtkStyleContext      *style_context;
   GdkRGBA               g_color;
@@ -813,7 +818,7 @@ populate_list (GtkTreeView* treeview, GtkListStore* model)
                                 color_string,
                                 family_and_face->str,
                                 font_desc,
-                                pango_language_get_sample_string (NULL));
+                                fontsel->priv->preview_text);
 
           gtk_list_store_append (model, &iter);
           gtk_list_store_set (model, &iter,
@@ -928,7 +933,7 @@ gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
 
   gtk_tree_view_append_column (treeview, col);
 
-  populate_list (treeview, fontsel->priv->model);
+  populate_list (fontsel, treeview, fontsel->priv->model);
 }
 
 
@@ -1626,37 +1631,43 @@ gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
  *
  * Gets the text displayed in the preview area.
  * 
- * Return value: the text displayed in the preview area. 
- *     This string is owned by the widget and should not be 
- *     modified or freed 
+ * Return value: (transfer none): the text displayed in the
+ *     preview area. This string is owned by the widget and
+ *     should not be modified or freed 
  */
 G_CONST_RETURN gchar*
 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
 {
-  return NULL;
+  g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
+  return (const gchar*)fontsel->priv->preview_text;
 }
 
 
 /**
  * gtk_font_selection_set_preview_text:
  * @fontsel: a #GtkFontSelection
- * @text: the text to display in the preview area 
+ * @text: (transfer none): the text to display in the preview area 
  *
  * Sets the text displayed in the preview area.
  * The @text is used to show how the selected font looks.
  */
 void
 gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
-              const gchar      *text)
+                                      const gchar      *text)
 {
-#if 0
-  GtkFontSelectionPrivate *priv;
-
   g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
   g_return_if_fail (text != NULL);
 
-  priv = fontsel->priv;
-#endif
+  g_free (fontsel->priv->preview_text);
+  fontsel->priv->preview_text = g_strdup (text);
+
+  populate_list (fontsel,
+                 GTK_TREE_VIEW (fontsel->priv->family_face_list),
+                 fontsel->priv->model);
+
+  gtk_entry_set_text (GTK_ENTRY (fontsel->priv->preview), text);
+
+  g_object_notify (G_OBJECT (fontsel), "preview-text");
 }
 
 #ifndef GTK_DISABLE_DEPRECATED
diff --git a/tests/testfontselection.c b/tests/testfontselection.c
index 20bb832..21dc4ff 100644
--- a/tests/testfontselection.c
+++ b/tests/testfontselection.c
@@ -27,6 +27,11 @@ notify_font_name_cb (GObject *fontsel, GParamSpec *pspec, gpointer data)
   g_debug ("Changed font name %s", gtk_font_selection_get_font_name (GTK_FONT_SELECTION (fontsel)));
 }
 
+static void
+notify_preview_text_cb (GObject *fontsel, GParamSpec *pspec, gpointer data)
+{
+  g_debug ("Changed preview text %s", gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (fontsel)));
+}
 
 int
 main (int argc, char *argv[])
@@ -44,21 +49,23 @@ main (int argc, char *argv[])
   hbox = gtk_hbox_new (FALSE, 6);
   gtk_container_add (GTK_CONTAINER (window), hbox);
 
-  g_object_ref (gtk_font_selection_get_size_list (GTK_FONT_SELECTION (fontsel)));
-  g_object_ref (gtk_font_selection_get_family_list (GTK_FONT_SELECTION (fontsel)));
-  g_object_ref (gtk_font_selection_get_face_list (GTK_FONT_SELECTION (fontsel)));
+//  g_object_ref (gtk_font_selection_get_size_list (GTK_FONT_SELECTION (fontsel)));
+//  g_object_ref (gtk_font_selection_get_family_list (GTK_FONT_SELECTION (fontsel)));
+//  g_object_ref (gtk_font_selection_get_face_list (GTK_FONT_SELECTION (fontsel)));
 
-  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_size_list (GTK_FONT_SELECTION (fontsel)));
-  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_family_list (GTK_FONT_SELECTION (fontsel)));
-  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_face_list (GTK_FONT_SELECTION (fontsel)));
+//  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_size_list (GTK_FONT_SELECTION (fontsel)));
+//  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_family_list (GTK_FONT_SELECTION (fontsel)));
+//  gtk_container_add (GTK_CONTAINER (hbox), gtk_font_selection_get_face_list (GTK_FONT_SELECTION (fontsel)));
   gtk_container_add (GTK_CONTAINER (hbox), fontsel);
 
   gtk_widget_show_all (window);
 
-  gtk_font_selection_set_font_name (GTK_FONT_SELECTION (fontsel), "Bitstream Vera Sans 45");
+  g_signal_connect (G_OBJECT (window), "delete-event",          G_CALLBACK(gtk_main_quit), NULL);
+  g_signal_connect (G_OBJECT (fontsel), "notify::font-name",    G_CALLBACK(notify_font_name_cb), NULL);
+  g_signal_connect (G_OBJECT (fontsel), "notify::preview-text", G_CALLBACK(notify_preview_text_cb), NULL);
 
-  g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_main_quit), NULL);
-  g_signal_connect (G_OBJECT (fontsel), "notify::font-name", G_CALLBACK(notify_font_name_cb), NULL);
+  gtk_font_selection_set_font_name (GTK_FONT_SELECTION (fontsel), "Bitstream Vera Sans 45");
+  gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (fontsel), "Siete caballos vienen de bonanza");
 
   gtk_main ();
 



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