]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
Make setting fontname work, independent of a screen
[~andy/gtk] / gtk / gtkfontchooser.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <glib/gprintf.h>
24 #include <string.h>
25
26 #include <atk/atk.h>
27
28 #include "gtkfontchooser.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkentry.h"
31 #include "gtkframe.h"
32 #include "gtkhbbox.h"
33 #include "gtkhbox.h"
34 #include "gtklabel.h"
35 #include "gtkliststore.h"
36 #include "gtkstock.h"
37 #include "gtktextview.h"
38 #include "gtktreeselection.h"
39 #include "gtktreeview.h"
40 #include "gtkbox.h"
41 #include "gtkscrolledwindow.h"
42 #include "gtkintl.h"
43 #include "gtkaccessible.h"
44 #include "gtkbuildable.h"
45 #include "gtkprivate.h"
46 #include "gtkalignment.h"
47 #include "gtkscale.h"
48 #include "gtkbox.h"
49 #include "gtkspinbutton.h"
50 #include "gtknotebook.h"
51 #include "gtkwidget.h"
52 #include "gtkgrid.h"
53
54 /**
55  * SECTION:gtkfontchooser
56  * @Short_description: A widget for selecting fonts
57  * @Title: GtkFontChooser
58  * @See_also: #GtkFontChooserDialog
59  *
60  * The #GtkFontChooser widget lists the available fonts, styles and sizes,
61  * allowing the user to select a font.
62  * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
63  * selecting fonts.
64  *
65  * To set the font which is initially selected, use
66  * gtk_font_chooser_set_font_name().
67  *
68  * To get the selected font use gtk_font_chooser_get_font_name().
69  *
70  * To change the text which is shown in the preview area, use
71  * gtk_font_chooser_set_preview_text().
72  *
73  * Since: 3.2
74  */
75
76
77 struct _GtkFontChooserPrivate
78 {
79   GtkWidget    *search_entry;
80   GtkWidget    *family_face_list;
81   GtkWidget    *list_scrolled_window;
82   GtkWidget    *empty_list;
83   GtkWidget    *list_notebook;
84   GtkListStore *model;
85   GtkTreeModel *filter_model;
86
87   GtkWidget       *preview;
88   gchar           *preview_text;
89   gboolean         show_preview_entry;
90
91   GtkWidget *size_spin;
92   GtkWidget *size_slider;
93
94   gchar           *fontname;
95   gint             size;
96   PangoFontFace   *face;
97   PangoFontFamily *family;
98
99   gulong           cursor_changed_handler;
100
101   GtkFontFilterFunc filter_func;
102   gpointer          filter_data;
103   GDestroyNotify    filter_data_destroy;
104 };
105
106 #define DEFAULT_FONT_NAME "Sans 10"
107 #define MAX_FONT_SIZE 999
108
109 /* This is the initial fixed height and the top padding of the preview entry */
110 #define PREVIEW_HEIGHT 72
111 #define PREVIEW_TOP_PADDING 6
112
113 /* These are the sizes of the font, style & size lists. */
114 #define FONT_LIST_HEIGHT  136
115 #define FONT_LIST_WIDTH   190
116 #define FONT_STYLE_LIST_WIDTH 170
117 #define FONT_SIZE_LIST_WIDTH  60
118
119 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
120
121 #define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
122
123 /* These are what we use as the standard font sizes, for the size list.
124  */
125 static const gint font_sizes[] = {
126   6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
127 };
128
129 enum {
130    PROP_0,
131    PROP_FONT_NAME,
132    PROP_PREVIEW_TEXT,
133    PROP_SHOW_PREVIEW_ENTRY
134 };
135
136
137 enum {
138   FAMILY_COLUMN,
139   FACE_COLUMN,
140   PREVIEW_TEXT_COLUMN,
141   PREVIEW_TITLE_COLUMN
142 };
143
144 static void  gtk_font_chooser_set_property       (GObject         *object,
145                                                   guint            prop_id,
146                                                   const GValue    *value,
147                                                   GParamSpec      *pspec);
148 static void  gtk_font_chooser_get_property       (GObject         *object,
149                                                   guint            prop_id,
150                                                   GValue          *value,
151                                                   GParamSpec      *pspec);
152 static void  gtk_font_chooser_finalize           (GObject         *object);
153 static void  gtk_font_chooser_dispose            (GObject         *object);
154
155 static void  gtk_font_chooser_screen_changed     (GtkWidget       *widget,
156                                                   GdkScreen       *previous_screen);
157 static void  gtk_font_chooser_style_updated      (GtkWidget       *widget);
158
159 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
160
161 static gboolean gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser);
162
163 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
164
165 static void
166 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
167 {
168   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
170
171   widget_class->screen_changed = gtk_font_chooser_screen_changed;
172   widget_class->style_updated = gtk_font_chooser_style_updated;
173
174   gobject_class->dispose = gtk_font_chooser_dispose;
175   gobject_class->finalize = gtk_font_chooser_finalize;
176   gobject_class->set_property = gtk_font_chooser_set_property;
177   gobject_class->get_property = gtk_font_chooser_get_property;
178
179   g_object_class_install_property (gobject_class,
180                                    PROP_FONT_NAME,
181                                    g_param_spec_string ("font-name",
182                                                         P_("Font name"),
183                                                         P_("The string that represents this font"),
184                                                         DEFAULT_FONT_NAME,
185                                                         GTK_PARAM_READWRITE));
186   g_object_class_install_property (gobject_class,
187                                    PROP_PREVIEW_TEXT,
188                                    g_param_spec_string ("preview-text",
189                                                         P_("Preview text"),
190                                                         P_("The text to display in order to demonstrate the selected font"),
191                                                         pango_language_get_sample_string (NULL),
192                                                         GTK_PARAM_READWRITE));
193
194   g_object_class_install_property (gobject_class,
195                                    PROP_SHOW_PREVIEW_ENTRY,
196                                    g_param_spec_boolean ("show-preview-entry",
197                                                         P_("Show preview text entry"),
198                                                         P_("Whether the preview text entry is shown or not"),
199                                                         TRUE,
200                                                         GTK_PARAM_READWRITE));
201
202   g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
203 }
204
205 static void
206 gtk_font_chooser_set_property (GObject         *object,
207                                guint            prop_id,
208                                const GValue    *value,
209                                GParamSpec      *pspec)
210 {
211   GtkFontChooser *fontchooser;
212
213   fontchooser = GTK_FONT_CHOOSER (object);
214
215   switch (prop_id)
216     {
217     case PROP_FONT_NAME:
218       gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
219       break;
220     case PROP_PREVIEW_TEXT:
221       gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
222       break;
223     case PROP_SHOW_PREVIEW_ENTRY:
224       gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
225       break;
226     default:
227       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228       break;
229     }
230 }
231
232 static void
233 gtk_font_chooser_get_property (GObject         *object,
234                                guint            prop_id,
235                                GValue          *value,
236                                GParamSpec      *pspec)
237 {
238   GtkFontChooser *fontchooser;
239
240   fontchooser = GTK_FONT_CHOOSER (object);
241
242   switch (prop_id)
243     {
244     case PROP_FONT_NAME:
245       g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
246       break;
247     case PROP_PREVIEW_TEXT:
248       g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
249       break;
250     case PROP_SHOW_PREVIEW_ENTRY:
251       g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256     }
257 }
258
259 static void
260 text_changed_cb (GtkEntry       *entry,
261                  GParamSpec     *pspec,
262                  GtkFontChooser *fc)
263 {
264   GtkFontChooserPrivate *priv = fc->priv;
265   const gchar *text;
266
267   text = gtk_entry_get_text (entry);
268
269   if (text == NULL || text[0] == '\0')
270     {
271       GIcon *icon;
272
273       icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
274       g_object_set (G_OBJECT (priv->search_entry),
275                     "secondary-icon-gicon", icon,
276                     "secondary-icon-activatable", FALSE,
277                     "secondary-icon-sensitive", FALSE,
278                     NULL);
279       g_object_unref (icon);
280     }
281   else
282     {
283       if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
284         {
285           GIcon *icon;
286
287           icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
288           g_object_set (G_OBJECT (priv->search_entry),
289                         "secondary-icon-gicon", icon,
290                         "secondary-icon-activatable", TRUE,
291                         "secondary-icon-sensitive", TRUE,
292                         NULL);
293           g_object_unref (icon);
294         }
295     }
296
297   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
298 }
299
300 static void
301 icon_press_cb (GtkEntry             *entry,
302                GtkEntryIconPosition  pos,
303                GdkEvent             *event,
304                gpointer              user_data)
305 {
306   gtk_entry_set_text (entry, "");
307 }
308
309 static void
310 slider_change_cb (GtkAdjustment *adjustment,
311                   gpointer       user_data)
312 {
313   GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
314   GtkFontChooserPrivate *priv  = fc->priv;
315   GtkAdjustment         *spin_adj     = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
316   gdouble                slider_value = gtk_adjustment_get_value (adjustment);
317   gdouble                spin_value   = gtk_adjustment_get_value (spin_adj);
318
319   if (slider_value != spin_value)
320     gtk_adjustment_set_value (spin_adj,
321                               gtk_adjustment_get_value (adjustment));
322 }
323
324 static void
325 spin_change_cb (GtkAdjustment *adjustment,
326                 gpointer       user_data)
327 {
328   PangoFontDescription    *desc;
329   GtkFontChooser          *fontchooser = (GtkFontChooser*)user_data;
330   GtkFontChooserPrivate   *priv        = fontchooser->priv;
331   GtkAdjustment           *slider_adj  = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
332
333   gdouble size = gtk_adjustment_get_value (adjustment);
334   priv->size = ((gint)size) * PANGO_SCALE;
335
336   desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
337   pango_font_description_set_size (desc, priv->size);
338   gtk_widget_override_font (priv->preview, desc);
339
340   g_object_notify (G_OBJECT (fontchooser), "font-name");
341
342   /* If the new value is lower than the lower bound of the slider, we set
343    * the slider adjustment to the lower bound value if it is not already set
344    */
345   if (size < gtk_adjustment_get_lower (slider_adj) &&
346       gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
347     gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
348
349   /* If the new value is upper than the upper bound of the slider, we set
350    * the slider adjustment to the upper bound value if it is not already set
351    */
352   else if (size > gtk_adjustment_get_upper (slider_adj) &&
353            gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
354     gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
355
356   /* If the new value is not already set on the slider we set it */
357   else if (size != gtk_adjustment_get_value (slider_adj))
358     gtk_adjustment_set_value (slider_adj, size);
359
360   gtk_widget_queue_draw (priv->preview);
361 }
362
363 static void
364 set_range_marks (GtkFontChooserPrivate *priv,
365                  GtkWidget             *size_slider,
366                  gint                  *sizes,
367                  gint                   length)
368 {
369   GtkAdjustment *adj;
370   gint i;
371   gdouble value;
372
373   if (length < 2)
374     {
375       sizes = (gint*)font_sizes;
376       length = G_N_ELEMENTS (font_sizes);
377     }
378
379   gtk_scale_clear_marks (GTK_SCALE (size_slider));
380
381   adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
382
383   gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
384   gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
385
386   value = gtk_adjustment_get_value (adj);
387   if (value > (gdouble) sizes[length-1])
388     gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
389   else if (value < (gdouble) sizes[0])
390     gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
391
392   for (i = 0; i < length; i++)
393     gtk_scale_add_mark (GTK_SCALE (size_slider),
394                         (gdouble) sizes[i],
395                         GTK_POS_BOTTOM, NULL);
396 }
397
398 static void
399 cursor_changed_cb (GtkTreeView *treeview,
400                    gpointer     user_data)
401 {
402   GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
403   GtkFontChooserPrivate *priv = fontchooser->priv;
404
405   PangoFontFamily      *family;
406   PangoFontFace        *face;
407   PangoFontDescription *desc;
408
409   gint *sizes;
410   gint  i, n_sizes;
411
412   GtkTreeIter  iter;
413   GtkTreePath *path = gtk_tree_path_new ();
414
415   gtk_tree_view_get_cursor (treeview, &path, NULL);
416
417   if (!path)
418     return;
419
420   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
421     {
422       gtk_tree_path_free (path);
423       return;
424     }
425
426
427   gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
428                       FACE_COLUMN, &face,
429                       FAMILY_COLUMN, &family,
430                       -1);
431
432   gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
433
434   gtk_tree_path_free (path);
435   path = NULL;
436
437   if (face == NULL || family == NULL)
438     {
439       if (face)
440         g_object_unref (face);
441       if (family)
442         g_object_unref (family);
443
444       return;
445     }
446
447   desc = pango_font_face_describe (face);
448   pango_font_description_set_size (desc, priv->size);
449   gtk_widget_override_font (priv->preview, desc);
450
451   pango_font_face_list_sizes (face, &sizes, &n_sizes);
452   /* It seems not many fonts actually have a sane set of sizes */
453   for (i = 0; i < n_sizes; i++)
454     sizes[i] = sizes[i] / PANGO_SCALE;
455
456   set_range_marks (priv, priv->size_slider, sizes, n_sizes);
457
458   if (priv->family)
459     g_object_unref (priv->family);
460   priv->family = family;
461
462   if (priv->face)
463     g_object_unref (priv->face);
464   priv->face = face;
465
466   pango_font_description_free (desc);
467
468   g_object_notify (G_OBJECT (fontchooser), "font-name");
469 }
470
471 static gboolean
472 zoom_preview_cb (GtkWidget      *scrolled_window,
473                  GdkEventScroll *event,
474                  gpointer        user_data)
475 {
476   GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
477   GtkFontChooserPrivate *priv  = fc->priv;
478
479   GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
480
481   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
482     gtk_adjustment_set_value (adj,
483                               gtk_adjustment_get_value (adj) +
484                               gtk_adjustment_get_step_increment (adj));
485   else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
486     gtk_adjustment_set_value (adj,
487                               gtk_adjustment_get_value (adj) -
488                               gtk_adjustment_get_step_increment (adj));
489   return TRUE;
490 }
491
492 static void
493 row_inserted_cb (GtkTreeModel *model,
494                  GtkTreePath  *path,
495                  GtkTreeIter  *iter,
496                  gpointer      user_data)
497 {
498   GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
499   GtkFontChooserPrivate *priv        = fontchooser->priv;
500
501   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
502 }
503
504 static void
505 row_deleted_cb  (GtkTreeModel *model,
506                  GtkTreePath  *path,
507                  gpointer      user_data)
508 {
509   GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
510   GtkFontChooserPrivate *priv        = fontchooser->priv;
511
512   if (gtk_tree_model_iter_n_children (model, NULL) == 0)
513     gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
514 }
515
516 static void
517 gtk_font_chooser_init (GtkFontChooser *fontchooser)
518 {
519   GIcon                   *icon;
520   GtkFontChooserPrivate   *priv;
521   PangoFontDescription    *font_desc;
522   GtkWidget               *scrolled_win;
523   GtkWidget               *grid;
524
525   fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
526                                                    GTK_TYPE_FONT_CHOOSER,
527                                                    GtkFontChooserPrivate);
528
529   priv = fontchooser->priv;
530
531   /* Default preview string  */
532   priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
533   priv->show_preview_entry = TRUE;
534
535   /* Getting the default size */
536   font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
537   priv->size = pango_font_description_get_size (font_desc);
538   priv->face = NULL;
539   priv->family = NULL;
540
541   gtk_widget_push_composite_child ();
542
543   /* Creating fundamental widgets for the private struct */
544   priv->search_entry = gtk_entry_new ();
545   priv->family_face_list = gtk_tree_view_new ();
546   priv->preview = gtk_entry_new ();
547   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
548                                                 (gdouble) font_sizes[0],
549                                                 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
550                                                 1.0);
551
552   priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
553
554   /** Bootstrapping widget layout **/
555   gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
556
557   /* Main font family/face view */
558   priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
559   scrolled_win = priv->list_scrolled_window;
560   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
561                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
562   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
563                                        GTK_SHADOW_ETCHED_IN);
564   gtk_widget_set_size_request (scrolled_win, 400, 300);
565   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
566
567   /* Text to display when list is empty */
568   priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
569   gtk_widget_set_margin_top    (priv->empty_list, 12);
570   gtk_widget_set_margin_left   (priv->empty_list, 12);
571   gtk_widget_set_margin_right  (priv->empty_list, 12);
572   gtk_widget_set_margin_bottom (priv->empty_list, 12);
573   gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
574   gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
575
576   priv->list_notebook = gtk_notebook_new ();
577   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
578   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
579   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
580
581   /* Basic layout */
582   grid = gtk_grid_new ();
583
584   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
585   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
586
587   gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
588   gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
589   gtk_grid_attach (GTK_GRID (grid), priv->preview,      0, 2, 2, 1);
590
591   gtk_grid_attach (GTK_GRID (grid), priv->size_slider,  0, 3, 1, 1);
592   gtk_grid_attach (GTK_GRID (grid), priv->size_spin,    1, 3, 1, 1);
593
594   gtk_widget_set_hexpand  (GTK_WIDGET (scrolled_win),      TRUE);
595   gtk_widget_set_vexpand  (GTK_WIDGET (scrolled_win),      TRUE);
596   gtk_widget_set_hexpand  (GTK_WIDGET (priv->search_entry), TRUE);
597
598   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_slider), TRUE);
599   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_spin),   FALSE);
600
601   gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
602
603   /* Setting the adjustment values for the size slider */
604   gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
605                             (gdouble)(priv->size / PANGO_SCALE));
606   gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
607                             (gdouble)(priv->size / PANGO_SCALE));
608
609   gtk_widget_show_all (GTK_WIDGET (fontchooser));
610   gtk_widget_hide     (GTK_WIDGET (fontchooser));
611
612   /* Treeview column and model bootstrapping */
613   gtk_font_chooser_bootstrap_fontlist (fontchooser);
614
615   /* Set default preview text */
616   gtk_entry_set_text (GTK_ENTRY (priv->preview),
617                       pango_language_get_sample_string (NULL));
618
619   /* Set search icon and place holder text */
620   icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
621   g_object_set (G_OBJECT (priv->search_entry),
622                 "secondary-icon-gicon", icon,
623                 "secondary-icon-activatable", FALSE,
624                 "secondary-icon-sensitive", FALSE,
625                 NULL);
626   g_object_unref (icon);
627
628   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
629
630   /** Callback connections **/
631   g_signal_connect (priv->search_entry, "notify::text",
632                     G_CALLBACK (text_changed_cb), fontchooser);
633   g_signal_connect (priv->search_entry,
634                     "icon-press", G_CALLBACK (icon_press_cb), NULL);
635
636   g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
637                     "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
638   g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
639                     "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
640
641   priv->cursor_changed_handler =
642       g_signal_connect (priv->family_face_list, "cursor-changed",
643                         G_CALLBACK (cursor_changed_cb), fontchooser);
644
645   /* Zoom on preview scroll*/
646   g_signal_connect (priv->preview, "scroll-event",
647                     G_CALLBACK (zoom_preview_cb), fontchooser);
648
649   g_signal_connect (priv->size_slider, "scroll-event",
650                     G_CALLBACK (zoom_preview_cb), fontchooser);
651
652   set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
653
654   /* Font list empty hides the scrolledwindow */
655   g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
656                     G_CALLBACK (row_deleted_cb), fontchooser);
657   g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
658                     G_CALLBACK (row_inserted_cb), fontchooser);
659
660   /* Set default focus */
661   gtk_widget_pop_composite_child ();
662 }
663
664 /**
665  * gtk_font_chooser_new:
666  *
667  * Creates a new #GtkFontChooser.
668  *
669  * Return value: a new #GtkFontChooser
670  *
671  * Since: 3.2
672  */
673 GtkWidget *
674 gtk_font_chooser_new (void)
675 {
676   GtkFontChooser *fontchooser;
677
678   fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
679
680   return GTK_WIDGET (fontchooser);
681 }
682
683 static int
684 cmp_families (const void *a,
685               const void *b)
686 {
687   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
688   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
689
690   return g_utf8_collate (a_name, b_name);
691 }
692
693 static void
694 populate_list (GtkFontChooser *fontchooser,
695                GtkTreeView    *treeview,
696                GtkListStore   *model)
697 {
698   GtkFontChooserPrivate *priv = fontchooser->priv;
699   GtkStyleContext *style_context;
700   PangoFontDescription *default_font;
701   PangoFontDescription *selected_font;
702
703   gint match;
704   GtkTreeIter match_row;
705   GtkTreePath *path;
706
707   gint n_families, i;
708   PangoFontFamily **families;
709   GString     *tmp;
710   GString     *family_and_face;
711
712   if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
713     return;
714
715   tmp = g_string_new (NULL);
716   family_and_face = g_string_new (NULL);
717
718   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
719                                &families,
720                                &n_families);
721
722   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
723
724   style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
725   default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
726                                                                      GTK_STATE_NORMAL);
727
728   if (priv->face)
729     selected_font = pango_font_face_describe (priv->face);
730   else
731     selected_font = NULL;
732
733   gtk_list_store_clear (model);
734
735   match = 0;
736
737   /* Iterate over families and faces */
738   for (i = 0; i < n_families; i++)
739     {
740       GtkTreeIter     iter;
741       PangoFontFace **faces;
742       int             j, n_faces;
743       const gchar    *fam_name = pango_font_family_get_name (families[i]);
744
745       pango_font_family_list_faces (families[i], &faces, &n_faces);
746
747       for (j = 0; j < n_faces; j++)
748         {
749           PangoFontDescription *pango_desc;
750           const gchar *face_name;
751           gchar *font_desc;
752
753           if (priv->filter_func != NULL &&
754               !priv->filter_func (families[i], faces[j], priv->filter_data))
755             continue;
756
757           pango_desc = pango_font_face_describe (faces[j]);
758           face_name = pango_font_face_get_face_name (faces[j]);
759           font_desc = pango_font_description_to_string (pango_desc);
760
761           g_string_printf (family_and_face, "%s %s", fam_name, face_name);
762           g_string_printf (tmp, ROW_FORMAT_STRING,
763                            family_and_face->str,
764                            font_desc,
765                            fontchooser->priv->preview_text);
766
767           gtk_list_store_append (model, &iter);
768           gtk_list_store_set (model, &iter,
769                               FAMILY_COLUMN, families[i],
770                               FACE_COLUMN, faces[j],
771                               PREVIEW_TITLE_COLUMN, family_and_face->str,
772                               PREVIEW_TEXT_COLUMN, tmp->str,
773                               -1);
774
775           /* Select the current font,
776            * the default font/face from the theme,
777            * or the first font
778            */
779           if (match < 3 &&
780               selected_font != NULL &&
781               pango_font_description_equal (selected_font, pango_desc))
782             {
783               match_row = iter;
784               match = 3;
785             }
786           if (match < 2 &&
787               strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
788             {
789               match_row = iter;
790               match = 2;
791             }
792           if (match < 1)
793             {
794               match_row = iter;
795               match = 1;
796             }
797
798           pango_font_description_free (pango_desc);
799           g_free (font_desc);
800         }
801
802       g_free (faces);
803     }
804
805   path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
806   if (path)
807     {
808       gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
809       gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
810       gtk_tree_path_free (path);
811     }
812
813   if (selected_font)
814     pango_font_description_free (selected_font);
815
816   g_string_free (family_and_face, TRUE);
817   g_string_free (tmp, TRUE);
818   g_free (families);
819 }
820
821 static gboolean
822 visible_func (GtkTreeModel *model,
823               GtkTreeIter  *iter,
824               gpointer      user_data)
825 {
826   gboolean result = TRUE;
827   GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
828
829   const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
830   gchar       *font_name;
831   gchar       *term;
832   gchar      **split_terms;
833   gint         n_terms = 0;
834
835   /* If there's no filter string we show the item */
836   if (strlen (search_text) == 0)
837     return TRUE;
838
839   gtk_tree_model_get (model, iter,
840                       PREVIEW_TITLE_COLUMN, &font_name,
841                       -1);
842
843   if (font_name == NULL)
844     return FALSE;
845
846   split_terms = g_strsplit (search_text, " ", 0);
847   term = split_terms[0];
848
849   while (term && result)
850   {
851     gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
852     gchar* term_casefold = g_utf8_casefold (term, -1);
853
854     if (g_strrstr (font_name_casefold, term_casefold))
855       result = result && TRUE;
856     else
857       result = FALSE;
858
859     n_terms++;
860     term = split_terms[n_terms];
861
862     g_free (term_casefold);
863     g_free (font_name_casefold);
864   }
865
866   g_free (font_name);
867   g_strfreev (split_terms);
868
869   return result;
870 }
871
872 static void
873 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
874 {
875   GtkFontChooserPrivate *priv = fontchooser->priv;
876   GtkTreeView       *treeview = GTK_TREE_VIEW (priv->family_face_list);
877   GtkCellRenderer   *cell;
878   GtkTreeViewColumn *col;
879
880   priv->model = gtk_list_store_new (4,
881                                     PANGO_TYPE_FONT_FAMILY,
882                                     PANGO_TYPE_FONT_FACE,
883                                     G_TYPE_STRING,
884                                     G_TYPE_STRING);
885
886   priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
887   g_object_unref (priv->model);
888
889   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
890                                           visible_func, (gpointer)priv, NULL);
891
892   gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
893   g_object_unref (priv->filter_model);
894
895   gtk_tree_view_set_rules_hint      (treeview, TRUE);
896   gtk_tree_view_set_headers_visible (treeview, FALSE);
897
898   cell = gtk_cell_renderer_text_new ();
899   col = gtk_tree_view_column_new_with_attributes ("Family",
900                                                   cell,
901                                                   "markup", PREVIEW_TEXT_COLUMN,
902                                                   NULL);
903
904   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
905
906   gtk_tree_view_append_column (treeview, col);
907
908   populate_list (fontchooser, treeview, priv->model);
909 }
910
911 static void
912 gtk_font_chooser_dispose (GObject *object)
913 {
914   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
915   GtkFontChooserPrivate *priv = fontchooser->priv;
916
917   if (priv->cursor_changed_handler != 0)
918     {
919       g_signal_handler_disconnect (priv->family_face_list,
920                                    priv->cursor_changed_handler);
921       priv->cursor_changed_handler = 0;
922     }
923
924   G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
925 }
926
927 static void
928 gtk_font_chooser_finalize (GObject *object)
929 {
930   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
931   GtkFontChooserPrivate *priv = fontchooser->priv;
932
933   if (priv->fontname)
934     g_free (priv->fontname);
935
936   if (priv->family)
937     g_object_unref (priv->family);
938
939   if (priv->face)
940     g_object_unref (priv->face);
941
942   if (priv->filter_data_destroy)
943     priv->filter_data_destroy (priv->filter_data);
944
945   G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
946 }
947
948 static void
949 gtk_font_chooser_screen_changed (GtkWidget *widget,
950                                  GdkScreen *previous_screen)
951 {
952   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
953   GtkFontChooserPrivate *priv = fontchooser->priv;
954
955   populate_list (fontchooser,
956                  GTK_TREE_VIEW (priv->family_face_list),
957                  priv->model);
958
959   if (priv->fontname)
960     gtk_font_chooser_select_font_name (fontchooser);
961 }
962
963 static void
964 gtk_font_chooser_style_updated (GtkWidget *widget)
965 {
966   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
967
968   GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
969
970   populate_list (fontchooser,
971                  GTK_TREE_VIEW (fontchooser->priv->family_face_list),
972                  fontchooser->priv->model);
973 }
974
975 /**
976  * gtk_font_chooser_get_family:
977  * @fontchooser: a #GtkFontChooser
978  *
979  * Gets the #PangoFontFamily representing the selected font family.
980  * Font families are a collection of font faces.
981  *
982  * Return value: (transfer none): A #PangoFontFamily representing the
983  *     selected font family. The returned object is owned by @fontchooser
984  *     and must not be modified or freed.
985  *
986  * Since: 3.2
987  */
988 PangoFontFamily *
989 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
990 {
991   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
992
993   return fontchooser->priv->family;
994 }
995
996 /**
997  * gtk_font_chooser_get_face:
998  * @fontchooser: a #GtkFontChooser
999  *
1000  * Gets the #PangoFontFace representing the selected font group
1001  * details (i.e. family, slant, weight, width, etc).
1002  *
1003  * Return value: (transfer none): A #PangoFontFace representing the
1004  *     selected font group details. The returned object is owned by
1005  *     @fontchooser and must not be modified or freed.
1006  *
1007  * Since: 3.2
1008  */
1009 PangoFontFace *
1010 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1011 {
1012   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1013
1014   return fontchooser->priv->face;
1015 }
1016
1017 /**
1018  * gtk_font_chooser_get_size:
1019  * @fontchooser: a #GtkFontChooser
1020  *
1021  * The selected font size.
1022  *
1023  * Return value: A n integer representing the selected font size,
1024  *     or -1 if no font size is selected.
1025  *
1026  * Since: 3.2
1027  */
1028 gint
1029 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1030 {
1031   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1032
1033   return fontchooser->priv->size;
1034 }
1035
1036 /**
1037  * gtk_font_chooser_get_font_name:
1038  * @fontchooser: a #GtkFontChooser
1039  *
1040  * Gets the currently-selected font name.
1041  *
1042  * Note that this can be a different string than what you set with
1043  * gtk_font_chooser_set_font_name(), as the font chooser widget may
1044  * normalize font names and thus return a string with a different
1045  * structure. For example, "Helvetica Italic Bold 12" could be
1046  * normalized to "Helvetica Bold Italic 12".
1047  *
1048  * Use pango_font_description_equal() if you want to compare two
1049  * font descriptions.
1050  *
1051  * Return value: (transfer full) (allow-none): A string with the name
1052  *     of the current font, or %NULL if  no font is selected. You must
1053  *     free this string with g_free().
1054  *
1055  * Since: 3.2
1056  */
1057 gchar *
1058 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1059 {
1060   gchar                *font_name;
1061   gchar                *font_desc_name;
1062   PangoFontDescription *desc;
1063
1064   if (!fontchooser->priv->face)
1065     return NULL;
1066
1067   desc = pango_font_face_describe (fontchooser->priv->face);
1068   font_desc_name = pango_font_description_to_string (desc);
1069   pango_font_description_free (desc);
1070
1071   font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1072   g_free (font_desc_name);
1073   return font_name;
1074 }
1075
1076 /**
1077  * gtk_font_chooser_set_font_name:
1078  * @fontchooser: a #GtkFontChooser
1079  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1080  *
1081  * Sets the currently-selected font.
1082  *
1083  * Return value: %TRUE if the font could be set successfully; %FALSE
1084  *     if no such font exists or if the @fontchooser doesn't belong
1085  *     to a particular screen yet.
1086  *
1087  * Since: 3.2
1088  */
1089 gboolean
1090 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1091                                 const gchar    *fontname)
1092 {
1093   GtkFontChooserPrivate *priv = fontchooser->priv;
1094   gboolean found = FALSE;
1095
1096   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1097   g_return_val_if_fail (fontname != NULL, FALSE);
1098
1099   if (priv->fontname)
1100     g_free (priv->fontname);
1101   priv->fontname = g_strdup (fontname);
1102
1103   if (gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1104     found = gtk_font_chooser_select_font_name (fontchooser);
1105
1106   g_object_notify (G_OBJECT (fontchooser), "font-name");
1107
1108   return found;
1109 }
1110
1111 static gboolean
1112 gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser)
1113 {
1114   GtkFontChooserPrivate *priv = fontchooser->priv;
1115   GtkTreeIter iter;
1116   gboolean valid;
1117   gchar *family_name;
1118   PangoFontDescription *desc;
1119   gboolean found = FALSE;
1120
1121   desc = pango_font_description_from_string (priv->fontname);
1122   family_name = (gchar*)pango_font_description_get_family (desc);
1123
1124   g_free (priv->fontname);
1125   priv->fontname = NULL;
1126
1127   if (!family_name)
1128     {
1129       pango_font_description_free (desc);
1130       return FALSE;
1131     }
1132
1133   /* We make sure the filter is clear */
1134   gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1135
1136   /* We find the matching family/face */
1137   for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter_model), &iter);
1138        valid;
1139        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter_model), &iter))
1140     {
1141       PangoFontFace        *face;
1142       PangoFontDescription *tmp_desc;
1143
1144       gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
1145                           FACE_COLUMN, &face,
1146                           -1);
1147
1148       tmp_desc = pango_font_face_describe (face);
1149       if (pango_font_description_get_size_is_absolute (desc))
1150         pango_font_description_set_absolute_size (tmp_desc,
1151                                                   pango_font_description_get_size (desc));
1152       else
1153         pango_font_description_set_size (tmp_desc,
1154                                          pango_font_description_get_size (desc));
1155
1156       if (pango_font_description_equal (desc, tmp_desc))
1157         {
1158           GtkTreePath *path;
1159           gint size = pango_font_description_get_size (desc);
1160
1161           if (size)
1162             {
1163               if (pango_font_description_get_size_is_absolute (desc))
1164                 size = size * PANGO_SCALE;
1165               gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1166                                          size / PANGO_SCALE);
1167             }
1168
1169           path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model), &iter);
1170
1171           if (path)
1172             {
1173               gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1174                                         path,
1175                                         NULL,
1176                                         FALSE);
1177               gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1178                                             path,
1179                                             NULL,
1180                                             FALSE,
1181                                             0.5,
1182                                             0.5);
1183               gtk_tree_path_free (path);
1184             }
1185
1186           found = TRUE;
1187         }
1188
1189       g_object_unref (face);
1190       pango_font_description_free (tmp_desc);
1191
1192       if (found)
1193         break;
1194     }
1195
1196   pango_font_description_free (desc);
1197
1198   return found;
1199 }
1200
1201 /**
1202  * gtk_font_chooser_get_preview_text:
1203  * @fontchooser: a #GtkFontChooser
1204  *
1205  * Gets the text displayed in the preview area.
1206  *
1207  * Return value: (transfer none): the text displayed in the
1208  *     preview area. This string is owned by the widget and
1209  *     should not be modified or freed
1210  *
1211  * Since: 3.2
1212  */
1213 const gchar*
1214 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1215 {
1216   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1217
1218   return (const gchar*)fontchooser->priv->preview_text;
1219 }
1220
1221
1222 /**
1223  * gtk_font_chooser_set_preview_text:
1224  * @fontchooser: a #GtkFontChooser
1225  * @text: (transfer none): the text to display in the preview area
1226  *
1227  * Sets the text displayed in the preview area.
1228  * The @text is used to show how the selected font looks.
1229  *
1230  * Since: 3.2
1231  */
1232 void
1233 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1234                                    const gchar    *text)
1235 {
1236   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1237   g_return_if_fail (text != NULL);
1238
1239   g_free (fontchooser->priv->preview_text);
1240   fontchooser->priv->preview_text = g_strdup (text);
1241
1242   populate_list (fontchooser,
1243                  GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1244                  fontchooser->priv->model);
1245
1246   gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1247
1248   g_object_notify (G_OBJECT (fontchooser), "preview-text");
1249 }
1250
1251 /**
1252  * gtk_font_chooser_get_show_preview_entry:
1253  * @fontchooser: a #GtkFontChooser
1254  *
1255  * Returns whether the preview entry is shown or not.
1256  *
1257  * Return value: %TRUE if the preview entry is shown
1258  *     or %FALSE if it is hidden.
1259  *
1260  * Since: 3.2
1261  */
1262 gboolean
1263 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1264 {
1265   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1266
1267   return fontchooser->priv->show_preview_entry;
1268 }
1269
1270 /**
1271  * gtk_font_chooser_set_show_preview_entry:
1272  * @fontchooser: a #GtkFontChooser
1273  * @show_preview_entry: whether to show the editable preview entry or not
1274  *
1275  * Shows or hides the editable preview entry.
1276  *
1277  * Since: 3.2
1278  */
1279 void
1280 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1281                                          gboolean        show_preview_entry)
1282 {
1283   GtkFontChooserPrivate *priv = fontchooser->priv;
1284
1285   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1286
1287   if (priv->show_preview_entry != show_preview_entry)
1288     {
1289       fontchooser->priv->show_preview_entry = show_preview_entry;
1290
1291       if (show_preview_entry)
1292         gtk_widget_show (fontchooser->priv->preview);
1293       else
1294         gtk_widget_hide (fontchooser->priv->preview);
1295
1296       g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1297     }
1298 }
1299
1300 /**
1301  * gtk_font_chooser_set_filter_func:
1302  * @fontchooser: a #GtkFontChooser
1303  * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
1304  * @data: data to pass to @filter
1305  * @destroy: function to call to free @data when it is no longer needed
1306  *
1307  * Adds a filter function that decides which fonts to display
1308  * in the font chooser.
1309  *
1310  * Since: 3.2
1311  */
1312 void
1313 gtk_font_chooser_set_filter_func (GtkFontChooser   *fontchooser,
1314                                   GtkFontFilterFunc filter,
1315                                   gpointer          data,
1316                                   GDestroyNotify    destroy)
1317 {
1318   GtkFontChooserPrivate *priv = fontchooser->priv;
1319
1320   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1321
1322   if (priv->filter_data_destroy)
1323     priv->filter_data_destroy (priv->filter_data);
1324
1325   priv->filter_func = filter;
1326   priv->filter_data = data;
1327   priv->filter_data_destroy = destroy;
1328
1329   populate_list (fontchooser,
1330                  GTK_TREE_VIEW (priv->family_face_list),
1331                  priv->model);
1332 }