]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
GtkFontChooser: Better cursor selection and model leak fixes
[~andy/gtk] / gtk / gtkfontchooser.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * Massively updated for Pango by Owen Taylor, May 2000
5  * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
6  * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
7  * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /*
26  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
30  */
31
32 #include "config.h"
33
34 #include <stdlib.h>
35 #include <glib/gprintf.h>
36 #include <string.h>
37
38 #include <atk/atk.h>
39
40 #include "gtkfontsel.h"
41 #include "gtkbutton.h"
42 #include "gtkcellrenderertext.h"
43 #include "gtkentry.h"
44 #include "gtkframe.h"
45 #include "gtkhbbox.h"
46 #include "gtkhbox.h"
47 #include "gtklabel.h"
48 #include "gtkliststore.h"
49 #include "gtkrc.h"
50 #include "gtkstock.h"
51 #include "gtktable.h"
52 #include "gtktreeselection.h"
53 #include "gtktreeview.h"
54 #include "gtkvbox.h"
55 #include "gtkscrolledwindow.h"
56 #include "gtkintl.h"
57 #include "gtkaccessible.h"
58 #include "gtkbuildable.h"
59 #include "gtkprivate.h"
60 #include "gtkalignment.h"
61 #include "gtkscale.h"
62 #include "gtkbox.h"
63 #include "gtkspinbutton.h"
64 #include "gtkwidget.h"
65
66 /**
67  * SECTION:gtkfontsel
68  * @Short_description: A widget for selecting fonts
69  * @Title: GtkFontSelection
70  * @See_also: #GtkFontSelectionDialog
71  *
72  * The #GtkFontSelection widget lists the available fonts, styles and sizes,
73  * allowing the user to select a font.
74  * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
75  * selecting fonts.
76  *
77  * To set the font which is initially selected, use
78  * gtk_font_selection_set_font_name().
79  *
80  * To get the selected font use gtk_font_selection_get_font_name().
81  *
82  * To change the text which is shown in the preview area, use
83  * gtk_font_selection_set_preview_text().
84  */
85
86
87 struct _GtkFontSelectionPrivate
88 {
89   GtkWidget *search_entry;
90   GtkWidget *family_face_list;
91   GtkWidget *size_slider;
92   GtkWidget *size_spin;
93   GtkWidget *preview;
94
95   GtkListStore *model;  
96   GtkTreeModel *filter;
97
98   gint             size;
99   PangoFontFace   *face;
100   PangoFontFamily *family;
101   
102   gboolean         ignore_slider;
103 };
104
105
106 struct _GtkFontSelectionDialogPrivate
107 {
108   GtkWidget *fontsel;
109
110   GtkWidget *ok_button;
111   GtkWidget *apply_button;
112   GtkWidget *cancel_button;
113 };
114
115
116 /* We don't enable the font and style entries because they don't add
117  * much in terms of visible effect and have a weird effect on keynav.
118  * the Windows font selector has entries similarly positioned but they
119  * act in conjunction with the associated lists to form a single focus
120  * location.
121  */
122 #undef INCLUDE_FONT_ENTRIES
123
124 /* This is the default text shown in the preview entry, though the user
125    can set it. Remember that some fonts only have capital letters. */
126 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
127
128 #define DEFAULT_FONT_NAME "Sans 10"
129 #define MAX_FONT_SIZE 999
130
131 /* This is the initial fixed height and the top padding of the preview entry */
132 #define PREVIEW_HEIGHT 84
133 #define PREVIEW_TOP_PADDING 6
134
135 /* These are the sizes of the font, style & size lists. */
136 #define FONT_LIST_HEIGHT        136
137 #define FONT_LIST_WIDTH         190
138 #define FONT_STYLE_LIST_WIDTH   170
139 #define FONT_SIZE_LIST_WIDTH    60
140
141 #define ROW_FORMAT_STRING "<span size=\"small\" foreground=\"%s\">%s <i>%s</i></span>\n<span size=\"large\" font_desc=\"%s\">%s</span>"
142
143 /* These are what we use as the standard font sizes, for the size list.
144  */
145 #define FONT_SIZES_LENGTH 25
146 static const gint font_sizes[] = {
147   6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
148   32, 36, 40, 48, 56, 64, 72
149 };
150
151 enum {
152    PROP_0,
153    PROP_FONT_NAME,
154    PROP_PREVIEW_TEXT
155 };
156
157
158 enum {
159   FAMILY_COLUMN,
160   FACE_COLUMN,
161   FAMILY_NAME_COLUMN,
162   TEXT_COLUMN
163 };
164
165 static void    gtk_font_selection_set_property       (GObject         *object,
166                                                       guint            prop_id,
167                                                       const GValue    *value,
168                                                       GParamSpec      *pspec);
169 static void    gtk_font_selection_get_property       (GObject         *object,
170                                                       guint            prop_id,
171                                                       GValue          *value,
172                                                       GParamSpec      *pspec);
173 static void    gtk_font_selection_finalize           (GObject         *object);
174 static void    gtk_font_selection_screen_changed     (GtkWidget       *widget,
175                                                       GdkScreen       *previous_screen);
176 static void    gtk_font_selection_style_updated      (GtkWidget      *widget);
177
178 static void     gtk_font_selection_ref_family            (GtkFontSelection *fontsel,
179                                                           PangoFontFamily  *family);
180 static void     gtk_font_selection_ref_face              (GtkFontSelection *fontsel,
181                                                           PangoFontFace    *face);
182 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
183
184 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
185
186 static void
187 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
188 {
189   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
190   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
191
192   gobject_class->finalize = gtk_font_selection_finalize;
193   gobject_class->set_property = gtk_font_selection_set_property;
194   gobject_class->get_property = gtk_font_selection_get_property;
195
196   widget_class->screen_changed = gtk_font_selection_screen_changed;
197   widget_class->style_updated = gtk_font_selection_style_updated;
198    
199   g_object_class_install_property (gobject_class,
200                                    PROP_FONT_NAME,
201                                    g_param_spec_string ("font-name",
202                                                         P_("Font name"),
203                                                         P_("The string that represents this font"),
204                                                         DEFAULT_FONT_NAME,
205                                                         GTK_PARAM_READWRITE));
206   g_object_class_install_property (gobject_class,
207                                    PROP_PREVIEW_TEXT,
208                                    g_param_spec_string ("preview-text",
209                                                         P_("Preview text"),
210                                                         P_("The text to display in order to demonstrate the selected font"),
211                                                         _(PREVIEW_TEXT),
212                                                         GTK_PARAM_READWRITE));
213
214   g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
215 }
216
217 static void 
218 gtk_font_selection_set_property (GObject         *object,
219                                  guint            prop_id,
220                                  const GValue    *value,
221                                  GParamSpec      *pspec)
222 {
223   GtkFontSelection *fontsel;
224
225   fontsel = GTK_FONT_SELECTION (object);
226
227   switch (prop_id)
228     {
229     case PROP_FONT_NAME:
230       gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
231       break;
232     case PROP_PREVIEW_TEXT:
233       gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
234       break;
235     default:
236       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237       break;
238     }
239 }
240
241 static void gtk_font_selection_get_property (GObject         *object,
242                                              guint            prop_id,
243                                              GValue          *value,
244                                              GParamSpec      *pspec)
245 {
246   GtkFontSelection *fontsel;
247
248   fontsel = GTK_FONT_SELECTION (object);
249
250   switch (prop_id)
251     {
252     case PROP_FONT_NAME:
253       g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
254       break;
255     case PROP_PREVIEW_TEXT:
256       g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
257       break;
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261     }
262 }
263
264 void
265 deleted_text_cb (GtkEntryBuffer *buffer,
266                  guint           position,
267                  guint           n_chars,
268                  gpointer        user_data)
269 {
270   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
271   GtkWidget               *entry = priv->search_entry;
272   
273   if (gtk_entry_buffer_get_length (buffer) == 0)
274     {
275       gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
276                                      GTK_ENTRY_ICON_SECONDARY,
277                                      GTK_STOCK_FIND);
278     }
279
280   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
281 }
282
283 void
284 inserted_text_cb (GtkEntryBuffer *buffer,
285                   guint           position,
286                   gchar          *chars,
287                   guint           n_chars,
288                   gpointer        user_data) 
289 {
290   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
291   GtkWidget               *entry = priv->search_entry;
292
293   if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
294                  GTK_STOCK_CLEAR))
295     gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
296                                    GTK_ENTRY_ICON_SECONDARY,
297                                    GTK_STOCK_CLEAR);
298
299
300   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
301 }
302
303 void
304 icon_press_cb (GtkEntry             *entry,
305                GtkEntryIconPosition  pos,
306                GdkEvent             *event,
307                gpointer              user_data)
308 {
309   gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
310 }
311
312 void
313 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
314 {
315   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
316
317   /* If we set the silder value manually, we ignore this callback */
318   if (priv->ignore_slider)
319     {
320       priv->ignore_slider = FALSE;
321       return;
322     }
323
324   gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
325                             gtk_adjustment_get_value (adjustment));
326 }
327
328 void
329 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
330 {
331   PangoFontDescription *desc;
332   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
333
334   gdouble size = gtk_adjustment_get_value (adjustment);
335   
336   GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
337
338   /* We ignore the slider value change callback for both of this set_value call */
339   if (size < gtk_adjustment_get_lower (slider_adj))
340     {
341       priv->ignore_slider = TRUE;
342       gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
343     }
344   else if (size > gtk_adjustment_get_upper (slider_adj))
345     {
346       priv->ignore_slider = TRUE;
347       gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
348     }
349
350   priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
351
352   desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
353   pango_font_description_set_size (desc, priv->size);
354   gtk_widget_override_font (priv->preview, desc);
355   
356   gtk_widget_queue_draw (priv->preview);
357 }
358
359 void
360 set_range_marks (GtkWidget* size_slider, gint* sizes, gint length)
361 {
362   gint i;
363   
364   gtk_scale_clear_marks (GTK_SCALE (size_slider));
365   
366   for (i=0; i<length; i++)
367     gtk_scale_add_mark (GTK_SCALE (size_slider),
368                         (gdouble) sizes[i],
369                         GTK_POS_BOTTOM, NULL);
370 }
371
372 void
373 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
374 {
375   gchar                *family_name;
376   PangoFontFace        *face;
377   PangoFontDescription *desc;
378   
379   gint *sizes;
380   gint  n_sizes;
381
382   GtkTreeIter iter;
383   GtkTreePath *path = gtk_tree_path_new ();
384
385   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
386   
387   gtk_tree_view_get_cursor (treeview, &path, NULL);
388   
389   if (!path)
390     return;
391
392   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->model), &iter, path))
393     return;
394   
395   gtk_tree_model_get (GTK_TREE_MODEL (priv->model), &iter,
396                       FACE_COLUMN, &face,
397                       FAMILY_NAME_COLUMN, &family_name,
398                       -1);
399
400   if (!face && !family_name)
401     return;
402   if (!face)
403     {
404       g_free (family_name);
405       return;
406     }
407   if (!family_name)
408     {
409       g_object_unref ((gpointer)face);
410       return;
411     }
412
413   desc = pango_font_face_describe (face);
414   pango_font_description_set_size (desc, priv->size);
415   gtk_widget_override_font (priv->preview, desc);
416
417   pango_font_face_list_sizes (face, &sizes, &n_sizes);
418   /* It seems not many fonts actually have a sane set of sizes */
419   /* set_range_marks (priv->size_slider, sizes, n_sizes); */
420
421   /* Free resources */
422   g_free (family_name);
423   g_object_unref ((gpointer)face);
424   pango_font_description_free(desc);
425   gtk_tree_path_free (path);
426 }
427
428 static void
429 gtk_font_selection_init (GtkFontSelection *fontsel)
430 {
431   GtkFontSelectionPrivate *priv;
432   PangoFontDescription    *font_desc;
433   GtkWidget               *scrolled_win;
434   GtkWidget               *alignment;
435   GtkWidget               *preview_and_size;
436   GtkWidget               *size_controls;
437 #if 0
438   GList                   *focus_chain = NULL;
439   AtkObject *atk_obj;
440 #endif
441
442   fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
443                                                GTK_TYPE_FONT_SELECTION,
444                                                GtkFontSelectionPrivate);
445   priv = fontsel->priv;
446   gtk_widget_push_composite_child ();
447
448   /* Creating fundamental widgets for the private struct */
449   priv->search_entry = gtk_entry_new ();
450   priv->family_face_list = gtk_tree_view_new ();
451   priv->preview = gtk_entry_new ();
452   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
453                                                 (gdouble) font_sizes[0],
454                                                 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
455                                                 1.0);
456
457   priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
458
459   /** Bootstrapping widget layout **/
460   gtk_box_set_spacing (GTK_BOX (fontsel), 6);
461   gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
462
463   /* Main font family/face view */
464   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
465   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
466                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
467   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
468
469   /* Alignment for the preview and size controls */
470   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
471   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
472                              PREVIEW_TOP_PADDING, 0, 0, 0);
473   gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
474
475   preview_and_size = gtk_vbox_new (TRUE, 0);
476   gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
477   
478   /* The preview entry needs a scrolled window to make sure we have a */
479   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
480   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
481                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
482   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
483                                          priv->preview);
484   gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
485   
486   /* Setting the size requests for various widgets */
487   gtk_widget_set_size_request (GTK_WIDGET (fontsel), -1, 460);
488   gtk_widget_set_size_request (scrolled_win,  -1, PREVIEW_HEIGHT);
489   gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
490
491   /* Unset the frame on the preview entry and set a shadow in the scrolled window */
492   gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
493   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
494                                        GTK_SHADOW_OUT);
495
496   /* Packing the slider and the spin in a hbox */
497   size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
498   gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
499   gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
500   gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
501   
502   gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
503
504   gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
505   gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
506
507   gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
508
509   /* Getting the default size */
510   font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
511   priv->size = pango_font_description_get_size (font_desc);
512   priv->face = NULL;
513   priv->family = NULL;
514   
515   gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
516                             (gdouble)(priv->size / PANGO_SCALE));
517   gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
518                             (gdouble)(priv->size / PANGO_SCALE));
519
520   gtk_widget_show_all (GTK_WIDGET (fontsel));
521   gtk_widget_hide (GTK_WIDGET (fontsel));
522
523   /* Treeview column and model bootstrapping */
524   gtk_font_selection_bootstrap_fontlist (fontsel);
525   
526   /* Set default preview text */
527   gtk_entry_set_text (GTK_ENTRY (priv->preview),
528                       pango_language_get_sample_string (NULL));
529   
530   /* Set search icon and place holder text */
531   gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
532                                  GTK_ENTRY_ICON_SECONDARY,
533                                  GTK_STOCK_FIND);
534   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
535   
536   /** Callback connections **/
537   /* Connect to callback for the live search text entry */
538   g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
539                     "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
540   g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
541                     "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
542   g_signal_connect (G_OBJECT (priv->search_entry),
543                     "icon-press", G_CALLBACK (icon_press_cb), (gpointer)priv);
544
545   /* Size controls callbacks */
546   g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
547                     "value-changed", G_CALLBACK (slider_change_cb), (gpointer)priv);
548   g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
549                     "value-changed", G_CALLBACK (spin_change_cb), (gpointer)priv);
550   priv->ignore_slider = FALSE;
551   
552   /* Font selection callbacks */
553   g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
554                     G_CALLBACK (cursor_changed_cb),    (gpointer)priv);
555                     
556                     
557   set_range_marks (priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
558                     
559   gtk_widget_pop_composite_child();
560 }
561
562 /**
563  * gtk_font_selection_new:
564  *
565  * Creates a new #GtkFontSelection.
566  *
567  * Return value: a n ew #GtkFontSelection
568  */
569 GtkWidget *
570 gtk_font_selection_new (void)
571 {
572   GtkFontSelection *fontsel;
573   
574   fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
575   
576   return GTK_WIDGET (fontsel);
577 }
578
579 static int
580 cmp_families (const void *a, const void *b)
581 {
582   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
583   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
584
585   return g_utf8_collate (a_name, b_name);
586 }
587
588 static void 
589 populate_list (GtkTreeView* treeview, GtkListStore* model)
590 {
591   GtkStyleContext *style_context;
592   GdkRGBA          g_color;
593   PangoColor       p_color;
594   gchar            *color_string;
595
596   GtkTreeIter   match_row;
597   GtkTreePath  *path;
598
599   gint n_families, i;  
600   PangoFontFamily **families;
601
602   GString     *tmp = g_string_new (NULL);
603
604   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
605                                &families,
606                                &n_families);
607
608   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
609
610   gtk_list_store_clear (model);
611
612   /* Get row header font color */
613   style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
614   gtk_style_context_get_color (style_context,
615                                GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
616                                &g_color);
617
618   p_color.red   = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
619   p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
620   p_color.blue  = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
621   color_string  = pango_color_to_string (&p_color);
622
623   /* Iterate over families and faces */
624   for (i=0; i<n_families; i++)
625     {
626       GtkTreeIter     iter;
627       PangoFontFace **faces;
628       int             j, n_faces;
629       const gchar    *fam_name = pango_font_family_get_name (families[i]);
630
631       pango_font_family_list_faces (families[i], &faces, &n_faces);
632       
633       for (j=0; j<n_faces; j++)
634         {
635           PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
636           const gchar *face_name = pango_font_face_get_face_name (faces[j]);
637           gchar       *font_desc = pango_font_description_to_string (pango_desc);
638           
639           /* foreground_color, family_name, face_name, desc, sample string */
640           g_string_printf (tmp, ROW_FORMAT_STRING,
641                                 color_string,
642                                 fam_name,
643                                 face_name,
644                                 font_desc,
645                                 PREVIEW_TEXT);
646
647
648           gtk_list_store_append (model, &iter);
649           gtk_list_store_set (model, &iter,
650                               FAMILY_COLUMN, families[i],
651                               FACE_COLUMN, faces[j],
652                               FAMILY_NAME_COLUMN, fam_name,
653                               TEXT_COLUMN, tmp->str,
654                               -1);
655
656           if ((i == 0 && j == 0) ||
657               (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
658             {
659               match_row = iter;
660             }
661
662           pango_font_description_free(pango_desc);
663           g_free (font_desc);
664         }
665
666       g_free (faces);
667     }
668
669   path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
670   gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
671   
672   gtk_tree_path_free(path);
673   g_string_free (tmp, TRUE);
674   g_free (color_string);
675   g_free (families);
676 }
677
678 gboolean
679 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
680 {
681   gboolean result = FALSE;
682   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
683
684   const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
685   gchar       *font_name;
686   gchar       *font_name_casefold;
687   gchar       *search_text_casefold;
688
689   gtk_tree_model_get (model, iter,
690                       FAMILY_NAME_COLUMN, &font_name,
691                       -1);
692
693   /* Covering some corner cases to speed up the result */
694   if (font_name == NULL ||
695       strlen (search_text) > strlen (font_name))
696     {
697       g_free (font_name);
698       return FALSE;
699     }  
700   if (strlen (search_text) == 0)
701     {
702       g_free (font_name);
703       return TRUE;
704     }
705   
706   font_name_casefold = g_utf8_casefold (font_name, -1);
707   search_text_casefold = g_utf8_casefold (search_text, -1);
708   
709   if (g_strrstr (font_name_casefold, search_text_casefold))
710     result = TRUE;
711
712   g_free (search_text_casefold);
713   g_free (font_name_casefold);
714   g_free (font_name);
715   return result;
716 }
717
718 static void
719 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
720 {
721   GtkTreeView       *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
722   GtkTreeViewColumn *col;
723
724   fontsel->priv->model = gtk_list_store_new (4,
725                                              PANGO_TYPE_FONT_FAMILY,
726                                              PANGO_TYPE_FONT_FACE,
727                                              G_TYPE_STRING,
728                                              G_TYPE_STRING);
729
730   fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
731                                                      NULL);
732   g_object_unref (fontsel->priv->model);
733
734   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
735                                           visible_func,
736                                           (gpointer)fontsel->priv,
737                                           NULL);
738                                           
739
740   gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
741   g_object_unref (fontsel->priv->filter);
742   
743   gtk_tree_view_set_rules_hint      (treeview, TRUE);
744   gtk_tree_view_set_headers_visible (treeview, FALSE);
745
746   col = gtk_tree_view_column_new_with_attributes ("Family",
747                                                    gtk_cell_renderer_text_new (),
748                                                    "markup", TEXT_COLUMN,
749                                                    NULL);
750   gtk_tree_view_append_column (treeview, col);
751
752   populate_list (treeview, fontsel->priv->model);
753 }
754
755
756 static void
757 gtk_font_selection_finalize (GObject *object)
758 {
759   GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
760
761   gtk_font_selection_ref_family (fontsel, NULL);
762   gtk_font_selection_ref_face (fontsel, NULL);
763
764   G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
765 }
766
767 static void
768 gtk_font_selection_screen_changed (GtkWidget *widget,
769                                   GdkScreen *previous_screen)
770 {
771   return;
772 }
773
774 static void
775 gtk_font_selection_style_updated (GtkWidget *widget)
776 {
777   GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
778
779   return;
780 }
781
782 static void
783 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
784                                PangoFontFamily  *family)
785 {
786   GtkFontSelectionPrivate *priv = fontsel->priv;
787
788   if (family)
789     family = g_object_ref (family);
790   if (priv->family)
791     g_object_unref (priv->family);
792   priv->family = family;
793 }
794
795 static void
796 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
797                              PangoFontFace    *face)
798 {
799   GtkFontSelectionPrivate *priv = fontsel->priv;
800
801   if (face)
802     face = g_object_ref (face);
803   if (priv->face)
804     g_object_unref (priv->face);
805   priv->face = face;
806 }
807
808 /*****************************************************************************
809  * These functions are the main public interface for getting/setting the font.
810  *****************************************************************************/
811
812 /**
813  * gtk_font_selection_get_family_list:
814  * @fontsel: a #GtkFontSelection
815  *
816  * This returns the #GtkTreeView that lists font families, for
817  * example, 'Sans', 'Serif', etc.
818  *
819  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
820  *
821  * Deprecated: 3.2
822  */
823 GtkWidget *
824 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
825 {
826   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
827
828   return NULL;
829 }
830
831 /**
832  * gtk_font_selection_get_face_list:
833  * @fontsel: a #GtkFontSelection
834  *
835  * This returns the #GtkTreeView which lists all styles available for
836  * the selected font. For example, 'Regular', 'Bold', etc.
837  * 
838  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
839  *
840  * Deprecated: 3.2
841  */
842 GtkWidget *
843 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
844 {
845   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
846
847   return NULL;
848 }
849
850 /**
851  * gtk_font_selection_get_size_entry:
852  * @fontsel: a #GtkFontSelection
853  *
854  * This returns the #GtkEntry used to allow the user to edit the font
855  * number manually instead of selecting it from the list of font sizes.
856  *
857  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
858  *
859  * Deprecated: 3.2
860  */
861 GtkWidget *
862 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
863 {
864   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
865
866   return NULL;
867 }
868
869 /**
870  * gtk_font_selection_get_size_list:
871  * @fontsel: a #GtkFontSelection
872  *
873  * This returns the #GtkTreeeView used to list font sizes.
874  *
875  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
876  *
877  * Deprecated: 3.2
878  */
879 GtkWidget *
880 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
881 {
882   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
883
884   return NULL;
885 }
886
887 /**
888  * gtk_font_selection_get_preview_entry:
889  * @fontsel: a #GtkFontSelection
890  *
891  * This returns the #GtkEntry used to display the font as a preview.
892  *
893  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
894  *
895  * Deprecated: 3.2
896  */
897 GtkWidget *
898 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
899 {
900   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
901
902   return NULL;
903 }
904
905 /**
906  * gtk_font_selection_get_family:
907  * @fontsel: a #GtkFontSelection
908  *
909  * Gets the #PangoFontFamily representing the selected font family.
910  *
911  * Return value: (transfer none): A #PangoFontFamily representing the
912  *     selected font family. Font families are a collection of font
913  *     faces. The returned object is owned by @fontsel and must not
914  *     be modified or freed.
915  *
916  * Since: 2.14
917  */
918 PangoFontFamily *
919 gtk_font_selection_get_family (GtkFontSelection *fontsel)
920 {
921   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
922
923   return NULL;
924 }
925
926 /**
927  * gtk_font_selection_get_face:
928  * @fontsel: a #GtkFontSelection
929  *
930  * Gets the #PangoFontFace representing the selected font group
931  * details (i.e. family, slant, weight, width, etc).
932  *
933  * Return value: (transfer none): A #PangoFontFace representing the
934  *     selected font group details. The returned object is owned by
935  *     @fontsel and must not be modified or freed.
936  *
937  * Since: 2.14
938  */
939 PangoFontFace *
940 gtk_font_selection_get_face (GtkFontSelection *fontsel)
941 {
942   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
943
944   return NULL;
945 }
946
947 /**
948  * gtk_font_selection_get_size:
949  * @fontsel: a #GtkFontSelection
950  *
951  * The selected font size.
952  *
953  * Return value: A n integer representing the selected font size,
954  *     or -1 if no font size is selected.
955  *
956  * Since: 2.14
957  **/
958 gint
959 gtk_font_selection_get_size (GtkFontSelection *fontsel)
960 {
961   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
962
963   return fontsel->priv->size;
964 }
965
966 /**
967  * gtk_font_selection_get_font_name:
968  * @fontsel: a #GtkFontSelection
969  * 
970  * Gets the currently-selected font name. 
971  *
972  * Note that this can be a different string than what you set with 
973  * gtk_font_selection_set_font_name(), as the font selection widget may 
974  * normalize font names and thus return a string with a different structure. 
975  * For example, "Helvetica Italic Bold 12" could be normalized to 
976  * "Helvetica Bold Italic 12". Use pango_font_description_equal()
977  * if you want to compare two font descriptions.
978  * 
979  * Return value: A string with the name of the current font, or %NULL if 
980  *     no font is selected. You must free this string with g_free().
981  */
982 gchar *
983 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
984 {
985   return NULL;
986 }
987
988 /* This sets the current font, then selecting the appropriate list rows. */
989
990 /**
991  * gtk_font_selection_set_font_name:
992  * @fontsel: a #GtkFontSelection
993  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
994  * 
995  * Sets the currently-selected font. 
996  *
997  * Note that the @fontsel needs to know the screen in which it will appear 
998  * for this to work; this can be guaranteed by simply making sure that the 
999  * @fontsel is inserted in a toplevel window before you call this function.
1000  * 
1001  * Return value: %TRUE if the font could be set successfully; %FALSE if no 
1002  *     such font exists or if the @fontsel doesn't belong to a particular 
1003  *     screen yet.
1004  */
1005 gboolean
1006 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1007                                   const gchar      *fontname)
1008 {
1009 #if 0
1010   PangoFontFamily *family = NULL;
1011   PangoFontFace *face = NULL;
1012   PangoFontDescription *new_desc;
1013 #endif
1014
1015   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1016
1017   return TRUE;
1018 }
1019
1020 /**
1021  * gtk_font_selection_get_preview_text:
1022  * @fontsel: a #GtkFontSelection
1023  *
1024  * Gets the text displayed in the preview area.
1025  * 
1026  * Return value: the text displayed in the preview area. 
1027  *     This string is owned by the widget and should not be 
1028  *     modified or freed 
1029  */
1030 G_CONST_RETURN gchar*
1031 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1032 {
1033   return NULL;
1034 }
1035
1036
1037 /**
1038  * gtk_font_selection_set_preview_text:
1039  * @fontsel: a #GtkFontSelection
1040  * @text: the text to display in the preview area 
1041  *
1042  * Sets the text displayed in the preview area.
1043  * The @text is used to show how the selected font looks.
1044  */
1045 void
1046 gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
1047                                       const gchar      *text)
1048 {
1049 #if 0
1050   GtkFontSelectionPrivate *priv;
1051
1052   g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1053   g_return_if_fail (text != NULL);
1054
1055   priv = fontsel->priv;
1056 #endif
1057 }
1058
1059
1060 /**
1061  * SECTION:gtkfontseldlg
1062  * @Short_description: A dialog box for selecting fonts
1063  * @Title: GtkFontSelectionDialog
1064  * @See_also: #GtkFontSelection, #GtkDialog
1065  *
1066  * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1067  *
1068  * To set the font which is initially selected, use
1069  * gtk_font_selection_dialog_set_font_name().
1070  *
1071  * To get the selected font use gtk_font_selection_dialog_get_font_name().
1072  *
1073  * To change the text which is shown in the preview area, use
1074  * gtk_font_selection_dialog_set_preview_text().
1075  *
1076  * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1077  * <title>GtkFontSelectionDialog as GtkBuildable</title>
1078  * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1079  * exposes the embedded #GtkFontSelection as internal child with the
1080  * name "font_selection". It also exposes the buttons with the names
1081  * "ok_button", "cancel_button" and "apply_button".
1082  * </refsect2>
1083  */
1084
1085 static void gtk_font_selection_dialog_buildable_interface_init     (GtkBuildableIface *iface);
1086 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1087                                                                           GtkBuilder   *builder,
1088                                                                           const gchar  *childname);
1089
1090 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1091                          GTK_TYPE_DIALOG,
1092                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1093                                                 gtk_font_selection_dialog_buildable_interface_init))
1094
1095 static GtkBuildableIface *parent_buildable_iface;
1096
1097 static void
1098 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1099 {
1100   g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1101 }
1102
1103 static void
1104 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1105 {
1106   GtkFontSelectionDialogPrivate *priv;
1107   GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1108   GtkWidget *action_area, *content_area;
1109
1110   fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1111                                                    GTK_TYPE_FONT_SELECTION_DIALOG,
1112                                                    GtkFontSelectionDialogPrivate);
1113   priv = fontseldiag->priv;
1114
1115   content_area = gtk_dialog_get_content_area (dialog);
1116   action_area = gtk_dialog_get_action_area (dialog);
1117
1118   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1119   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1120   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1121   gtk_box_set_spacing (GTK_BOX (action_area), 6);
1122
1123   gtk_widget_push_composite_child ();
1124
1125   gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1126
1127   /* Create the content area */
1128   priv->fontsel = gtk_font_selection_new ();
1129   gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1130   gtk_widget_show (priv->fontsel);
1131   gtk_box_pack_start (GTK_BOX (content_area),
1132                       priv->fontsel, TRUE, TRUE, 0);
1133
1134   /* Create the action area */
1135   priv->cancel_button = gtk_dialog_add_button (dialog,
1136                                                GTK_STOCK_CANCEL,
1137                                                GTK_RESPONSE_CANCEL);
1138
1139   priv->apply_button = gtk_dialog_add_button (dialog,
1140                                               GTK_STOCK_APPLY,
1141                                               GTK_RESPONSE_APPLY);
1142   gtk_widget_hide (priv->apply_button);
1143
1144   priv->ok_button = gtk_dialog_add_button (dialog,
1145                                            GTK_STOCK_OK,
1146                                            GTK_RESPONSE_OK);
1147   gtk_widget_grab_default (priv->ok_button);
1148
1149   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1150                                            GTK_RESPONSE_OK,
1151                                            GTK_RESPONSE_APPLY,
1152                                            GTK_RESPONSE_CANCEL,
1153                                            -1);
1154
1155   gtk_window_set_title (GTK_WINDOW (fontseldiag),
1156                         _("Font Selection"));
1157
1158   gtk_widget_pop_composite_child ();
1159 }
1160
1161 /**
1162  * gtk_font_selection_dialog_new:
1163  * @title: the title of the dialog window 
1164  *
1165  * Creates a new #GtkFontSelectionDialog.
1166  *
1167  * Return value: a new #GtkFontSelectionDialog
1168  */
1169 GtkWidget*
1170 gtk_font_selection_dialog_new (const gchar *title)
1171 {
1172   GtkFontSelectionDialog *fontseldiag;
1173   
1174   fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1175
1176   if (title)
1177     gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1178   
1179   return GTK_WIDGET (fontseldiag);
1180 }
1181
1182 /**
1183  * gtk_font_selection_dialog_get_font_selection:
1184  * @fsd: a #GtkFontSelectionDialog
1185  *
1186  * Retrieves the #GtkFontSelection widget embedded in the dialog.
1187  *
1188  * Returns: (transfer none): the embedded #GtkFontSelection
1189  *
1190  * Since: 2.22
1191  **/
1192 GtkWidget*
1193 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1194 {
1195   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1196
1197   return fsd->priv->fontsel;
1198 }
1199
1200
1201 /**
1202  * gtk_font_selection_dialog_get_ok_button:
1203  * @fsd: a #GtkFontSelectionDialog
1204  *
1205  * Gets the 'OK' button.
1206  *
1207  * Return value: (transfer none): the #GtkWidget used in the dialog
1208  *     for the 'OK' button.
1209  *
1210  * Since: 2.14
1211  */
1212 GtkWidget *
1213 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1214 {
1215   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1216
1217   return fsd->priv->ok_button;
1218 }
1219
1220 /**
1221  * gtk_font_selection_dialog_get_cancel_button:
1222  * @fsd: a #GtkFontSelectionDialog
1223  *
1224  * Gets the 'Cancel' button.
1225  *
1226  * Return value: (transfer none): the #GtkWidget used in the dialog
1227  *     for the 'Cancel' button.
1228  *
1229  * Since: 2.14
1230  */
1231 GtkWidget *
1232 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1233 {
1234   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1235
1236   return fsd->priv->cancel_button;
1237 }
1238
1239 static void
1240 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1241 {
1242   parent_buildable_iface = g_type_interface_peek_parent (iface);
1243   iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1244 }
1245
1246 static GObject *
1247 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1248                                                         GtkBuilder   *builder,
1249                                                         const gchar  *childname)
1250 {
1251   GtkFontSelectionDialogPrivate *priv;
1252
1253   priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1254
1255   if (g_strcmp0 (childname, "ok_button") == 0)
1256     return G_OBJECT (priv->ok_button);
1257   else if (g_strcmp0 (childname, "cancel_button") == 0)
1258     return G_OBJECT (priv->cancel_button);
1259   else if (g_strcmp0 (childname, "apply_button") == 0)
1260     return G_OBJECT (priv->apply_button);
1261   else if (g_strcmp0 (childname, "font_selection") == 0)
1262     return G_OBJECT (priv->fontsel);
1263
1264   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1265 }
1266
1267 /**
1268  * gtk_font_selection_dialog_get_font_name:
1269  * @fsd: a #GtkFontSelectionDialog
1270  * 
1271  * Gets the currently-selected font name.
1272  *
1273  * Note that this can be a different string than what you set with 
1274  * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1275  * may normalize font names and thus return a string with a different 
1276  * structure. For example, "Helvetica Italic Bold 12" could be normalized 
1277  * to "Helvetica Bold Italic 12".  Use pango_font_description_equal()
1278  * if you want to compare two font descriptions.
1279  * 
1280  * Return value: A string with the name of the current font, or %NULL if no 
1281  *     font is selected. You must free this string with g_free().
1282  */
1283 gchar*
1284 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1285 {
1286   GtkFontSelectionDialogPrivate *priv;
1287
1288   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1289
1290   priv = fsd->priv;
1291
1292   return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1293 }
1294
1295 /**
1296  * gtk_font_selection_dialog_set_font_name:
1297  * @fsd: a #GtkFontSelectionDialog
1298  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1299  *
1300  * Sets the currently selected font. 
1301  * 
1302  * Return value: %TRUE if the font selected in @fsd is now the
1303  *     @fontname specified, %FALSE otherwise. 
1304  */
1305 gboolean
1306 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1307                                          const gchar            *fontname)
1308 {
1309   GtkFontSelectionDialogPrivate *priv;
1310
1311   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1312   g_return_val_if_fail (fontname, FALSE);
1313
1314   priv = fsd->priv;
1315
1316   return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1317 }
1318
1319 /**
1320  * gtk_font_selection_dialog_get_preview_text:
1321  * @fsd: a #GtkFontSelectionDialog
1322  *
1323  * Gets the text displayed in the preview area.
1324  * 
1325  * Return value: the text displayed in the preview area. 
1326  *     This string is owned by the widget and should not be 
1327  *     modified or freed 
1328  */
1329 G_CONST_RETURN gchar*
1330 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1331 {
1332   GtkFontSelectionDialogPrivate *priv;
1333
1334   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1335
1336   priv = fsd->priv;
1337
1338   return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1339 }
1340
1341 /**
1342  * gtk_font_selection_dialog_set_preview_text:
1343  * @fsd: a #GtkFontSelectionDialog
1344  * @text: the text to display in the preview area
1345  *
1346  * Sets the text displayed in the preview area. 
1347  */
1348 void
1349 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1350                                             const gchar            *text)
1351 {
1352   GtkFontSelectionDialogPrivate *priv;
1353
1354   g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1355   g_return_if_fail (text != NULL);
1356
1357   priv = fsd->priv;
1358
1359   gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);
1360 }