]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
f4cc2197c827dbaaf3e3d3bac57ec8a6b2eb3ce6
[~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 14
146 static const gint font_sizes[] = {
147   6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
148 };
149
150 enum {
151    PROP_0,
152    PROP_FONT_NAME,
153    PROP_PREVIEW_TEXT
154 };
155
156
157 enum {
158   FAMILY_COLUMN,
159   FACE_COLUMN,
160   FAMILY_NAME_COLUMN,
161   TEXT_COLUMN
162 };
163
164 static void    gtk_font_selection_set_property       (GObject         *object,
165                                                       guint            prop_id,
166                                                       const GValue    *value,
167                                                       GParamSpec      *pspec);
168 static void    gtk_font_selection_get_property       (GObject         *object,
169                                                       guint            prop_id,
170                                                       GValue          *value,
171                                                       GParamSpec      *pspec);
172 static void    gtk_font_selection_finalize           (GObject         *object);
173 static void    gtk_font_selection_screen_changed     (GtkWidget       *widget,
174                                                       GdkScreen       *previous_screen);
175 static void    gtk_font_selection_style_updated      (GtkWidget      *widget);
176
177 static void     gtk_font_selection_ref_family            (GtkFontSelection *fontsel,
178                                                           PangoFontFamily  *family);
179 static void     gtk_font_selection_ref_face              (GtkFontSelection *fontsel,
180                                                           PangoFontFace    *face);
181 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
182
183 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
184
185 static void
186 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
187 {
188   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
190
191   gobject_class->finalize = gtk_font_selection_finalize;
192   gobject_class->set_property = gtk_font_selection_set_property;
193   gobject_class->get_property = gtk_font_selection_get_property;
194
195   widget_class->screen_changed = gtk_font_selection_screen_changed;
196   widget_class->style_updated = gtk_font_selection_style_updated;
197    
198   g_object_class_install_property (gobject_class,
199                                    PROP_FONT_NAME,
200                                    g_param_spec_string ("font-name",
201                                                         P_("Font name"),
202                                                         P_("The string that represents this font"),
203                                                         DEFAULT_FONT_NAME,
204                                                         GTK_PARAM_READWRITE));
205   g_object_class_install_property (gobject_class,
206                                    PROP_PREVIEW_TEXT,
207                                    g_param_spec_string ("preview-text",
208                                                         P_("Preview text"),
209                                                         P_("The text to display in order to demonstrate the selected font"),
210                                                         _(PREVIEW_TEXT),
211                                                         GTK_PARAM_READWRITE));
212
213   g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
214 }
215
216 static void 
217 gtk_font_selection_set_property (GObject         *object,
218                                  guint            prop_id,
219                                  const GValue    *value,
220                                  GParamSpec      *pspec)
221 {
222   GtkFontSelection *fontsel;
223
224   fontsel = GTK_FONT_SELECTION (object);
225
226   switch (prop_id)
227     {
228     case PROP_FONT_NAME:
229       gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
230       break;
231     case PROP_PREVIEW_TEXT:
232       gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237     }
238 }
239
240 static void gtk_font_selection_get_property (GObject         *object,
241                                              guint            prop_id,
242                                              GValue          *value,
243                                              GParamSpec      *pspec)
244 {
245   GtkFontSelection *fontsel;
246
247   fontsel = GTK_FONT_SELECTION (object);
248
249   switch (prop_id)
250     {
251     case PROP_FONT_NAME:
252       g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
253       break;
254     case PROP_PREVIEW_TEXT:
255       g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260     }
261 }
262
263 void
264 deleted_text_cb (GtkEntryBuffer *buffer,
265                  guint           position,
266                  guint           n_chars,
267                  gpointer        user_data)
268 {
269   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
270   GtkWidget               *entry = priv->search_entry;
271   
272   if (gtk_entry_buffer_get_length (buffer) == 0)
273     {
274       gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
275                                      GTK_ENTRY_ICON_SECONDARY,
276                                      GTK_STOCK_FIND);
277     }
278
279   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
280 }
281
282 void
283 inserted_text_cb (GtkEntryBuffer *buffer,
284                   guint           position,
285                   gchar          *chars,
286                   guint           n_chars,
287                   gpointer        user_data) 
288 {
289   GtkFontSelectionPrivate *priv  = (GtkFontSelectionPrivate*)user_data;
290   GtkWidget               *entry = priv->search_entry;
291
292   if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
293                  GTK_STOCK_CLEAR))
294     gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
295                                    GTK_ENTRY_ICON_SECONDARY,
296                                    GTK_STOCK_CLEAR);
297
298
299   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
300 }
301
302 void
303 icon_press_cb (GtkEntry             *entry,
304                GtkEntryIconPosition  pos,
305                GdkEvent             *event,
306                gpointer              user_data)
307 {
308   gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
309 }
310
311 void
312 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
313 {
314   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
315
316   /* If we set the silder value manually, we ignore this callback */
317   if (priv->ignore_slider)
318     {
319       priv->ignore_slider = FALSE;
320       return;
321     }
322
323   gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
324                             gtk_adjustment_get_value (adjustment));
325 }
326
327 void
328 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
329 {
330   PangoFontDescription *desc;
331   GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
332
333   gdouble size = gtk_adjustment_get_value (adjustment);
334   
335   GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
336
337   /* We ignore the slider value change callback for both of this set_value call */
338   if (size < gtk_adjustment_get_lower (slider_adj))
339     {
340       priv->ignore_slider = TRUE;
341       gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
342     }
343   else if (size > gtk_adjustment_get_upper (slider_adj))
344     {
345       priv->ignore_slider = TRUE;
346       gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
347     }
348
349   priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
350
351   desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
352   pango_font_description_set_size (desc, priv->size);
353   gtk_widget_override_font (priv->preview, desc);
354   
355   gtk_widget_queue_draw (priv->preview);
356 }
357
358 void
359 set_range_marks (GtkWidget* size_slider, gint* sizes, gint length)
360 {
361   gint i;
362   
363   gtk_scale_clear_marks (GTK_SCALE (size_slider));
364   
365   for (i=0; i<length; i++)
366     gtk_scale_add_mark (GTK_SCALE (size_slider),
367                         (gdouble) sizes[i],
368                         GTK_POS_BOTTOM, NULL);
369 }
370
371 void
372 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
373 {
374   gchar                *family_name;
375   PangoFontFamily      *family;
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   GtkFontSelection        *fontsel = (GtkFontSelection*)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 (fontsel->priv->model), &iter, path))
393     return;
394   
395   gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->model), &iter,
396                       FACE_COLUMN, &face,
397                       FAMILY_COLUMN, &family,
398                       FAMILY_NAME_COLUMN, &family_name,
399                       -1);
400   gtk_tree_path_free (path);
401   path = NULL;
402   
403   if (!face || !family_name || !family)
404     {
405       g_free (family_name);
406       g_object_unref (face);
407       g_object_unref (family);
408       return;
409     }
410
411   desc = pango_font_face_describe (face);
412   pango_font_description_set_size (desc, fontsel->priv->size);
413   gtk_widget_override_font (fontsel->priv->preview, desc);
414
415   pango_font_face_list_sizes (face, &sizes, &n_sizes);
416   /* It seems not many fonts actually have a sane set of sizes */
417   /* set_range_marks (priv->size_slider, sizes, n_sizes); */
418
419   gtk_font_selection_ref_family (fontsel, family);
420   gtk_font_selection_ref_face  (fontsel,   face);
421
422   /* Free resources */
423   g_free (family_name);
424   g_object_unref ((gpointer)face);
425   pango_font_description_free(desc);
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)fontsel);
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   GtkCellRenderer   *cell;
723   GtkTreeViewColumn *col;
724
725   fontsel->priv->model = gtk_list_store_new (4,
726                                              PANGO_TYPE_FONT_FAMILY,
727                                              PANGO_TYPE_FONT_FACE,
728                                              G_TYPE_STRING,
729                                              G_TYPE_STRING);
730
731   fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
732                                                      NULL);
733   g_object_unref (fontsel->priv->model);
734
735   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
736                                           visible_func,
737                                           (gpointer)fontsel->priv,
738                                           NULL);
739                                           
740
741   gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
742   g_object_unref (fontsel->priv->filter);
743   
744   gtk_tree_view_set_rules_hint      (treeview, TRUE);
745   gtk_tree_view_set_headers_visible (treeview, FALSE);
746
747   cell = gtk_cell_renderer_text_new ();
748   col = gtk_tree_view_column_new_with_attributes ("Family",
749                                                   cell,
750                                                   "markup", TEXT_COLUMN,
751                                                   NULL);
752                                                   
753
754   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
755   
756   gtk_tree_view_append_column (treeview, col);
757
758   populate_list (treeview, fontsel->priv->model);
759 }
760
761
762 static void
763 gtk_font_selection_finalize (GObject *object)
764 {
765   GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
766
767   gtk_font_selection_ref_family (fontsel, NULL);
768   gtk_font_selection_ref_face (fontsel, NULL);
769
770   G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
771 }
772
773 static void
774 gtk_font_selection_screen_changed (GtkWidget *widget,
775                                   GdkScreen *previous_screen)
776 {
777   return;
778 }
779
780 static void
781 gtk_font_selection_style_updated (GtkWidget *widget)
782 {
783   GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
784
785   return;
786 }
787
788 static void
789 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
790                                PangoFontFamily  *family)
791 {
792   GtkFontSelectionPrivate *priv = fontsel->priv;
793
794   if (family)
795     family = g_object_ref (family);
796   if (priv->family)
797     g_object_unref (priv->family);
798   priv->family = family;
799 }
800
801 static void
802 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
803                              PangoFontFace    *face)
804 {
805   GtkFontSelectionPrivate *priv = fontsel->priv;
806
807   if (face)
808     face = g_object_ref (face);
809   if (priv->face)
810     g_object_unref (priv->face);
811   priv->face = face;
812 }
813
814 /*****************************************************************************
815  * These functions are the main public interface for getting/setting the font.
816  *****************************************************************************/
817
818 /**
819  * gtk_font_selection_get_family_list:
820  * @fontsel: a #GtkFontSelection
821  *
822  * This returns the #GtkTreeView that lists font families, for
823  * example, 'Sans', 'Serif', etc.
824  *
825  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
826  *
827  * Deprecated: 3.2
828  */
829 GtkWidget *
830 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
831 {
832   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
833
834   return NULL;
835 }
836
837 /**
838  * gtk_font_selection_get_face_list:
839  * @fontsel: a #GtkFontSelection
840  *
841  * This returns the #GtkTreeView which lists all styles available for
842  * the selected font. For example, 'Regular', 'Bold', etc.
843  * 
844  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
845  *
846  * Deprecated: 3.2
847  */
848 GtkWidget *
849 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
850 {
851   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
852
853   return NULL;
854 }
855
856 /**
857  * gtk_font_selection_get_size_entry:
858  * @fontsel: a #GtkFontSelection
859  *
860  * This returns the #GtkEntry used to allow the user to edit the font
861  * number manually instead of selecting it from the list of font sizes.
862  *
863  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
864  *
865  * Deprecated: 3.2
866  */
867 GtkWidget *
868 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
869 {
870   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
871
872   return NULL;
873 }
874
875 /**
876  * gtk_font_selection_get_size_list:
877  * @fontsel: a #GtkFontSelection
878  *
879  * This returns the #GtkTreeeView used to list font sizes.
880  *
881  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
882  *
883  * Deprecated: 3.2
884  */
885 GtkWidget *
886 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
887 {
888   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
889
890   return NULL;
891 }
892
893 /**
894  * gtk_font_selection_get_preview_entry:
895  * @fontsel: a #GtkFontSelection
896  *
897  * This returns the #GtkEntry used to display the font as a preview.
898  *
899  * Return value: (transfer none): A #GtkWidget that is part of @fontsel
900  *
901  * Deprecated: 3.2
902  */
903 GtkWidget *
904 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
905 {
906   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
907
908   return NULL;
909 }
910
911 /**
912  * gtk_font_selection_get_family:
913  * @fontsel: a #GtkFontSelection
914  *
915  * Gets the #PangoFontFamily representing the selected font family.
916  *
917  * Return value: (transfer none): A #PangoFontFamily representing the
918  *     selected font family. Font families are a collection of font
919  *     faces. The returned object is owned by @fontsel and must not
920  *     be modified or freed.
921  *
922  * Since: 2.14
923  */
924 PangoFontFamily *
925 gtk_font_selection_get_family (GtkFontSelection *fontsel)
926 {
927   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
928
929   return NULL;
930 }
931
932 /**
933  * gtk_font_selection_get_face:
934  * @fontsel: a #GtkFontSelection
935  *
936  * Gets the #PangoFontFace representing the selected font group
937  * details (i.e. family, slant, weight, width, etc).
938  *
939  * Return value: (transfer none): A #PangoFontFace representing the
940  *     selected font group details. The returned object is owned by
941  *     @fontsel and must not be modified or freed.
942  *
943  * Since: 2.14
944  */
945 PangoFontFace *
946 gtk_font_selection_get_face (GtkFontSelection *fontsel)
947 {
948   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
949
950   return NULL;
951 }
952
953 /**
954  * gtk_font_selection_get_size:
955  * @fontsel: a #GtkFontSelection
956  *
957  * The selected font size.
958  *
959  * Return value: A n integer representing the selected font size,
960  *     or -1 if no font size is selected.
961  *
962  * Since: 2.14
963  **/
964 gint
965 gtk_font_selection_get_size (GtkFontSelection *fontsel)
966 {
967   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
968
969   return fontsel->priv->size;
970 }
971
972 /**
973  * gtk_font_selection_get_font_name:
974  * @fontsel: a #GtkFontSelection
975  * 
976  * Gets the currently-selected font name. 
977  *
978  * Note that this can be a different string than what you set with 
979  * gtk_font_selection_set_font_name(), as the font selection widget may 
980  * normalize font names and thus return a string with a different structure. 
981  * For example, "Helvetica Italic Bold 12" could be normalized to 
982  * "Helvetica Bold Italic 12". Use pango_font_description_equal()
983  * if you want to compare two font descriptions.
984  * 
985  * Return value: A string with the name of the current font, or %NULL if 
986  *     no font is selected. You must free this string with g_free().
987  */
988 gchar *
989 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
990 {
991   return NULL;
992 }
993
994 /* This sets the current font, then selecting the appropriate list rows. */
995
996 /**
997  * gtk_font_selection_set_font_name:
998  * @fontsel: a #GtkFontSelection
999  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1000  * 
1001  * Sets the currently-selected font. 
1002  *
1003  * Note that the @fontsel needs to know the screen in which it will appear 
1004  * for this to work; this can be guaranteed by simply making sure that the 
1005  * @fontsel is inserted in a toplevel window before you call this function.
1006  * 
1007  * Return value: %TRUE if the font could be set successfully; %FALSE if no 
1008  *     such font exists or if the @fontsel doesn't belong to a particular 
1009  *     screen yet.
1010  */
1011 gboolean
1012 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1013                                   const gchar      *fontname)
1014 {
1015 #if 0
1016   PangoFontFamily *family = NULL;
1017   PangoFontFace *face = NULL;
1018   PangoFontDescription *new_desc;
1019 #endif
1020
1021   g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1022
1023   return TRUE;
1024 }
1025
1026 /**
1027  * gtk_font_selection_get_preview_text:
1028  * @fontsel: a #GtkFontSelection
1029  *
1030  * Gets the text displayed in the preview area.
1031  * 
1032  * Return value: the text displayed in the preview area. 
1033  *     This string is owned by the widget and should not be 
1034  *     modified or freed 
1035  */
1036 G_CONST_RETURN gchar*
1037 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1038 {
1039   return NULL;
1040 }
1041
1042
1043 /**
1044  * gtk_font_selection_set_preview_text:
1045  * @fontsel: a #GtkFontSelection
1046  * @text: the text to display in the preview area 
1047  *
1048  * Sets the text displayed in the preview area.
1049  * The @text is used to show how the selected font looks.
1050  */
1051 void
1052 gtk_font_selection_set_preview_text  (GtkFontSelection *fontsel,
1053                                       const gchar      *text)
1054 {
1055 #if 0
1056   GtkFontSelectionPrivate *priv;
1057
1058   g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1059   g_return_if_fail (text != NULL);
1060
1061   priv = fontsel->priv;
1062 #endif
1063 }
1064
1065
1066 /**
1067  * SECTION:gtkfontseldlg
1068  * @Short_description: A dialog box for selecting fonts
1069  * @Title: GtkFontSelectionDialog
1070  * @See_also: #GtkFontSelection, #GtkDialog
1071  *
1072  * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1073  *
1074  * To set the font which is initially selected, use
1075  * gtk_font_selection_dialog_set_font_name().
1076  *
1077  * To get the selected font use gtk_font_selection_dialog_get_font_name().
1078  *
1079  * To change the text which is shown in the preview area, use
1080  * gtk_font_selection_dialog_set_preview_text().
1081  *
1082  * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1083  * <title>GtkFontSelectionDialog as GtkBuildable</title>
1084  * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1085  * exposes the embedded #GtkFontSelection as internal child with the
1086  * name "font_selection". It also exposes the buttons with the names
1087  * "ok_button", "cancel_button" and "apply_button".
1088  * </refsect2>
1089  */
1090
1091 static void gtk_font_selection_dialog_buildable_interface_init     (GtkBuildableIface *iface);
1092 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1093                                                                           GtkBuilder   *builder,
1094                                                                           const gchar  *childname);
1095
1096 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1097                          GTK_TYPE_DIALOG,
1098                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1099                                                 gtk_font_selection_dialog_buildable_interface_init))
1100
1101 static GtkBuildableIface *parent_buildable_iface;
1102
1103 static void
1104 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1105 {
1106   g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1107 }
1108
1109 static void
1110 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1111 {
1112   GtkFontSelectionDialogPrivate *priv;
1113   GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1114   GtkWidget *action_area, *content_area;
1115
1116   fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1117                                                    GTK_TYPE_FONT_SELECTION_DIALOG,
1118                                                    GtkFontSelectionDialogPrivate);
1119   priv = fontseldiag->priv;
1120
1121   content_area = gtk_dialog_get_content_area (dialog);
1122   action_area = gtk_dialog_get_action_area (dialog);
1123
1124   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1125   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1126   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1127   gtk_box_set_spacing (GTK_BOX (action_area), 6);
1128
1129   gtk_widget_push_composite_child ();
1130
1131   gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1132
1133   /* Create the content area */
1134   priv->fontsel = gtk_font_selection_new ();
1135   gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1136   gtk_widget_show (priv->fontsel);
1137   gtk_box_pack_start (GTK_BOX (content_area),
1138                       priv->fontsel, TRUE, TRUE, 0);
1139
1140   /* Create the action area */
1141   priv->cancel_button = gtk_dialog_add_button (dialog,
1142                                                GTK_STOCK_CANCEL,
1143                                                GTK_RESPONSE_CANCEL);
1144
1145   priv->apply_button = gtk_dialog_add_button (dialog,
1146                                               GTK_STOCK_APPLY,
1147                                               GTK_RESPONSE_APPLY);
1148   gtk_widget_hide (priv->apply_button);
1149
1150   priv->ok_button = gtk_dialog_add_button (dialog,
1151                                            GTK_STOCK_OK,
1152                                            GTK_RESPONSE_OK);
1153   gtk_widget_grab_default (priv->ok_button);
1154
1155   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1156                                            GTK_RESPONSE_OK,
1157                                            GTK_RESPONSE_APPLY,
1158                                            GTK_RESPONSE_CANCEL,
1159                                            -1);
1160
1161   gtk_window_set_title (GTK_WINDOW (fontseldiag),
1162                         _("Font Selection"));
1163
1164   gtk_widget_pop_composite_child ();
1165 }
1166
1167 /**
1168  * gtk_font_selection_dialog_new:
1169  * @title: the title of the dialog window 
1170  *
1171  * Creates a new #GtkFontSelectionDialog.
1172  *
1173  * Return value: a new #GtkFontSelectionDialog
1174  */
1175 GtkWidget*
1176 gtk_font_selection_dialog_new (const gchar *title)
1177 {
1178   GtkFontSelectionDialog *fontseldiag;
1179   
1180   fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1181
1182   if (title)
1183     gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1184   
1185   return GTK_WIDGET (fontseldiag);
1186 }
1187
1188 /**
1189  * gtk_font_selection_dialog_get_font_selection:
1190  * @fsd: a #GtkFontSelectionDialog
1191  *
1192  * Retrieves the #GtkFontSelection widget embedded in the dialog.
1193  *
1194  * Returns: (transfer none): the embedded #GtkFontSelection
1195  *
1196  * Since: 2.22
1197  **/
1198 GtkWidget*
1199 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1200 {
1201   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1202
1203   return fsd->priv->fontsel;
1204 }
1205
1206
1207 /**
1208  * gtk_font_selection_dialog_get_ok_button:
1209  * @fsd: a #GtkFontSelectionDialog
1210  *
1211  * Gets the 'OK' button.
1212  *
1213  * Return value: (transfer none): the #GtkWidget used in the dialog
1214  *     for the 'OK' button.
1215  *
1216  * Since: 2.14
1217  */
1218 GtkWidget *
1219 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1220 {
1221   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1222
1223   return fsd->priv->ok_button;
1224 }
1225
1226 /**
1227  * gtk_font_selection_dialog_get_cancel_button:
1228  * @fsd: a #GtkFontSelectionDialog
1229  *
1230  * Gets the 'Cancel' button.
1231  *
1232  * Return value: (transfer none): the #GtkWidget used in the dialog
1233  *     for the 'Cancel' button.
1234  *
1235  * Since: 2.14
1236  */
1237 GtkWidget *
1238 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1239 {
1240   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1241
1242   return fsd->priv->cancel_button;
1243 }
1244
1245 static void
1246 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1247 {
1248   parent_buildable_iface = g_type_interface_peek_parent (iface);
1249   iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1250 }
1251
1252 static GObject *
1253 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1254                                                         GtkBuilder   *builder,
1255                                                         const gchar  *childname)
1256 {
1257   GtkFontSelectionDialogPrivate *priv;
1258
1259   priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1260
1261   if (g_strcmp0 (childname, "ok_button") == 0)
1262     return G_OBJECT (priv->ok_button);
1263   else if (g_strcmp0 (childname, "cancel_button") == 0)
1264     return G_OBJECT (priv->cancel_button);
1265   else if (g_strcmp0 (childname, "apply_button") == 0)
1266     return G_OBJECT (priv->apply_button);
1267   else if (g_strcmp0 (childname, "font_selection") == 0)
1268     return G_OBJECT (priv->fontsel);
1269
1270   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1271 }
1272
1273 /**
1274  * gtk_font_selection_dialog_get_font_name:
1275  * @fsd: a #GtkFontSelectionDialog
1276  * 
1277  * Gets the currently-selected font name.
1278  *
1279  * Note that this can be a different string than what you set with 
1280  * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1281  * may normalize font names and thus return a string with a different 
1282  * structure. For example, "Helvetica Italic Bold 12" could be normalized 
1283  * to "Helvetica Bold Italic 12".  Use pango_font_description_equal()
1284  * if you want to compare two font descriptions.
1285  * 
1286  * Return value: A string with the name of the current font, or %NULL if no 
1287  *     font is selected. You must free this string with g_free().
1288  */
1289 gchar*
1290 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1291 {
1292   GtkFontSelectionDialogPrivate *priv;
1293
1294   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1295
1296   priv = fsd->priv;
1297
1298   return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1299 }
1300
1301 /**
1302  * gtk_font_selection_dialog_set_font_name:
1303  * @fsd: a #GtkFontSelectionDialog
1304  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1305  *
1306  * Sets the currently selected font. 
1307  * 
1308  * Return value: %TRUE if the font selected in @fsd is now the
1309  *     @fontname specified, %FALSE otherwise. 
1310  */
1311 gboolean
1312 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1313                                          const gchar            *fontname)
1314 {
1315   GtkFontSelectionDialogPrivate *priv;
1316
1317   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1318   g_return_val_if_fail (fontname, FALSE);
1319
1320   priv = fsd->priv;
1321
1322   return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1323 }
1324
1325 /**
1326  * gtk_font_selection_dialog_get_preview_text:
1327  * @fsd: a #GtkFontSelectionDialog
1328  *
1329  * Gets the text displayed in the preview area.
1330  * 
1331  * Return value: the text displayed in the preview area. 
1332  *     This string is owned by the widget and should not be 
1333  *     modified or freed 
1334  */
1335 G_CONST_RETURN gchar*
1336 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1337 {
1338   GtkFontSelectionDialogPrivate *priv;
1339
1340   g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1341
1342   priv = fsd->priv;
1343
1344   return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1345 }
1346
1347 /**
1348  * gtk_font_selection_dialog_set_preview_text:
1349  * @fsd: a #GtkFontSelectionDialog
1350  * @text: the text to display in the preview area
1351  *
1352  * Sets the text displayed in the preview area. 
1353  */
1354 void
1355 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1356                                             const gchar            *text)
1357 {
1358   GtkFontSelectionDialogPrivate *priv;
1359
1360   g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1361   g_return_if_fail (text != NULL);
1362
1363   priv = fsd->priv;
1364
1365   gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);
1366 }