]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserwidget.c
Remove some more CUPS 1.2 ifdefs.
[~andy/gtk] / gtk / gtkfontchooserwidget.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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include <stdlib.h>
21 #include <glib/gprintf.h>
22 #include <string.h>
23
24 #include <atk/atk.h>
25
26 #include "gtkfontchooserwidget.h"
27
28 #include "gtkadjustment.h"
29 #include "gtkbuildable.h"
30 #include "gtkbox.h"
31 #include "gtkcellrenderertext.h"
32 #include "gtkentry.h"
33 #include "gtkgrid.h"
34 #include "gtkfontchooser.h"
35 #include "gtkfontchooserutils.h"
36 #include "gtkintl.h"
37 #include "gtklabel.h"
38 #include "gtkliststore.h"
39 #include "gtknotebook.h"
40 #include "gtkprivate.h"
41 #include "gtkscale.h"
42 #include "gtkscrolledwindow.h"
43 #include "gtkspinbutton.h"
44 #include "gtktextview.h"
45 #include "gtktreeselection.h"
46 #include "gtktreeview.h"
47 #include "gtkwidget.h"
48
49 /**
50  * SECTION:gtkfontchooserwidget
51  * @Short_description: A widget for selecting fonts
52  * @Title: GtkFontChooserWidget
53  * @See_also: #GtkFontChooserDialog
54  *
55  * The #GtkFontChooserWidget widget lists the available fonts,
56  * styles and sizes, allowing the user to select a font. It is
57  * used in the #GtkFontChooserDialog widget to provide a
58  * dialog box for selecting fonts.
59  *
60  * To set the font which is initially selected, use
61  * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
62  *
63  * To get the selected font use gtk_font_chooser_get_font() or
64  * gtk_font_chooser_get_font_desc().
65  *
66  * To change the text which is shown in the preview area, use
67  * gtk_font_chooser_set_preview_text().
68  *
69  * Since: 3.2
70  */
71
72
73 struct _GtkFontChooserWidgetPrivate
74 {
75   GtkWidget    *search_entry;
76   GtkWidget    *family_face_list;
77   GtkCellRenderer *family_face_cell;
78   GtkWidget    *list_scrolled_window;
79   GtkWidget    *empty_list;
80   GtkWidget    *list_notebook;
81   GtkTreeModel *model;
82   GtkTreeModel *filter_model;
83
84   GtkWidget       *preview;
85   gchar           *preview_text;
86   gboolean         show_preview_entry;
87
88   GtkWidget *size_spin;
89   GtkWidget *size_slider;
90
91   PangoFontDescription *font_desc;
92   GtkTreeIter           font_iter;      /* invalid if font not available or pointer into model
93                                            (not filter_model) to the row containing font */
94   GtkFontFilterFunc filter_func;
95   gpointer          filter_data;
96   GDestroyNotify    filter_data_destroy;
97 };
98
99 /* This is the initial fixed height and the top padding of the preview entry */
100 #define PREVIEW_HEIGHT 72
101 #define PREVIEW_TOP_PADDING 6
102
103 /* These are the sizes of the font, style & size lists. */
104 #define FONT_LIST_HEIGHT  136
105 #define FONT_LIST_WIDTH   190
106 #define FONT_STYLE_LIST_WIDTH 170
107 #define FONT_SIZE_LIST_WIDTH  60
108
109 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
110
111 enum {
112   FAMILY_COLUMN,
113   FACE_COLUMN,
114   FONT_DESC_COLUMN,
115   PREVIEW_TITLE_COLUMN
116 };
117
118 static void gtk_font_chooser_widget_set_property         (GObject         *object,
119                                                           guint            prop_id,
120                                                           const GValue    *value,
121                                                           GParamSpec      *pspec);
122 static void gtk_font_chooser_widget_get_property         (GObject         *object,
123                                                           guint            prop_id,
124                                                           GValue          *value,
125                                                           GParamSpec      *pspec);
126 static void gtk_font_chooser_widget_finalize             (GObject         *object);
127
128 static void gtk_font_chooser_widget_screen_changed       (GtkWidget       *widget,
129                                                           GdkScreen       *previous_screen);
130
131 static void gtk_font_chooser_widget_bootstrap_fontlist   (GtkFontChooserWidget *fontchooser);
132
133 static gboolean gtk_font_chooser_widget_find_font        (GtkFontChooserWidget *fontchooser,
134                                                           const PangoFontDescription *font_desc,
135                                                           GtkTreeIter          *iter);
136 static void     gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
137
138 static gchar   *gtk_font_chooser_widget_get_font         (GtkFontChooserWidget *fontchooser);
139 static void     gtk_font_chooser_widget_set_font         (GtkFontChooserWidget *fontchooser,
140                                                           const gchar          *fontname);
141
142 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc  (GtkFontChooserWidget *fontchooser);
143 static void                  gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget *fontchooser,
144                                                                      PangoFontDescription *font_desc,
145                                                                      GtkTreeIter          *iter);
146 static void                  gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
147                                                                      PangoFontDescription *font_desc);
148
149
150 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
151 static void         gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
152                                                               const gchar          *text);
153
154 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
155 static void     gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
156                                                                 gboolean              show_preview_entry);
157
158 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
159
160 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
161                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
162                                                 gtk_font_chooser_widget_iface_init))
163
164 static void
165 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
166 {
167   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
168   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
169
170   widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
171
172   gobject_class->finalize = gtk_font_chooser_widget_finalize;
173   gobject_class->set_property = gtk_font_chooser_widget_set_property;
174   gobject_class->get_property = gtk_font_chooser_widget_get_property;
175
176   _gtk_font_chooser_install_properties (gobject_class);
177
178   g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
179 }
180
181 static void
182 gtk_font_chooser_widget_set_property (GObject         *object,
183                                       guint            prop_id,
184                                       const GValue    *value,
185                                       GParamSpec      *pspec)
186 {
187   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
188
189   switch (prop_id)
190     {
191     case GTK_FONT_CHOOSER_PROP_FONT:
192       gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
193       break;
194     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
195       gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
196       break;
197     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
198       gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
199       break;
200     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
201       gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
202       break;
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206     }
207 }
208
209 static void
210 gtk_font_chooser_widget_get_property (GObject         *object,
211                                       guint            prop_id,
212                                       GValue          *value,
213                                       GParamSpec      *pspec)
214 {
215   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
216
217   switch (prop_id)
218     {
219     case GTK_FONT_CHOOSER_PROP_FONT:
220       g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
221       break;
222     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
223       g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
224       break;
225     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
226       g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
227       break;
228     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
229       g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
230       break;
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234     }
235 }
236
237 static void
238 gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
239 {
240   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
241   gtk_font_chooser_widget_ensure_selection (fontchooser);
242 }
243
244 static void
245 text_changed_cb (GtkEntry       *entry,
246                  GParamSpec     *pspec,
247                  GtkFontChooserWidget *fc)
248 {
249   GtkFontChooserWidgetPrivate *priv = fc->priv;
250   const gchar *text;
251
252   text = gtk_entry_get_text (entry);
253
254   if (text == NULL || text[0] == '\0')
255     {
256       GIcon *icon;
257
258       icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
259       g_object_set (G_OBJECT (priv->search_entry),
260                     "secondary-icon-gicon", icon,
261                     "secondary-icon-activatable", FALSE,
262                     "secondary-icon-sensitive", FALSE,
263                     NULL);
264       g_object_unref (icon);
265     }
266   else
267     {
268       if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
269         {
270           GIcon *icon;
271
272           icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
273           g_object_set (G_OBJECT (priv->search_entry),
274                         "secondary-icon-gicon", icon,
275                         "secondary-icon-activatable", TRUE,
276                         "secondary-icon-sensitive", TRUE,
277                         NULL);
278           g_object_unref (icon);
279         }
280     }
281
282   gtk_font_chooser_widget_refilter_font_list (fc);
283 }
284
285 static void
286 icon_press_cb (GtkEntry             *entry,
287                GtkEntryIconPosition  pos,
288                GdkEvent             *event,
289                gpointer              user_data)
290 {
291   gtk_entry_set_text (entry, "");
292 }
293
294 static void
295 size_change_cb (GtkAdjustment *adjustment,
296                 gpointer       user_data)
297 {
298   GtkFontChooserWidget *fontchooser = user_data;
299   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
300   PangoFontDescription *font_desc;
301   gdouble size = gtk_adjustment_get_value (adjustment);
302
303   font_desc = pango_font_description_new ();
304   if (pango_font_description_get_size_is_absolute (priv->font_desc))
305     pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
306   else
307     pango_font_description_set_size (font_desc, size * PANGO_SCALE);
308
309   gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
310 }
311
312 static void
313 gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
314 {
315   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
316   GtkAdjustment *adj;
317   const int *sizes;
318   gint *font_sizes;
319   gint i, n_sizes;
320
321   if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
322     {
323       PangoFontFace *face;
324
325       gtk_tree_model_get (priv->model, &priv->font_iter,
326                           FACE_COLUMN, &face,
327                           -1);
328
329       pango_font_face_list_sizes (face, &font_sizes, &n_sizes);
330
331       /* It seems not many fonts actually have a sane set of sizes */
332       for (i = 0; i < n_sizes; i++)
333         font_sizes[i] = font_sizes[i] / PANGO_SCALE;
334
335       g_object_unref (face);
336     }
337   else
338     {
339       font_sizes = NULL;
340       n_sizes = 0;
341     }
342
343   if (n_sizes < 2)
344     {
345       static const gint fallback_sizes[] = {
346         6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
347       };
348
349       sizes = fallback_sizes;
350       n_sizes = G_N_ELEMENTS (fallback_sizes);
351     }
352   else
353     {
354       sizes = font_sizes;
355     }
356
357   gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
358
359   adj = gtk_range_get_adjustment(GTK_RANGE (priv->size_slider));
360
361   /* ensure clamping doesn't callback into font resizing code */
362   g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
363   gtk_adjustment_configure (adj,
364                             gtk_adjustment_get_value (adj),
365                             sizes[0],
366                             sizes[n_sizes - 1],
367                             gtk_adjustment_get_step_increment (adj),
368                             gtk_adjustment_get_page_increment (adj),
369                             gtk_adjustment_get_page_size (adj));
370   g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);
371
372   for (i = 0; i < n_sizes; i++)
373     {
374       gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
375                           sizes[i],
376                           GTK_POS_BOTTOM, NULL);
377     }
378
379   g_free (font_sizes);
380 }
381
382 static void
383 row_activated_cb (GtkTreeView       *view,
384                   GtkTreePath       *path,
385                   GtkTreeViewColumn *column,
386                   gpointer           user_data)
387 {
388   GtkFontChooserWidget *fontchooser = user_data;
389   gchar *fontname;
390
391   fontname = gtk_font_chooser_widget_get_font (fontchooser);
392   _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
393   g_free (fontname);
394 }
395
396 static PangoFontDescription *
397 tree_model_get_font_description (GtkTreeModel *model,
398                                  GtkTreeIter  *iter)
399 {
400   PangoFontDescription *desc;
401   PangoFontFace *face;
402   GtkTreeIter child_iter;
403
404   gtk_tree_model_get (model, iter,
405                       FONT_DESC_COLUMN, &desc,
406                       -1);
407   if (desc != NULL)
408     return desc;
409
410   gtk_tree_model_get (model, iter,
411                       FACE_COLUMN, &face,
412                       -1);
413   desc = pango_font_face_describe (face);
414   g_object_unref (face);
415   
416   if (GTK_IS_TREE_MODEL_FILTER (model))
417     {
418       gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
419                                                         &child_iter,
420                                                         iter);
421       iter = &child_iter;
422       model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
423     }
424
425   gtk_list_store_set (GTK_LIST_STORE (model), iter,
426                       FONT_DESC_COLUMN, desc,
427                       -1);
428
429   return desc;
430 }
431
432 static void
433 cursor_changed_cb (GtkTreeView *treeview,
434                    gpointer     user_data)
435 {
436   GtkFontChooserWidget *fontchooser = user_data;
437   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
438   PangoFontDescription *desc;
439   GtkTreeIter filter_iter, iter;
440   GtkTreePath *path = NULL;
441
442   gtk_tree_view_get_cursor (treeview, &path, NULL);
443
444   if (!path)
445     return;
446
447   if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
448     {
449       gtk_tree_path_free (path);
450       return;
451     }
452
453   gtk_tree_path_free (path);
454
455   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
456                                                     &iter,
457                                                     &filter_iter);
458   desc = tree_model_get_font_description (priv->model, &iter);
459
460   gtk_font_chooser_widget_merge_font_desc (fontchooser, desc, &iter);
461 }
462
463 static gboolean
464 zoom_preview_cb (GtkWidget      *scrolled_window,
465                  GdkEventScroll *event,
466                  gpointer        user_data)
467 {
468   GtkFontChooserWidget *fc = user_data;
469   GtkFontChooserWidgetPrivate *priv = fc->priv;
470   GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
471
472   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
473     gtk_adjustment_set_value (adj,
474                               gtk_adjustment_get_value (adj) +
475                               gtk_adjustment_get_step_increment (adj));
476   else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
477     gtk_adjustment_set_value (adj,
478                               gtk_adjustment_get_value (adj) -
479                               gtk_adjustment_get_step_increment (adj));
480   return TRUE;
481 }
482
483 static void
484 row_inserted_cb (GtkTreeModel *model,
485                  GtkTreePath  *path,
486                  GtkTreeIter  *iter,
487                  gpointer      user_data)
488 {
489   GtkFontChooserWidget *fontchooser = user_data;
490   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
491
492   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
493 }
494
495 static void
496 row_deleted_cb  (GtkTreeModel *model,
497                  GtkTreePath  *path,
498                  gpointer      user_data)
499 {
500   GtkFontChooserWidget *fontchooser = user_data;
501   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
502
503   if (gtk_tree_model_iter_n_children (model, NULL) == 0)
504     gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
505 }
506
507 static void
508 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
509 {
510   GIcon *icon;
511   GtkFontChooserWidgetPrivate *priv;
512   GtkWidget *scrolled_win;
513   GtkWidget *grid;
514
515   fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
516                                                    GTK_TYPE_FONT_CHOOSER_WIDGET,
517                                                    GtkFontChooserWidgetPrivate);
518
519   priv = fontchooser->priv;
520
521   /* Default preview string  */
522   priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
523   priv->show_preview_entry = TRUE;
524   priv->font_desc = pango_font_description_new ();
525
526   gtk_widget_push_composite_child ();
527
528   /* Creating fundamental widgets for the private struct */
529   priv->search_entry = gtk_entry_new ();
530   priv->family_face_list = gtk_tree_view_new ();
531   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
532   priv->preview = gtk_entry_new ();
533   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
534                                                 0.0,
535                                                 (gdouble)(G_MAXINT / PANGO_SCALE),
536                                                 1.0);
537
538   priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
539
540   /** Bootstrapping widget layout **/
541   gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
542
543   /* Main font family/face view */
544   priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
545   scrolled_win = priv->list_scrolled_window;
546   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
547                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
548   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
549                                        GTK_SHADOW_ETCHED_IN);
550   gtk_widget_set_size_request (scrolled_win, 400, 300);
551   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
552
553   /* Text to display when list is empty */
554   priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
555   gtk_widget_set_margin_top    (priv->empty_list, 12);
556   gtk_widget_set_margin_left   (priv->empty_list, 12);
557   gtk_widget_set_margin_right  (priv->empty_list, 12);
558   gtk_widget_set_margin_bottom (priv->empty_list, 12);
559   gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
560   gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
561
562   priv->list_notebook = gtk_notebook_new ();
563   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
564   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
565   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
566
567   /* Basic layout */
568   grid = gtk_grid_new ();
569
570   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
571   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
572
573   gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
574   gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
575   gtk_grid_attach (GTK_GRID (grid), priv->preview,      0, 2, 2, 1);
576
577   gtk_grid_attach (GTK_GRID (grid), priv->size_slider,  0, 3, 1, 1);
578   gtk_grid_attach (GTK_GRID (grid), priv->size_spin,    1, 3, 1, 1);
579
580   gtk_widget_set_hexpand  (GTK_WIDGET (scrolled_win),      TRUE);
581   gtk_widget_set_vexpand  (GTK_WIDGET (scrolled_win),      TRUE);
582   gtk_widget_set_hexpand  (GTK_WIDGET (priv->search_entry), TRUE);
583
584   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_slider), TRUE);
585   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_spin),   FALSE);
586
587   gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
588
589   gtk_widget_show_all (GTK_WIDGET (fontchooser));
590   gtk_widget_hide     (GTK_WIDGET (fontchooser));
591
592   /* Treeview column and model bootstrapping */
593   gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
594
595   /* Set default preview text */
596   gtk_entry_set_text (GTK_ENTRY (priv->preview),
597                       pango_language_get_sample_string (NULL));
598
599   /* Set search icon and place holder text */
600   icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
601   g_object_set (G_OBJECT (priv->search_entry),
602                 "secondary-icon-gicon", icon,
603                 "secondary-icon-activatable", FALSE,
604                 "secondary-icon-sensitive", FALSE,
605                 NULL);
606   g_object_unref (icon);
607
608   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
609
610   /** Callback connections **/
611   g_signal_connect (priv->search_entry, "notify::text",
612                     G_CALLBACK (text_changed_cb), fontchooser);
613   g_signal_connect (priv->search_entry,
614                     "icon-press", G_CALLBACK (icon_press_cb), NULL);
615
616   g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
617                     "value-changed", G_CALLBACK (size_change_cb), fontchooser);
618   g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
619                     "value-changed", G_CALLBACK (size_change_cb), fontchooser);
620
621   g_signal_connect (priv->family_face_list, "cursor-changed",
622                     G_CALLBACK (cursor_changed_cb), fontchooser);
623   g_signal_connect (priv->family_face_list, "row-activated",
624                     G_CALLBACK (row_activated_cb), fontchooser);
625
626   /* Zoom on preview scroll */
627   g_signal_connect (priv->preview, "scroll-event",
628                     G_CALLBACK (zoom_preview_cb), fontchooser);
629
630   g_signal_connect (priv->size_slider, "scroll-event",
631                     G_CALLBACK (zoom_preview_cb), fontchooser);
632
633   /* Font list empty hides the scrolledwindow */
634   g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
635                     G_CALLBACK (row_deleted_cb), fontchooser);
636   g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
637                     G_CALLBACK (row_inserted_cb), fontchooser);
638
639   /* Set default focus */
640   gtk_widget_pop_composite_child ();
641
642   gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
643 }
644
645 /**
646  * gtk_font_chooser_widget_new:
647  *
648  * Creates a new #GtkFontChooserWidget.
649  *
650  * Return value: a new #GtkFontChooserWidget
651  *
652  * Since: 3.2
653  */
654 GtkWidget *
655 gtk_font_chooser_widget_new (void)
656 {
657   return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
658 }
659
660 static int
661 cmp_families (const void *a,
662               const void *b)
663 {
664   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
665   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
666
667   return g_utf8_collate (a_name, b_name);
668 }
669
670 static void
671 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
672 {
673   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
674   GtkListStore *list_store;
675   gint n_families, i;
676   PangoFontFamily **families;
677   gchar *family_and_face;
678
679   list_store = GTK_LIST_STORE (priv->model);
680
681   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
682                                &families,
683                                &n_families);
684
685   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
686
687   gtk_list_store_clear (list_store);
688
689   /* Iterate over families and faces */
690   for (i = 0; i < n_families; i++)
691     {
692       GtkTreeIter     iter;
693       PangoFontFace **faces;
694       int             j, n_faces;
695       const gchar    *fam_name = pango_font_family_get_name (families[i]);
696
697       pango_font_family_list_faces (families[i], &faces, &n_faces);
698
699       for (j = 0; j < n_faces; j++)
700         {
701           const gchar *face_name;
702
703           face_name = pango_font_face_get_face_name (faces[j]);
704
705           family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
706
707           gtk_list_store_insert_with_values (list_store, &iter, -1,
708                                              FAMILY_COLUMN, families[i],
709                                              FACE_COLUMN, faces[j],
710                                              PREVIEW_TITLE_COLUMN, family_and_face,
711                                              -1);
712
713           g_free (family_and_face);
714         }
715
716       g_free (faces);
717     }
718
719   g_free (families);
720
721   /* now make sure the font list looks right */
722   if (!gtk_font_chooser_widget_find_font (fontchooser,
723                                           priv->font_desc,
724                                           &priv->font_iter))
725     memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
726
727   gtk_font_chooser_widget_ensure_selection (fontchooser);
728 }
729
730 static gboolean
731 visible_func (GtkTreeModel *model,
732               GtkTreeIter  *iter,
733               gpointer      user_data)
734 {
735   GtkFontChooserWidgetPrivate *priv = user_data;
736   gboolean result = TRUE;
737   const gchar *search_text;
738   gchar **split_terms;
739   gchar *font_name, *font_name_casefold;
740   guint i;
741
742   if (priv->filter_func != NULL)
743     {
744       PangoFontFamily *family;
745       PangoFontFace *face;
746
747       gtk_tree_model_get (model, iter,
748                           FAMILY_COLUMN, &family,
749                           FACE_COLUMN, &face,
750                           -1);
751
752       result = priv->filter_func (family, face, priv->filter_data);
753
754       g_object_unref (family);
755       g_object_unref (face);
756       
757       if (!result)
758         return FALSE;
759     }
760
761   /* If there's no filter string we show the item */
762   search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
763   if (strlen (search_text) == 0)
764     return TRUE;
765
766   gtk_tree_model_get (model, iter,
767                       PREVIEW_TITLE_COLUMN, &font_name,
768                       -1);
769
770   if (font_name == NULL)
771     return FALSE;
772
773   split_terms = g_strsplit (search_text, " ", 0);
774   font_name_casefold = g_utf8_casefold (font_name, -1);
775
776   for (i = 0; split_terms[i] && result; i++)
777     {
778       gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
779
780       if (!strstr (font_name_casefold, term_casefold))
781         result = FALSE;
782
783       g_free (term_casefold);
784     }
785
786   g_free (font_name_casefold);
787   g_free (font_name);
788   g_strfreev (split_terms);
789
790   return result;
791 }
792
793 /* in pango units */
794 static int
795 gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
796 {
797   GtkWidget *treeview = fontchooser->priv->family_face_list;
798   double dpi, font_size;
799
800   dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
801   gtk_style_context_get (gtk_widget_get_style_context (treeview),
802                          gtk_widget_get_state_flags (treeview),
803                          "font-size", &font_size,
804                          NULL);
805
806   return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
807 }
808
809 static PangoAttrList *
810 gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget       *fontchooser,
811                                                 const PangoFontDescription *font_desc,
812                                                 gsize                       first_line_len)
813 {
814   PangoAttribute *attribute;
815   PangoAttrList *attrs;
816
817   attrs = pango_attr_list_new ();
818
819   attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
820   attribute->end_index = first_line_len;
821   pango_attr_list_insert (attrs, attribute);
822
823   attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
824   attribute->end_index = first_line_len;
825   pango_attr_list_insert (attrs, attribute);
826
827   if (font_desc)
828     {
829       attribute = pango_attr_font_desc_new (font_desc);
830       attribute->start_index = first_line_len;
831       pango_attr_list_insert (attrs, attribute);
832     }
833
834   attribute = pango_attr_fallback_new (FALSE);
835   attribute->start_index = first_line_len;
836   pango_attr_list_insert (attrs, attribute);
837
838   attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
839   attribute->start_index = first_line_len;
840   pango_attr_list_insert (attrs, attribute);
841
842   return attrs;
843 }
844
845 static void
846 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
847                                         GtkCellRenderer   *cell,
848                                         GtkTreeModel      *tree_model,
849                                         GtkTreeIter       *iter,
850                                         gpointer           user_data)
851 {
852   GtkFontChooserWidget *fontchooser = user_data;
853   PangoFontDescription *font_desc;
854   PangoAttrList *attrs;
855   char *to_string, *text;
856   gsize first_line_len;
857
858   font_desc = tree_model_get_font_description (tree_model, iter);
859
860   to_string = pango_font_description_to_string (font_desc);
861
862   text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
863   first_line_len = strlen (to_string) + 1;
864   
865   attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
866                                                           font_desc,
867                                                           first_line_len);
868
869   g_object_set (cell,
870                 "attributes", attrs,
871                 "text", text,
872                 NULL);
873
874   pango_font_description_free (font_desc);
875   pango_attr_list_unref (attrs);
876   g_free (to_string);
877   g_free (text);
878 }
879
880 static void
881 gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
882 {
883   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
884   PangoAttrList *attrs;
885   GtkRequisition size;
886
887   gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
888
889   attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
890                                                           NULL,
891                                                           1);
892   
893   g_object_set (priv->family_face_cell,
894                 "attributes", attrs,
895                 "text", "x\nx",
896                 NULL);
897
898   pango_attr_list_unref (attrs);
899
900   gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
901                                         priv->family_face_list,
902                                         &size,
903                                         NULL);
904   gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
905 }
906
907 static void
908 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
909 {
910   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
911   GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
912   GtkTreeViewColumn *col;
913
914   g_signal_connect_data (priv->family_face_list,
915                          "style-updated",
916                          G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
917                          fontchooser,
918                          NULL,
919                          G_CONNECT_AFTER | G_CONNECT_SWAPPED);
920
921   priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
922                                                     PANGO_TYPE_FONT_FAMILY,
923                                                     PANGO_TYPE_FONT_FACE,
924                                                     PANGO_TYPE_FONT_DESCRIPTION,
925                                                     G_TYPE_STRING));
926
927   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
928   g_object_unref (priv->model);
929
930   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
931                                           visible_func, (gpointer)priv, NULL);
932
933   gtk_tree_view_set_model (treeview, priv->filter_model);
934   g_object_unref (priv->filter_model);
935
936   gtk_tree_view_set_rules_hint      (treeview, TRUE);
937   gtk_tree_view_set_headers_visible (treeview, FALSE);
938   gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
939
940   priv->family_face_cell = gtk_cell_renderer_text_new ();
941   g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
942
943   col = gtk_tree_view_column_new ();
944   gtk_tree_view_column_set_title (col, _("Font Family"));
945   gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
946   gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
947   gtk_tree_view_column_set_cell_data_func (col,
948                                            priv->family_face_cell,
949                                            gtk_font_chooser_widget_cell_data_func,
950                                            fontchooser,
951                                            NULL);
952
953   gtk_tree_view_append_column (treeview, col);
954
955   gtk_font_chooser_widget_load_fonts (fontchooser);
956
957   gtk_font_chooser_widget_set_cell_size (fontchooser);
958 }
959
960 static void
961 gtk_font_chooser_widget_finalize (GObject *object)
962 {
963   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
964   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
965
966   if (priv->font_desc)
967     pango_font_description_free (priv->font_desc);
968
969   if (priv->filter_data_destroy)
970     priv->filter_data_destroy (priv->filter_data);
971
972   G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
973 }
974
975 static gboolean
976 my_pango_font_family_equal (const char *familya,
977                             const char *familyb)
978 {
979   return g_ascii_strcasecmp (familya, familyb) == 0;
980 }
981
982 static gboolean
983 gtk_font_chooser_widget_find_font (GtkFontChooserWidget        *fontchooser,
984                                    const PangoFontDescription  *font_desc,
985                                    /* out arguments */
986                                    GtkTreeIter                 *iter)
987 {
988   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
989   PangoFontDescription *desc;
990   PangoFontFamily *family;
991   gboolean valid;
992
993   if (pango_font_description_get_family (font_desc) == NULL)
994     return FALSE;
995
996   for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
997        valid;
998        valid = gtk_tree_model_iter_next (priv->model, iter))
999     {
1000       gtk_tree_model_get (priv->model, iter,
1001                           FAMILY_COLUMN, &family,
1002                           -1);
1003
1004       if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
1005                                        pango_font_family_get_name (family)))
1006         continue;
1007
1008       desc = tree_model_get_font_description (priv->model, iter);
1009
1010       pango_font_description_merge_static (desc, font_desc, FALSE);
1011       if (pango_font_description_equal (desc, font_desc))
1012         break;
1013
1014       pango_font_description_free (desc);
1015     }
1016   
1017   return valid;
1018 }
1019
1020 static void
1021 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
1022                                         GdkScreen *previous_screen)
1023 {
1024   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
1025
1026   if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
1027     GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
1028
1029   if (previous_screen == NULL)
1030     previous_screen = gdk_screen_get_default ();
1031
1032   if (previous_screen == gtk_widget_get_screen (widget))
1033     return;
1034
1035   gtk_font_chooser_widget_load_fonts (fontchooser);
1036 }
1037
1038 static PangoFontFamily *
1039 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
1040 {
1041   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1042   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1043   PangoFontFamily *family;
1044
1045   if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1046     return NULL;
1047
1048   gtk_tree_model_get (priv->model, &priv->font_iter,
1049                       FAMILY_COLUMN, &family,
1050                       -1);
1051   g_object_unref (family);
1052
1053   return family;
1054 }
1055
1056 static PangoFontFace *
1057 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
1058 {
1059   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1060   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1061   PangoFontFace *face;
1062
1063   if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1064     return NULL;
1065
1066   gtk_tree_model_get (priv->model, &priv->font_iter,
1067                       FACE_COLUMN, &face,
1068                       -1);
1069   g_object_unref (face);
1070
1071   return face;
1072 }
1073
1074 static gint
1075 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1076 {
1077   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1078
1079   return pango_font_description_get_size (fontchooser->priv->font_desc);
1080 }
1081
1082 static gchar *
1083 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1084 {
1085   return pango_font_description_to_string (fontchooser->priv->font_desc);
1086 }
1087
1088 static PangoFontDescription *
1089 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1090 {
1091   return fontchooser->priv->font_desc;
1092 }
1093
1094 static void
1095 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1096                                   const gchar          *fontname)
1097 {
1098   PangoFontDescription *font_desc;
1099
1100   font_desc = pango_font_description_from_string (fontname);
1101   gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1102 }
1103
1104 static void
1105 gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
1106 {
1107   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1108   GtkTreeSelection *selection;
1109   GtkTreeIter filter_iter;
1110   
1111   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
1112
1113   if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
1114       gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1115                                                         &filter_iter,
1116                                                         &priv->font_iter))
1117     {
1118       gtk_tree_selection_select_iter (selection, &filter_iter);
1119     }
1120   else
1121     {
1122       gtk_tree_selection_unselect_all (selection);
1123     }
1124 }
1125
1126 static void
1127 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser,
1128                                          PangoFontDescription *font_desc,
1129                                          GtkTreeIter          *iter)
1130 {
1131   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1132   PangoFontMask mask;
1133
1134   g_assert (font_desc != NULL);
1135   /* iter may be NULL if the font doesn't exist on the list */
1136
1137   mask = pango_font_description_get_set_fields (font_desc);
1138
1139   /* sucky test, because we can't restrict the comparison to 
1140    * only the parts that actually do get merged */
1141   if (pango_font_description_equal (font_desc, priv->font_desc))
1142     {
1143       pango_font_description_free (font_desc);
1144       return;
1145     }
1146
1147   pango_font_description_merge (priv->font_desc, font_desc, TRUE);
1148   
1149   if (mask & PANGO_FONT_MASK_SIZE)
1150     {
1151       double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1152       /* XXX: This clamps, which can cause it to reloop into here, do we need
1153        * to block its signal handler? */
1154       gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1155       gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1156     }
1157   if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1158               PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1159     {
1160       if (&priv->font_iter != iter)
1161         {
1162           if (iter == NULL)
1163             memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1164           else
1165             memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
1166           
1167           gtk_font_chooser_widget_ensure_selection (fontchooser);
1168         }
1169
1170       gtk_font_chooser_widget_update_marks (fontchooser);
1171     }
1172
1173   gtk_widget_override_font (priv->preview, priv->font_desc);
1174
1175   pango_font_description_free (font_desc); /* adopted */
1176
1177   g_object_notify (G_OBJECT (fontchooser), "font");
1178   g_object_notify (G_OBJECT (fontchooser), "font-desc");
1179 }
1180
1181 static void
1182 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1183                                         PangoFontDescription *font_desc)
1184 {
1185   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1186   PangoFontMask mask;
1187
1188   if (font_desc == NULL)
1189     font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1190
1191   mask = pango_font_description_get_set_fields (font_desc);
1192   if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1193               PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1194     {
1195       GtkTreeIter iter;
1196
1197       if (gtk_font_chooser_widget_find_font (fontchooser,
1198                                              font_desc,
1199                                              &iter))
1200         {
1201           gtk_font_chooser_widget_merge_font_desc (fontchooser,
1202                                                    font_desc,
1203                                                    &iter);
1204         }
1205       else
1206         {
1207           gtk_font_chooser_widget_merge_font_desc (fontchooser,
1208                                                    font_desc,
1209                                                    NULL);
1210         }
1211     }
1212   else
1213     {
1214       gtk_font_chooser_widget_merge_font_desc (fontchooser,
1215                                                font_desc,
1216                                                &priv->font_iter);
1217                                                           
1218     }
1219 }
1220
1221 static const gchar*
1222 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1223 {
1224   return fontchooser->priv->preview_text;
1225 }
1226
1227 static void
1228 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1229                                           const gchar          *text)
1230 {
1231   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1232
1233   g_free (priv->preview_text);
1234   priv->preview_text = g_strdup (text);
1235
1236   gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1237
1238   g_object_notify (G_OBJECT (fontchooser), "preview-text");
1239
1240   /* XXX: There's no API to tell the treeview that a column has changed,
1241    * so we just */
1242   gtk_widget_queue_draw (priv->family_face_list);
1243 }
1244
1245 static gboolean
1246 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1247 {
1248   return fontchooser->priv->show_preview_entry;
1249 }
1250
1251 static void
1252 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1253                                                 gboolean              show_preview_entry)
1254 {
1255   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1256
1257   if (priv->show_preview_entry != show_preview_entry)
1258     {
1259       fontchooser->priv->show_preview_entry = show_preview_entry;
1260
1261       if (show_preview_entry)
1262         gtk_widget_show (fontchooser->priv->preview);
1263       else
1264         gtk_widget_hide (fontchooser->priv->preview);
1265
1266       g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1267     }
1268 }
1269
1270 static void
1271 gtk_font_chooser_widget_set_filter_func (GtkFontChooser  *chooser,
1272                                          GtkFontFilterFunc filter,
1273                                          gpointer          data,
1274                                          GDestroyNotify    destroy)
1275 {
1276   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1277   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1278
1279   if (priv->filter_data_destroy)
1280     priv->filter_data_destroy (priv->filter_data);
1281
1282   priv->filter_func = filter;
1283   priv->filter_data = data;
1284   priv->filter_data_destroy = destroy;
1285
1286   gtk_font_chooser_widget_refilter_font_list (fontchooser);
1287 }
1288
1289 static void
1290 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1291 {
1292   iface->get_font_family = gtk_font_chooser_widget_get_family;
1293   iface->get_font_face = gtk_font_chooser_widget_get_face;
1294   iface->get_font_size = gtk_font_chooser_widget_get_size;
1295   iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;
1296 }