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