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