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