]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserwidget.c
4ef5436fd1b9374eea70537d936d27300e3da6a5
[~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   GtkWidget    *list_scrolled_window;
82   GtkWidget    *empty_list;
83   GtkWidget    *list_notebook;
84   GtkTreeModel *model;
85   GtkTreeModel *filter_model;
86
87   GtkWidget       *preview;
88   gchar           *preview_text;
89   gboolean         show_preview_entry;
90
91   GtkWidget *size_spin;
92   GtkWidget *size_slider;
93
94   PangoFontDescription *font_desc;
95   GtkTreeIter           font_iter;      /* invalid if font not available or pointer into model
96                                            (not filter_model) to the row containing font */
97   PangoFontFace   *face;
98   PangoFontFamily *family;
99
100   GtkFontFilterFunc filter_func;
101   gpointer          filter_data;
102   GDestroyNotify    filter_data_destroy;
103 };
104
105 /* This is the initial fixed height and the top padding of the preview entry */
106 #define PREVIEW_HEIGHT 72
107 #define PREVIEW_TOP_PADDING 6
108
109 /* These are the sizes of the font, style & size lists. */
110 #define FONT_LIST_HEIGHT  136
111 #define FONT_LIST_WIDTH   190
112 #define FONT_STYLE_LIST_WIDTH 170
113 #define FONT_SIZE_LIST_WIDTH  60
114
115 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
116
117 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
118
119 /* These are what we use as the standard font sizes, for the size list.
120  */
121 static const gint font_sizes[] = {
122   6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
123 };
124
125 enum {
126   FAMILY_COLUMN,
127   FACE_COLUMN,
128   FONT_DESC_COLUMN,
129   PREVIEW_TITLE_COLUMN
130 };
131
132 static void gtk_font_chooser_widget_set_property         (GObject         *object,
133                                                           guint            prop_id,
134                                                           const GValue    *value,
135                                                           GParamSpec      *pspec);
136 static void gtk_font_chooser_widget_get_property         (GObject         *object,
137                                                           guint            prop_id,
138                                                           GValue          *value,
139                                                           GParamSpec      *pspec);
140 static void gtk_font_chooser_widget_finalize             (GObject         *object);
141
142 static void gtk_font_chooser_widget_screen_changed       (GtkWidget       *widget,
143                                                           GdkScreen       *previous_screen);
144
145 static void gtk_font_chooser_widget_bootstrap_fontlist   (GtkFontChooserWidget *fontchooser);
146
147 static void gtk_font_chooser_widget_select_font          (GtkFontChooserWidget *fontchooser);
148
149 static gchar   *gtk_font_chooser_widget_get_font         (GtkFontChooserWidget *fontchooser);
150 static void     gtk_font_chooser_widget_set_font         (GtkFontChooserWidget *fontchooser,
151                                                           const gchar          *fontname);
152
153 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc  (GtkFontChooserWidget *fontchooser);
154 static void                  gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
155                                                                      PangoFontDescription *font_desc);
156
157
158 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
159 static void         gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
160                                                               const gchar          *text);
161
162 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
163 static void     gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
164                                                                 gboolean              show_preview_entry);
165
166 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
167
168 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
169                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
170                                                 gtk_font_chooser_widget_iface_init))
171
172 static void
173 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
174 {
175   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
176   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
177
178   widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
179
180   gobject_class->finalize = gtk_font_chooser_widget_finalize;
181   gobject_class->set_property = gtk_font_chooser_widget_set_property;
182   gobject_class->get_property = gtk_font_chooser_widget_get_property;
183
184   _gtk_font_chooser_install_properties (gobject_class);
185
186   g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
187 }
188
189 static void
190 gtk_font_chooser_widget_set_property (GObject         *object,
191                                       guint            prop_id,
192                                       const GValue    *value,
193                                       GParamSpec      *pspec)
194 {
195   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
196
197   switch (prop_id)
198     {
199     case GTK_FONT_CHOOSER_PROP_FONT:
200       gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
201       break;
202     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
203       gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
204       break;
205     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
206       gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
207       break;
208     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
209       gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
210       break;
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214     }
215 }
216
217 static void
218 gtk_font_chooser_widget_get_property (GObject         *object,
219                                       guint            prop_id,
220                                       GValue          *value,
221                                       GParamSpec      *pspec)
222 {
223   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
224
225   switch (prop_id)
226     {
227     case GTK_FONT_CHOOSER_PROP_FONT:
228       g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
229       break;
230     case GTK_FONT_CHOOSER_PROP_FONT_DESC:
231       g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
232       break;
233     case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
234       g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
235       break;
236     case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
237       g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242     }
243 }
244
245 static void
246 text_changed_cb (GtkEntry       *entry,
247                  GParamSpec     *pspec,
248                  GtkFontChooserWidget *fc)
249 {
250   GtkFontChooserWidgetPrivate *priv = fc->priv;
251   const gchar *text;
252
253   text = gtk_entry_get_text (entry);
254
255   if (text == NULL || text[0] == '\0')
256     {
257       GIcon *icon;
258
259       icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
260       g_object_set (G_OBJECT (priv->search_entry),
261                     "secondary-icon-gicon", icon,
262                     "secondary-icon-activatable", FALSE,
263                     "secondary-icon-sensitive", FALSE,
264                     NULL);
265       g_object_unref (icon);
266     }
267   else
268     {
269       if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
270         {
271           GIcon *icon;
272
273           icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
274           g_object_set (G_OBJECT (priv->search_entry),
275                         "secondary-icon-gicon", icon,
276                         "secondary-icon-activatable", TRUE,
277                         "secondary-icon-sensitive", TRUE,
278                         NULL);
279           g_object_unref (icon);
280         }
281     }
282
283   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
284 }
285
286 static void
287 icon_press_cb (GtkEntry             *entry,
288                GtkEntryIconPosition  pos,
289                GdkEvent             *event,
290                gpointer              user_data)
291 {
292   gtk_entry_set_text (entry, "");
293 }
294
295 static void
296 size_change_cb (GtkAdjustment *adjustment,
297                 gpointer       user_data)
298 {
299   GtkFontChooserWidget *fontchooser = user_data;
300   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
301   PangoFontDescription *font_desc;
302   gdouble size = gtk_adjustment_get_value (adjustment);
303
304   font_desc = pango_font_description_new ();
305   if (pango_font_description_get_size_is_absolute (priv->font_desc))
306     pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
307   else
308     pango_font_description_set_size (font_desc, size * PANGO_SCALE);
309
310   gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
311 }
312
313 static void
314 set_range_marks (GtkFontChooserWidgetPrivate *priv,
315                  GtkWidget             *size_slider,
316                  gint                  *sizes,
317                  gint                   length)
318 {
319   GtkAdjustment *adj;
320   gint i;
321   gdouble value;
322
323   if (length < 2)
324     {
325       sizes = (gint*)font_sizes;
326       length = G_N_ELEMENTS (font_sizes);
327     }
328
329   gtk_scale_clear_marks (GTK_SCALE (size_slider));
330
331   adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
332
333   gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
334   gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
335
336   value = gtk_adjustment_get_value (adj);
337   if (value > (gdouble) sizes[length-1])
338     gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
339   else if (value < (gdouble) sizes[0])
340     gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
341
342   for (i = 0; i < length; i++)
343     gtk_scale_add_mark (GTK_SCALE (size_slider),
344                         (gdouble) sizes[i],
345                         GTK_POS_BOTTOM, NULL);
346 }
347
348 static void
349 row_activated_cb (GtkTreeView       *view,
350                   GtkTreePath       *path,
351                   GtkTreeViewColumn *column,
352                   gpointer           user_data)
353 {
354   GtkFontChooserWidget *fontchooser = user_data;
355   gchar *fontname;
356
357   fontname = gtk_font_chooser_widget_get_font (fontchooser);
358   _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
359   g_free (fontname);
360 }
361
362 static void
363 cursor_changed_cb (GtkTreeView *treeview,
364                    gpointer     user_data)
365 {
366   GtkFontChooserWidget *fontchooser = user_data;
367   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
368   PangoFontFamily      *family;
369   PangoFontFace        *face;
370   PangoFontDescription *desc;
371   gint *sizes;
372   gint  i, n_sizes;
373   GtkTreeIter  iter;
374   GtkTreePath *path = NULL;
375
376   gtk_tree_view_get_cursor (treeview, &path, NULL);
377
378   if (!path)
379     return;
380
381   if (!gtk_tree_model_get_iter (priv->filter_model, &iter, path))
382     {
383       gtk_tree_path_free (path);
384       return;
385     }
386
387   gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
388                                                     &priv->font_iter,
389                                                     &iter);
390   gtk_tree_model_get (priv->filter_model, &iter,
391                       FACE_COLUMN, &face,
392                       FAMILY_COLUMN, &family,
393                       FONT_DESC_COLUMN, &desc,
394                       -1);
395
396   gtk_tree_path_free (path);
397   path = NULL;
398
399   pango_font_description_set_size (desc, pango_font_description_get_size (priv->font_desc));
400   gtk_widget_override_font (priv->preview, desc);
401
402   pango_font_face_list_sizes (face, &sizes, &n_sizes);
403   /* It seems not many fonts actually have a sane set of sizes */
404   for (i = 0; i < n_sizes; i++)
405     sizes[i] = sizes[i] / PANGO_SCALE;
406
407   set_range_marks (priv, priv->size_slider, sizes, n_sizes);
408
409   if (priv->family)
410     g_object_unref (priv->family);
411   priv->family = family;
412
413   if (priv->face)
414     g_object_unref (priv->face);
415   priv->face = face;
416
417   if (priv->font_desc)
418     pango_font_description_free (priv->font_desc);
419   priv->font_desc = desc;
420
421   g_object_notify (G_OBJECT (fontchooser), "font");
422   g_object_notify (G_OBJECT (fontchooser), "font-desc");
423 }
424
425 static gboolean
426 zoom_preview_cb (GtkWidget      *scrolled_window,
427                  GdkEventScroll *event,
428                  gpointer        user_data)
429 {
430   GtkFontChooserWidget *fc = user_data;
431   GtkFontChooserWidgetPrivate *priv = fc->priv;
432   GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
433
434   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
435     gtk_adjustment_set_value (adj,
436                               gtk_adjustment_get_value (adj) +
437                               gtk_adjustment_get_step_increment (adj));
438   else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
439     gtk_adjustment_set_value (adj,
440                               gtk_adjustment_get_value (adj) -
441                               gtk_adjustment_get_step_increment (adj));
442   return TRUE;
443 }
444
445 static void
446 row_inserted_cb (GtkTreeModel *model,
447                  GtkTreePath  *path,
448                  GtkTreeIter  *iter,
449                  gpointer      user_data)
450 {
451   GtkFontChooserWidget *fontchooser = user_data;
452   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
453
454   gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
455 }
456
457 static void
458 row_deleted_cb  (GtkTreeModel *model,
459                  GtkTreePath  *path,
460                  gpointer      user_data)
461 {
462   GtkFontChooserWidget *fontchooser = user_data;
463   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
464
465   if (gtk_tree_model_iter_n_children (model, NULL) == 0)
466     gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
467 }
468
469 static void
470 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
471 {
472   GIcon *icon;
473   GtkFontChooserWidgetPrivate *priv;
474   GtkWidget *scrolled_win;
475   GtkWidget *grid;
476
477   fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
478                                                    GTK_TYPE_FONT_CHOOSER_WIDGET,
479                                                    GtkFontChooserWidgetPrivate);
480
481   priv = fontchooser->priv;
482
483   /* Default preview string  */
484   priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
485   priv->show_preview_entry = TRUE;
486   priv->font_desc = pango_font_description_new ();
487
488   gtk_widget_push_composite_child ();
489
490   /* Creating fundamental widgets for the private struct */
491   priv->search_entry = gtk_entry_new ();
492   priv->family_face_list = gtk_tree_view_new ();
493   priv->preview = gtk_entry_new ();
494   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
495                                                 (gdouble) font_sizes[0],
496                                                 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
497                                                 1.0);
498
499   priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
500
501   /** Bootstrapping widget layout **/
502   gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
503
504   /* Main font family/face view */
505   priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
506   scrolled_win = priv->list_scrolled_window;
507   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
508                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
509   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
510                                        GTK_SHADOW_ETCHED_IN);
511   gtk_widget_set_size_request (scrolled_win, 400, 300);
512   gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
513
514   /* Text to display when list is empty */
515   priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
516   gtk_widget_set_margin_top    (priv->empty_list, 12);
517   gtk_widget_set_margin_left   (priv->empty_list, 12);
518   gtk_widget_set_margin_right  (priv->empty_list, 12);
519   gtk_widget_set_margin_bottom (priv->empty_list, 12);
520   gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
521   gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
522
523   priv->list_notebook = gtk_notebook_new ();
524   gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
525   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
526   gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
527
528   /* Basic layout */
529   grid = gtk_grid_new ();
530
531   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
532   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
533
534   gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
535   gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
536   gtk_grid_attach (GTK_GRID (grid), priv->preview,      0, 2, 2, 1);
537
538   gtk_grid_attach (GTK_GRID (grid), priv->size_slider,  0, 3, 1, 1);
539   gtk_grid_attach (GTK_GRID (grid), priv->size_spin,    1, 3, 1, 1);
540
541   gtk_widget_set_hexpand  (GTK_WIDGET (scrolled_win),      TRUE);
542   gtk_widget_set_vexpand  (GTK_WIDGET (scrolled_win),      TRUE);
543   gtk_widget_set_hexpand  (GTK_WIDGET (priv->search_entry), TRUE);
544
545   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_slider), TRUE);
546   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_spin),   FALSE);
547
548   gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
549
550   gtk_widget_show_all (GTK_WIDGET (fontchooser));
551   gtk_widget_hide     (GTK_WIDGET (fontchooser));
552
553   /* Treeview column and model bootstrapping */
554   gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
555
556   /* Set default preview text */
557   gtk_entry_set_text (GTK_ENTRY (priv->preview),
558                       pango_language_get_sample_string (NULL));
559
560   /* Set search icon and place holder text */
561   icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
562   g_object_set (G_OBJECT (priv->search_entry),
563                 "secondary-icon-gicon", icon,
564                 "secondary-icon-activatable", FALSE,
565                 "secondary-icon-sensitive", FALSE,
566                 NULL);
567   g_object_unref (icon);
568
569   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
570
571   /** Callback connections **/
572   g_signal_connect (priv->search_entry, "notify::text",
573                     G_CALLBACK (text_changed_cb), fontchooser);
574   g_signal_connect (priv->search_entry,
575                     "icon-press", G_CALLBACK (icon_press_cb), NULL);
576
577   g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
578                     "value-changed", G_CALLBACK (size_change_cb), fontchooser);
579   g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
580                     "value-changed", G_CALLBACK (size_change_cb), fontchooser);
581
582   g_signal_connect (priv->family_face_list, "cursor-changed",
583                     G_CALLBACK (cursor_changed_cb), fontchooser);
584   g_signal_connect (priv->family_face_list, "row-activated",
585                     G_CALLBACK (row_activated_cb), fontchooser);
586
587   /* Zoom on preview scroll */
588   g_signal_connect (priv->preview, "scroll-event",
589                     G_CALLBACK (zoom_preview_cb), fontchooser);
590
591   g_signal_connect (priv->size_slider, "scroll-event",
592                     G_CALLBACK (zoom_preview_cb), fontchooser);
593
594   set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
595
596   /* Font list empty hides the scrolledwindow */
597   g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
598                     G_CALLBACK (row_deleted_cb), fontchooser);
599   g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
600                     G_CALLBACK (row_inserted_cb), fontchooser);
601
602   /* Set default focus */
603   gtk_widget_pop_composite_child ();
604
605   gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
606 }
607
608 /**
609  * gtk_font_chooser_widget_new:
610  *
611  * Creates a new #GtkFontChooserWidget.
612  *
613  * Return value: a new #GtkFontChooserWidget
614  *
615  * Since: 3.2
616  */
617 GtkWidget *
618 gtk_font_chooser_widget_new (void)
619 {
620   return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
621 }
622
623 static int
624 cmp_families (const void *a,
625               const void *b)
626 {
627   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
628   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
629
630   return g_utf8_collate (a_name, b_name);
631 }
632
633 static void
634 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
635 {
636   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
637   GtkListStore *list_store;
638   gint n_families, i;
639   PangoFontFamily **families;
640   gchar *family_and_face;
641
642   list_store = GTK_LIST_STORE (priv->model);
643
644   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
645                                &families,
646                                &n_families);
647
648   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
649
650   gtk_list_store_clear (list_store);
651   memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
652
653   /* Iterate over families and faces */
654   for (i = 0; i < n_families; i++)
655     {
656       GtkTreeIter     iter;
657       PangoFontFace **faces;
658       int             j, n_faces;
659       const gchar    *fam_name = pango_font_family_get_name (families[i]);
660
661       pango_font_family_list_faces (families[i], &faces, &n_faces);
662
663       for (j = 0; j < n_faces; j++)
664         {
665           PangoFontDescription *pango_desc;
666           const gchar *face_name;
667
668           pango_desc = pango_font_face_describe (faces[j]);
669           face_name = pango_font_face_get_face_name (faces[j]);
670
671           family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
672
673           gtk_list_store_insert_with_values (list_store, &iter, -1,
674                                              FAMILY_COLUMN, families[i],
675                                              FACE_COLUMN, faces[j],
676                                              FONT_DESC_COLUMN, pango_desc,
677                                              PREVIEW_TITLE_COLUMN, family_and_face,
678                                              -1);
679
680           pango_font_description_free (pango_desc);
681           g_free (family_and_face);
682         }
683
684       g_free (faces);
685     }
686
687   g_free (families);
688 }
689
690 static gboolean
691 visible_func (GtkTreeModel *model,
692               GtkTreeIter  *iter,
693               gpointer      user_data)
694 {
695   GtkFontChooserWidgetPrivate *priv = user_data;
696   gboolean result = TRUE;
697   const gchar *search_text;
698   gchar **split_terms;
699   gchar *font_name, *font_name_casefold;
700   guint i;
701
702   if (priv->filter_func != NULL)
703     {
704       PangoFontFamily *family;
705       PangoFontFace *face;
706
707       gtk_tree_model_get (model, iter,
708                           FAMILY_COLUMN, &family,
709                           FACE_COLUMN, &face,
710                           -1);
711
712       result = priv->filter_func (family, face, priv->filter_data);
713
714       g_object_unref (family);
715       g_object_unref (face);
716       
717       if (!result)
718         return FALSE;
719     }
720
721   /* If there's no filter string we show the item */
722   search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
723   if (strlen (search_text) == 0)
724     return TRUE;
725
726   gtk_tree_model_get (model, iter,
727                       PREVIEW_TITLE_COLUMN, &font_name,
728                       -1);
729
730   if (font_name == NULL)
731     return FALSE;
732
733   split_terms = g_strsplit (search_text, " ", 0);
734   font_name_casefold = g_utf8_casefold (font_name, -1);
735
736   for (i = 0; split_terms[i] && result; i++)
737     {
738       gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
739
740       if (!strstr (font_name_casefold, term_casefold))
741         result = FALSE;
742
743       g_free (term_casefold);
744     }
745
746   g_free (font_name_casefold);
747   g_free (font_name);
748   g_strfreev (split_terms);
749
750   return result;
751 }
752
753 static void
754 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
755                                         GtkCellRenderer   *cell,
756                                         GtkTreeModel      *tree_model,
757                                         GtkTreeIter       *iter,
758                                         gpointer           user_data)
759 {
760   GtkFontChooserWidget *fontchooser = user_data;
761   PangoFontDescription *font_desc;
762   char *to_string, *markup;
763
764   gtk_tree_model_get (tree_model, iter,
765                       FONT_DESC_COLUMN, &font_desc,
766                       -1);
767
768   to_string = pango_font_description_to_string (font_desc);
769
770   markup = g_markup_printf_escaped (ROW_FORMAT_STRING,
771                                     to_string,
772                                     to_string,
773                                     fontchooser->priv->preview_text);
774
775   g_object_set (cell,
776                 "markup", markup,
777                 NULL);
778
779   pango_font_description_free (font_desc);
780   g_free (to_string);
781   g_free (markup);
782 }
783
784 static void
785 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
786 {
787   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
788   GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
789   GtkCellRenderer *cell;
790   GtkTreeViewColumn *col;
791
792   priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
793                                                     PANGO_TYPE_FONT_FAMILY,
794                                                     PANGO_TYPE_FONT_FACE,
795                                                     PANGO_TYPE_FONT_DESCRIPTION,
796                                                     G_TYPE_STRING));
797
798   priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
799   g_object_unref (priv->model);
800
801   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
802                                           visible_func, (gpointer)priv, NULL);
803
804   gtk_tree_view_set_model (treeview, priv->filter_model);
805   g_object_unref (priv->filter_model);
806
807   gtk_tree_view_set_rules_hint      (treeview, TRUE);
808   gtk_tree_view_set_headers_visible (treeview, FALSE);
809
810   cell = gtk_cell_renderer_text_new ();
811   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
812
813   col = gtk_tree_view_column_new ();
814   gtk_tree_view_column_set_title (col, _("Font Family"));
815   gtk_tree_view_column_pack_start (col, cell, TRUE);
816   gtk_tree_view_column_set_cell_data_func (col,
817                                            cell,
818                                            gtk_font_chooser_widget_cell_data_func,
819                                            fontchooser,
820                                            NULL);
821
822   gtk_tree_view_append_column (treeview, col);
823
824   gtk_font_chooser_widget_load_fonts (fontchooser);
825 }
826
827 static void
828 gtk_font_chooser_widget_finalize (GObject *object)
829 {
830   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
831   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
832
833   if (priv->font_desc)
834     pango_font_description_free (priv->font_desc);
835
836   if (priv->family)
837     g_object_unref (priv->family);
838
839   if (priv->face)
840     g_object_unref (priv->face);
841
842   if (priv->filter_data_destroy)
843     priv->filter_data_destroy (priv->filter_data);
844
845   G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
846 }
847
848 static gboolean
849 gtk_font_chooser_widget_find_font (GtkFontChooserWidget        *fontchooser,
850                                    const PangoFontDescription  *font_desc,
851                                    /* out arguments */
852                                    GtkTreeIter                 *iter,
853                                    PangoFontFamily            **family,
854                                    PangoFontFace              **face)
855 {
856   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
857   PangoFontDescription *desc;
858   gboolean valid;
859
860   for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
861        valid;
862        valid = gtk_tree_model_iter_next (priv->model, iter))
863     {
864       gtk_tree_model_get (priv->model, iter,
865                           FACE_COLUMN, face,
866                           FAMILY_COLUMN, family,
867                           FONT_DESC_COLUMN, &desc,
868                           -1);
869
870       pango_font_description_merge_static (desc, font_desc, FALSE);
871       if (pango_font_description_equal (desc, font_desc))
872         break;
873
874       g_object_unref (face);
875       g_object_unref (family);
876       pango_font_description_free (desc);
877     }
878   
879   return valid;
880 }
881
882 static void
883 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
884                                         GdkScreen *previous_screen)
885 {
886   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
887
888   if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
889     GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
890
891   if (previous_screen == NULL)
892     previous_screen = gdk_screen_get_default ();
893
894   if (previous_screen == gtk_widget_get_screen (widget))
895     return;
896
897   gtk_font_chooser_widget_load_fonts (fontchooser);
898
899   gtk_font_chooser_widget_select_font (fontchooser);
900 }
901
902 static PangoFontFamily *
903 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
904 {
905   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
906
907   return fontchooser->priv->family;
908 }
909
910 static PangoFontFace *
911 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
912 {
913   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
914
915   return fontchooser->priv->face;
916 }
917
918 static gint
919 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
920 {
921   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
922
923   return pango_font_description_get_size (fontchooser->priv->font_desc);
924 }
925
926 static gchar *
927 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
928 {
929   return pango_font_description_to_string (fontchooser->priv->font_desc);
930 }
931
932 static PangoFontDescription *
933 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
934 {
935   return fontchooser->priv->font_desc;
936 }
937
938 static void
939 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
940                                   const gchar          *fontname)
941 {
942   PangoFontDescription *font_desc;
943
944   font_desc = pango_font_description_from_string (fontname);
945   gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
946 }
947
948 static void
949 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
950                                         PangoFontDescription *font_desc)
951 {
952   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
953   PangoFontMask mask;
954
955   if (font_desc && priv->font_desc &&
956       pango_font_description_equal (font_desc, priv->font_desc))
957     {
958       pango_font_description_free (font_desc);
959       return;
960     }
961
962   if (font_desc == NULL)
963     font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
964
965   pango_font_description_merge (priv->font_desc, font_desc, TRUE);
966
967   mask = pango_font_description_get_set_fields (font_desc);
968   
969   if (mask & PANGO_FONT_MASK_SIZE)
970     {
971       double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
972       /* XXX: This clamps, which can cause it to reloop into here, do we need
973        * to block its signal handler? */
974       gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
975       gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
976     }
977   if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
978               PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
979     gtk_font_chooser_widget_select_font (fontchooser);
980
981   gtk_widget_override_font (priv->preview, priv->font_desc);
982
983   pango_font_description_free (font_desc); /* adopted */
984
985   g_object_notify (G_OBJECT (fontchooser), "font");
986   g_object_notify (G_OBJECT (fontchooser), "font-desc");
987 }
988
989 static void
990 gtk_font_chooser_widget_select_font (GtkFontChooserWidget *fontchooser)
991 {
992   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
993   GtkTreeIter iter;
994
995   if (priv->family)
996     g_object_unref (priv->family);
997   if (priv->face)
998     g_object_unref (priv->face);
999
1000   if (gtk_font_chooser_widget_find_font (fontchooser,
1001                                          priv->font_desc,
1002                                          &priv->font_iter,
1003                                          &priv->family,
1004                                          &priv->face))
1005     {
1006       GtkTreeIter filter_iter;
1007
1008       if (gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1009                                                             &filter_iter,
1010                                                             &iter))
1011         {
1012           GtkTreePath *path = gtk_tree_model_get_path (priv->filter_model, &filter_iter);
1013           gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1014                                     path,
1015                                     NULL,
1016                                     FALSE);
1017           gtk_tree_path_free (path);
1018         }
1019     }
1020   else
1021     {
1022       gtk_tree_selection_unselect_all
1023         (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list)));
1024       priv->face = NULL;
1025       priv->family = NULL;
1026       memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1027     }
1028 }
1029
1030 static const gchar*
1031 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1032 {
1033   return fontchooser->priv->preview_text;
1034 }
1035
1036 static void
1037 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1038                                           const gchar          *text)
1039 {
1040   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1041
1042   g_free (priv->preview_text);
1043   priv->preview_text = g_strdup (text);
1044
1045   gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1046
1047   g_object_notify (G_OBJECT (fontchooser), "preview-text");
1048
1049   /* XXX: There's no API to tell the treeview that a column has changed,
1050    * so we just */
1051   gtk_widget_queue_draw (priv->family_face_list);
1052 }
1053
1054 static gboolean
1055 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1056 {
1057   return fontchooser->priv->show_preview_entry;
1058 }
1059
1060 static void
1061 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1062                                                 gboolean              show_preview_entry)
1063 {
1064   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1065
1066   if (priv->show_preview_entry != show_preview_entry)
1067     {
1068       fontchooser->priv->show_preview_entry = show_preview_entry;
1069
1070       if (show_preview_entry)
1071         gtk_widget_show (fontchooser->priv->preview);
1072       else
1073         gtk_widget_hide (fontchooser->priv->preview);
1074
1075       g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1076     }
1077 }
1078
1079 static void
1080 gtk_font_chooser_widget_set_filter_func (GtkFontChooser  *chooser,
1081                                          GtkFontFilterFunc filter,
1082                                          gpointer          data,
1083                                          GDestroyNotify    destroy)
1084 {
1085   GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1086   GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1087
1088   if (priv->filter_data_destroy)
1089     priv->filter_data_destroy (priv->filter_data);
1090
1091   priv->filter_func = filter;
1092   priv->filter_data = data;
1093   priv->filter_data_destroy = destroy;
1094
1095   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1096 }
1097
1098 static void
1099 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1100 {
1101   iface->get_font_family = gtk_font_chooser_widget_get_family;
1102   iface->get_font_face = gtk_font_chooser_widget_get_face;
1103   iface->get_font_size = gtk_font_chooser_widget_get_size;
1104   iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;
1105 }