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