]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
c14dee8570210fef550418b546b30f11d1521f37
[~andy/gtk] / gtk / gtkfontchooser.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 "gtkfontchooser.h"
29 #include "gtkcellrenderertext.h"
30 #include "gtkentry.h"
31 #include "gtkframe.h"
32 #include "gtkhbbox.h"
33 #include "gtkhbox.h"
34 #include "gtklabel.h"
35 #include "gtkliststore.h"
36 #include "gtkstock.h"
37 #include "gtktextview.h"
38 #include "gtktreeselection.h"
39 #include "gtktreeview.h"
40 #include "gtkbox.h"
41 #include "gtkscrolledwindow.h"
42 #include "gtkintl.h"
43 #include "gtkaccessible.h"
44 #include "gtkbuildable.h"
45 #include "gtkprivate.h"
46 #include "gtkalignment.h"
47 #include "gtkscale.h"
48 #include "gtkbox.h"
49 #include "gtkspinbutton.h"
50 #include "gtkwidget.h"
51 #include "gtkgrid.h"
52
53 /**
54  * SECTION:gtkfontchooser
55  * @Short_description: A widget for selecting fonts
56  * @Title: GtkFontChooser
57  * @See_also: #GtkFontChooserDialog
58  *
59  * The #GtkFontChooser widget lists the available fonts, styles and sizes,
60  * allowing the user to select a font.
61  * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
62  * selecting fonts.
63  *
64  * To set the font which is initially selected, use
65  * gtk_font_chooser_set_font_name().
66  *
67  * To get the selected font use gtk_font_chooser_get_font_name().
68  *
69  * To change the text which is shown in the preview area, use
70  * gtk_font_chooser_set_preview_text().
71  *
72  * Since: 3.2
73  */
74
75
76 struct _GtkFontChooserPrivate
77 {
78   GtkWidget    *search_entry;
79   GtkWidget    *family_face_list;
80   GtkWidget    *list_scrolled_window;
81   GtkWidget    *empty_list;
82   GtkListStore *model;
83   GtkTreeModel *filter;
84
85   GtkWidget       *preview;
86   gchar           *preview_text;
87   gboolean         show_preview_entry;
88
89   GtkWidget *size_spin;
90   GtkWidget *size_slider;
91
92   gint             size;
93   PangoFontFace   *face;
94   PangoFontFamily *family;
95
96   gulong           cursor_changed_handler;
97 };
98
99 #define DEFAULT_FONT_NAME "Sans 10"
100 #define MAX_FONT_SIZE 999
101
102 /* This is the initial fixed height and the top padding of the preview entry */
103 #define PREVIEW_HEIGHT 72
104 #define PREVIEW_TOP_PADDING 6
105
106 /* These are the sizes of the font, style & size lists. */
107 #define FONT_LIST_HEIGHT  136
108 #define FONT_LIST_WIDTH   190
109 #define FONT_STYLE_LIST_WIDTH 170
110 #define FONT_SIZE_LIST_WIDTH  60
111
112 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
113
114 #define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
115
116 /* These are what we use as the standard font sizes, for the size list.
117  */
118 static const gint font_sizes[] = {
119   6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
120 };
121
122 enum {
123    PROP_0,
124    PROP_FONT_NAME,
125    PROP_PREVIEW_TEXT,
126    PROP_SHOW_PREVIEW_ENTRY
127 };
128
129
130 enum {
131   FAMILY_COLUMN,
132   FACE_COLUMN,
133   PREVIEW_TEXT_COLUMN,
134   PREVIEW_TITLE_COLUMN
135 };
136
137 static void  gtk_font_chooser_set_property       (GObject         *object,
138                                                   guint            prop_id,
139                                                   const GValue    *value,
140                                                   GParamSpec      *pspec);
141 static void  gtk_font_chooser_get_property       (GObject         *object,
142                                                   guint            prop_id,
143                                                   GValue          *value,
144                                                   GParamSpec      *pspec);
145 static void  gtk_font_chooser_finalize           (GObject         *object);
146 static void  gtk_font_chooser_dispose            (GObject         *object);
147
148 static void  gtk_font_chooser_screen_changed     (GtkWidget       *widget,
149                                                   GdkScreen       *previous_screen);
150 static void  gtk_font_chooser_style_updated      (GtkWidget      *widget);
151
152 static void  gtk_font_chooser_ref_family         (GtkFontChooser *fontchooser,
153                                                   PangoFontFamily  *family);
154 static void  gtk_font_chooser_ref_face           (GtkFontChooser *fontchooser,
155                                                   PangoFontFace    *face);
156
157 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
158
159 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
160
161 static void
162 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
163 {
164   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
166
167   widget_class->screen_changed = gtk_font_chooser_screen_changed;
168   widget_class->style_updated = gtk_font_chooser_style_updated;
169
170   gobject_class->dispose = gtk_font_chooser_dispose;
171   gobject_class->finalize = gtk_font_chooser_finalize;
172   gobject_class->set_property = gtk_font_chooser_set_property;
173   gobject_class->get_property = gtk_font_chooser_get_property;
174
175   g_object_class_install_property (gobject_class,
176                                    PROP_FONT_NAME,
177                                    g_param_spec_string ("font-name",
178                                                         P_("Font name"),
179                                                         P_("The string that represents this font"),
180                                                         DEFAULT_FONT_NAME,
181                                                         GTK_PARAM_READWRITE));
182   g_object_class_install_property (gobject_class,
183                                    PROP_PREVIEW_TEXT,
184                                    g_param_spec_string ("preview-text",
185                                                         P_("Preview text"),
186                                                         P_("The text to display in order to demonstrate the selected font"),
187                                                         pango_language_get_sample_string (NULL),
188                                                         GTK_PARAM_READWRITE));
189
190   g_object_class_install_property (gobject_class,
191                                    PROP_SHOW_PREVIEW_ENTRY,
192                                    g_param_spec_boolean ("show-preview-entry",
193                                                         P_("Show preview text entry"),
194                                                         P_("Whether the preview text entry is shown or not"),
195                                                         TRUE,
196                                                         GTK_PARAM_READWRITE));
197
198   g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
199 }
200
201 static void
202 gtk_font_chooser_set_property (GObject         *object,
203                                guint            prop_id,
204                                const GValue    *value,
205                                GParamSpec      *pspec)
206 {
207   GtkFontChooser *fontchooser;
208
209   fontchooser = GTK_FONT_CHOOSER (object);
210
211   switch (prop_id)
212     {
213     case PROP_FONT_NAME:
214       gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
215       break;
216     case PROP_PREVIEW_TEXT:
217       gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
218       break;
219     case PROP_SHOW_PREVIEW_ENTRY:
220       gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
221       break;
222     default:
223       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224       break;
225     }
226 }
227
228 static void
229 gtk_font_chooser_get_property (GObject         *object,
230                                guint            prop_id,
231                                GValue          *value,
232                                GParamSpec      *pspec)
233 {
234   GtkFontChooser *fontchooser;
235
236   fontchooser = GTK_FONT_CHOOSER (object);
237
238   switch (prop_id)
239     {
240     case PROP_FONT_NAME:
241       g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
242       break;
243     case PROP_PREVIEW_TEXT:
244       g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
245       break;
246     case PROP_SHOW_PREVIEW_ENTRY:
247       g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252     }
253 }
254
255 static void
256 text_changed_cb (GtkEntry       *entry,
257                  GParamSpec     *pspec,
258                  GtkFontChooser *fc)
259 {
260   GtkFontChooserPrivate *priv = fc->priv;
261   const gchar *text;
262
263   text = gtk_entry_get_text (entry);
264
265   if (text == NULL || text[0] == '\0')
266     {
267       GIcon *icon;
268
269       icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
270       g_object_set (G_OBJECT (priv->search_entry),
271                     "secondary-icon-gicon", icon,
272                     "secondary-icon-activatable", FALSE,
273                     "secondary-icon-sensitive", FALSE,
274                     NULL);
275       g_object_unref (icon);
276     }
277   else
278     {
279       if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
280         {
281           GIcon *icon;
282
283           icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
284           g_object_set (G_OBJECT (priv->search_entry),
285                         "secondary-icon-gicon", icon,
286                         "secondary-icon-activatable", TRUE,
287                         "secondary-icon-sensitive", TRUE,
288                         NULL);
289           g_object_unref (icon);
290         }
291     }
292
293   gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
294 }
295
296 static void
297 icon_press_cb (GtkEntry             *entry,
298                GtkEntryIconPosition  pos,
299                GdkEvent             *event,
300                gpointer              user_data)
301 {
302   gtk_entry_set_text (entry, "");
303 }
304
305 static void
306 slider_change_cb (GtkAdjustment *adjustment,
307                   gpointer       user_data)
308 {
309   GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
310   GtkFontChooserPrivate *priv  = fc->priv;
311   GtkAdjustment         *spin_adj     = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
312   gdouble                slider_value = gtk_adjustment_get_value (adjustment);
313   gdouble                spin_value   = gtk_adjustment_get_value (spin_adj);
314
315   if (slider_value != spin_value)
316     gtk_adjustment_set_value (spin_adj,
317                               gtk_adjustment_get_value (adjustment));
318 }
319
320 static void
321 spin_change_cb (GtkAdjustment *adjustment,
322                 gpointer       user_data)
323 {
324   PangoFontDescription    *desc;
325   GtkFontChooser          *fontchooser = (GtkFontChooser*)user_data;
326   GtkFontChooserPrivate   *priv        = fontchooser->priv;
327   GtkAdjustment           *slider_adj  = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
328
329   gdouble size = gtk_adjustment_get_value (adjustment);
330   priv->size = ((gint)size) * PANGO_SCALE;
331
332   desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
333   pango_font_description_set_size (desc, priv->size);
334   gtk_widget_override_font (priv->preview, desc);
335
336   g_object_notify (G_OBJECT (fontchooser), "font-name");
337
338   /* If the new value is lower than the lower bound of the slider, we set
339    * the slider adjustment to the lower bound value if it is not already set
340    */
341   if (size < gtk_adjustment_get_lower (slider_adj) &&
342       gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
343     gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
344
345   /* If the new value is upper than the upper bound of the slider, we set
346    * the slider adjustment to the upper bound value if it is not already set
347    */
348   else if (size > gtk_adjustment_get_upper (slider_adj) &&
349            gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
350     gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
351
352   /* If the new value is not already set on the slider we set it */
353   else if (size != gtk_adjustment_get_value (slider_adj))
354     gtk_adjustment_set_value (slider_adj, size);
355
356   gtk_widget_queue_draw (priv->preview);
357 }
358
359 static void
360 set_range_marks (GtkFontChooserPrivate *priv,
361                  GtkWidget             *size_slider,
362                  gint                  *sizes,
363                  gint                   length)
364 {
365   GtkAdjustment *adj;
366   gint i;
367   gdouble value;
368
369   if (length < 2)
370     {
371       sizes = (gint*)font_sizes;
372       length = G_N_ELEMENTS (font_sizes);
373     }
374
375   gtk_scale_clear_marks (GTK_SCALE (size_slider));
376
377   adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
378
379   gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
380   gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
381
382   value = gtk_adjustment_get_value (adj);
383   if (value > (gdouble) sizes[length-1])
384     gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
385   else if (value < (gdouble) sizes[0])
386     gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
387
388   for (i = 0; i < length; i++)
389     gtk_scale_add_mark (GTK_SCALE (size_slider),
390                         (gdouble) sizes[i],
391                         GTK_POS_BOTTOM, NULL);
392 }
393
394 static void
395 cursor_changed_cb (GtkTreeView *treeview,
396                    gpointer     user_data)
397 {
398   PangoFontFamily      *family;
399   PangoFontFace        *face;
400   PangoFontDescription *desc;
401
402   gint *sizes;
403   gint  i, n_sizes;
404
405   GtkTreeIter  iter;
406   GtkTreePath *path = gtk_tree_path_new ();
407
408   GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
409
410   gtk_tree_view_get_cursor (treeview, &path, NULL);
411
412   if (!path)
413     return;
414
415   if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
416     {
417       gtk_tree_path_free (path);
418       return;
419     }
420
421
422   gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
423                       FACE_COLUMN, &face,
424                       FAMILY_COLUMN, &family,
425                       -1);
426
427   gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
428
429   gtk_tree_path_free (path);
430   path = NULL;
431
432   if (!face || !family)
433     {
434       g_object_unref (face);
435       g_object_unref (family);
436       return;
437     }
438
439   desc = pango_font_face_describe (face);
440   pango_font_description_set_size (desc, fontchooser->priv->size);
441   gtk_widget_override_font (fontchooser->priv->preview, desc);
442
443   pango_font_face_list_sizes (face, &sizes, &n_sizes);
444   /* It seems not many fonts actually have a sane set of sizes */
445   for (i = 0; i < n_sizes; i++)
446     sizes[i] = sizes[i] / PANGO_SCALE;
447
448   set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
449
450   gtk_font_chooser_ref_family (fontchooser, family);
451   gtk_font_chooser_ref_face   (fontchooser, face);
452
453   /* Free resources */
454   g_object_unref ((gpointer)family);
455   g_object_unref ((gpointer)face);
456   pango_font_description_free(desc);
457
458   g_object_notify (G_OBJECT (fontchooser), "font-name");
459 }
460
461 static gboolean
462 zoom_preview_cb (GtkWidget      *scrolled_window,
463                  GdkEventScroll *event,
464                  gpointer        user_data)
465 {
466   GtkFontChooser        *fc    = (GtkFontChooser*)user_data;
467   GtkFontChooserPrivate *priv  = fc->priv;
468
469   GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
470
471   if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
472     gtk_adjustment_set_value (adj,
473                               gtk_adjustment_get_value (adj) +
474                               gtk_adjustment_get_step_increment (adj));
475   else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
476     gtk_adjustment_set_value (adj,
477                               gtk_adjustment_get_value (adj) -
478                               gtk_adjustment_get_step_increment (adj));
479   return TRUE;
480 }
481
482 #if 0
483 static void
484 row_inserted_cb (GtkTreeModel *model,
485                  GtkTreePath  *path,
486                  GtkTreeIter  *iter,
487                  gpointer      user_data)
488 {
489   GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
490   GtkFontChooserPrivate *priv        = fontchooser->priv;
491
492   if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
493       priv->empty_list)
494     {
495       g_object_ref (priv->empty_list);
496       gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
497                             priv->empty_list);
498       gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
499                          priv->family_face_list);
500     }
501 }
502
503 static void
504 row_deleted_cb  (GtkTreeModel *model,
505                  GtkTreePath  *path,
506                  gpointer      user_data)
507 {
508   GtkFontChooser        *fontchooser = (GtkFontChooser*)user_data;
509   GtkFontChooserPrivate *priv        = fontchooser->priv;
510
511   if (gtk_tree_model_iter_n_children (model, NULL) == 0)
512     {
513       if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
514           priv->family_face_list)
515         {
516           g_object_ref (priv->family_face_list);
517           gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
518                                 priv->family_face_list);
519           gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
520                              priv->empty_list);
521         }
522     }
523 }
524 #endif
525
526 static void
527 gtk_font_chooser_init (GtkFontChooser *fontchooser)
528 {
529   GIcon                   *icon;
530   GtkFontChooserPrivate   *priv;
531   PangoFontDescription    *font_desc;
532   GtkWidget               *scrolled_win;
533   GtkWidget               *grid;
534   GtkWidget               *sub_grid;
535
536   fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
537                                                    GTK_TYPE_FONT_CHOOSER,
538                                                    GtkFontChooserPrivate);
539
540   priv = fontchooser->priv;
541
542   /* Default preview string  */
543   priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
544   priv->show_preview_entry = TRUE;
545
546   /* Getting the default size */
547   font_desc  = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
548   priv->size = pango_font_description_get_size (font_desc);
549   priv->face = NULL;
550   priv->family = NULL;
551
552   gtk_widget_push_composite_child ();
553
554   /* Creating fundamental widgets for the private struct */
555   priv->search_entry = gtk_entry_new ();
556   priv->family_face_list = gtk_tree_view_new ();
557   priv->preview = gtk_entry_new ();
558   priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
559                                                 (gdouble) font_sizes[0],
560                                                 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
561                                                 1.0);
562
563   priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
564
565   /** Bootstrapping widget layout **/
566   gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
567
568   /* Main font family/face view */
569   priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
570   scrolled_win = priv->list_scrolled_window;
571   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
572                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
573   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
574                                        GTK_SHADOW_ETCHED_IN);
575   gtk_container_add (GTK_CONTAINER (scrolled_win),
576                      priv->family_face_list);
577
578   /* Text to display when list is empty */
579   priv->empty_list = gtk_text_view_new ();
580   gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->empty_list)),
581                             _(NO_FONT_MATCHED_SEARCH),
582                             -1);
583
584   /* Basic layout */
585   grid = gtk_grid_new ();
586   sub_grid = gtk_grid_new ();
587
588   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
589   gtk_grid_set_row_spacing (GTK_GRID (sub_grid), 6);
590
591   gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
592   gtk_grid_attach (GTK_GRID (grid), scrolled_win,       0, 1, 3, 1);
593   gtk_grid_attach (GTK_GRID (grid), priv->preview,      0, 2, 3, 1);
594   gtk_grid_attach (GTK_GRID (grid), sub_grid,           0, 4, 3, 1);
595
596   gtk_widget_set_hexpand  (GTK_WIDGET (sub_grid),          TRUE);
597   gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider,  0, 3, 2, 1);
598   gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin,    2, 3, 1, 1);
599
600   gtk_widget_set_hexpand  (GTK_WIDGET (scrolled_win),      TRUE);
601   gtk_widget_set_vexpand  (GTK_WIDGET (scrolled_win),      TRUE);
602   gtk_widget_set_hexpand  (GTK_WIDGET (priv->search_entry), TRUE);
603
604   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_slider), TRUE);
605   gtk_widget_set_hexpand  (GTK_WIDGET (priv->size_spin),   FALSE);
606
607   gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
608   gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
609
610   /* Setting the adjustment values for the size slider */
611   gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
612                             (gdouble)(priv->size / PANGO_SCALE));
613   gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
614                             (gdouble)(priv->size / PANGO_SCALE));
615
616   gtk_widget_show_all (GTK_WIDGET (fontchooser));
617   gtk_widget_hide     (GTK_WIDGET (fontchooser));
618
619   /* Treeview column and model bootstrapping */
620   gtk_font_chooser_bootstrap_fontlist (fontchooser);
621
622   /* Set default preview text */
623   gtk_entry_set_text (GTK_ENTRY (priv->preview),
624                       pango_language_get_sample_string (NULL));
625
626   /* Set search icon and place holder text */
627   icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
628   g_object_set (G_OBJECT (priv->search_entry),
629                 "secondary-icon-gicon", icon,
630                 "secondary-icon-activatable", FALSE,
631                 "secondary-icon-sensitive", FALSE,
632                 NULL);
633   g_object_unref (icon);
634
635   gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
636
637   /** Callback connections **/
638   g_signal_connect (priv->search_entry, "notify::text",
639                     G_CALLBACK (text_changed_cb), fontchooser);
640   g_signal_connect (priv->search_entry,
641                     "icon-press", G_CALLBACK (icon_press_cb), NULL);
642
643   g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
644                     "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
645   g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
646                     "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
647
648   priv->cursor_changed_handler =
649       g_signal_connect (priv->family_face_list, "cursor-changed",
650                         G_CALLBACK (cursor_changed_cb), fontchooser);
651
652   /* Zoom on preview scroll*/
653   g_signal_connect (priv->preview, "scroll-event",
654                     G_CALLBACK (zoom_preview_cb), fontchooser);
655
656   g_signal_connect (priv->size_slider, "scroll-event",
657                     G_CALLBACK (zoom_preview_cb), fontchooser);
658
659   set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
660
661   /* Font list empty hides the scrolledwindow */
662   /*
663   g_signal_connect (G_OBJECT (priv->filter), "row-deleted",
664                     G_CALLBACK (row_deleted_cb), fontchooser);
665   g_signal_connect (G_OBJECT (priv->filter), "row-inserted",
666                     G_CALLBACK (row_inserted_cb), fontchooser);
667   */
668
669   /* Set default focus */
670   gtk_widget_pop_composite_child ();
671 }
672
673 /**
674  * gtk_font_chooser_new:
675  *
676  * Creates a new #GtkFontChooser.
677  *
678  * Return value: a new #GtkFontChooser
679  *
680  * Since: 3.2
681  */
682 GtkWidget *
683 gtk_font_chooser_new (void)
684 {
685   GtkFontChooser *fontchooser;
686
687   fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
688
689   return GTK_WIDGET (fontchooser);
690 }
691
692 static int
693 cmp_families (const void *a,
694               const void *b)
695 {
696   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
697   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
698
699   return g_utf8_collate (a_name, b_name);
700 }
701
702 static void
703 populate_list (GtkFontChooser *fontchooser,
704                GtkTreeView    *treeview,
705                GtkListStore   *model)
706 {
707   GtkStyleContext      *style_context;
708   PangoFontDescription *default_font;
709
710   GtkTreeIter   match_row;
711   GtkTreePath  *path;
712
713   gint n_families, i;
714   PangoFontFamily **families;
715   GString     *tmp;
716   GString     *family_and_face;
717
718   if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
719     return;
720
721   tmp = g_string_new (NULL);
722   family_and_face = g_string_new (NULL);
723
724   pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
725                                &families,
726                                &n_families);
727
728   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
729
730   gtk_list_store_clear (model);
731
732   /* Get row header font color */
733   style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
734
735   /* Get theme font */
736   default_font  = (PangoFontDescription*) gtk_style_context_get_font (style_context,
737                                                                       GTK_STATE_NORMAL);
738
739   /* Iterate over families and faces */
740   for (i = 0; i < n_families; i++)
741     {
742       GtkTreeIter     iter;
743       PangoFontFace **faces;
744
745       int             j, n_faces;
746       const gchar    *fam_name = pango_font_family_get_name (families[i]);
747
748       pango_font_family_list_faces (families[i], &faces, &n_faces);
749
750       for (j = 0; j < n_faces; j++)
751         {
752           PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
753           const gchar *face_name = pango_font_face_get_face_name (faces[j]);
754           gchar       *font_desc = pango_font_description_to_string (pango_desc);
755
756           /* foreground_color, family_name, face_name, desc, sample string */
757           g_string_printf (family_and_face, "%s %s", fam_name, face_name);
758
759           g_string_printf (tmp, ROW_FORMAT_STRING,
760                            family_and_face->str,
761                            font_desc,
762                            fontchooser->priv->preview_text);
763
764           gtk_list_store_append (model, &iter);
765           gtk_list_store_set (model, &iter,
766                               FAMILY_COLUMN, families[i],
767                               FACE_COLUMN, faces[j],
768                               PREVIEW_TITLE_COLUMN, family_and_face->str,
769                               PREVIEW_TEXT_COLUMN, tmp->str,
770                               -1);
771
772           /* Select the first font or the default font/face from the style context */
773           if ((i == 0 && j == 0) ||
774               (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
775             match_row = iter;
776
777           pango_font_description_free(pango_desc);
778           g_free (font_desc);
779         }
780
781       g_free (faces);
782     }
783
784   path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
785
786   if (path)
787     {
788       gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
789       gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
790       gtk_tree_path_free(path);
791     }
792
793   g_string_free (family_and_face, TRUE);
794   g_string_free (tmp, TRUE);
795   g_free (families);
796 }
797
798 static gboolean
799 visible_func (GtkTreeModel *model,
800               GtkTreeIter  *iter,
801               gpointer      user_data)
802 {
803   gboolean result = TRUE;
804   GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
805
806   const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
807   gchar       *font_name;
808   gchar       *term;
809   gchar      **split_terms;
810   gint         n_terms = 0;
811
812   /* If there's no filter string we show the item */
813   if (strlen (search_text) == 0)
814     return TRUE;
815
816   gtk_tree_model_get (model, iter,
817                       PREVIEW_TITLE_COLUMN, &font_name,
818                       -1);
819
820   if (font_name == NULL)
821     return FALSE;
822
823   split_terms = g_strsplit (search_text, " ", 0);
824   term = split_terms[0];
825
826   while (term && result)
827   {
828     gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
829     gchar* term_casefold = g_utf8_casefold (term, -1);
830
831     if (g_strrstr (font_name_casefold, term_casefold))
832       result = result && TRUE;
833     else
834       result = FALSE;
835
836     n_terms++;
837     term = split_terms[n_terms];
838
839     g_free (term_casefold);
840     g_free (font_name_casefold);
841   }
842
843   g_free (font_name);
844   g_strfreev (split_terms);
845
846   return result;
847 }
848
849 static void
850 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
851 {
852   GtkTreeView       *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
853   GtkCellRenderer   *cell;
854   GtkTreeViewColumn *col;
855
856   fontchooser->priv->model = gtk_list_store_new (4,
857                                                  PANGO_TYPE_FONT_FAMILY,
858                                                  PANGO_TYPE_FONT_FACE,
859                                                  G_TYPE_STRING,
860                                                  G_TYPE_STRING);
861
862   fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
863                                                          NULL);
864   g_object_unref (fontchooser->priv->model);
865
866   gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
867                                           visible_func,
868                                           (gpointer)fontchooser->priv,
869                                           NULL);
870
871   gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
872   g_object_unref (fontchooser->priv->filter);
873
874   gtk_tree_view_set_rules_hint      (treeview, TRUE);
875   gtk_tree_view_set_headers_visible (treeview, FALSE);
876
877   cell = gtk_cell_renderer_text_new ();
878   col = gtk_tree_view_column_new_with_attributes ("Family",
879                                                   cell,
880                                                   "markup", PREVIEW_TEXT_COLUMN,
881                                                   NULL);
882
883   g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
884
885   gtk_tree_view_append_column (treeview, col);
886
887   populate_list (fontchooser, treeview, fontchooser->priv->model);
888 }
889
890 static void
891 gtk_font_chooser_dispose (GObject *object)
892 {
893   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
894   GtkFontChooserPrivate *priv = fontchooser->priv;
895
896   if (priv->cursor_changed_handler != 0)
897     {
898       g_signal_handler_disconnect (priv->family_face_list,
899                                    priv->cursor_changed_handler);
900       priv->cursor_changed_handler = 0;
901     }
902
903   G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
904 }
905
906 static void
907 gtk_font_chooser_finalize (GObject *object)
908 {
909   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
910
911   gtk_font_chooser_ref_family (fontchooser, NULL);
912   gtk_font_chooser_ref_face (fontchooser, NULL);
913
914   G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
915 }
916
917 static void
918 gtk_font_chooser_screen_changed (GtkWidget *widget,
919                                  GdkScreen *previous_screen)
920 {
921   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
922
923   populate_list (fontchooser,
924                  GTK_TREE_VIEW (fontchooser->priv->family_face_list),
925                  fontchooser->priv->model);
926 }
927
928 static void
929 gtk_font_chooser_style_updated (GtkWidget *widget)
930 {
931   GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
932
933   GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
934
935   populate_list (fontchooser,
936                  GTK_TREE_VIEW (fontchooser->priv->family_face_list),
937                  fontchooser->priv->model);
938 }
939
940 static void
941 gtk_font_chooser_ref_family (GtkFontChooser  *fontchooser,
942                              PangoFontFamily *family)
943 {
944   GtkFontChooserPrivate *priv = fontchooser->priv;
945
946   if (family)
947     family = g_object_ref (family);
948   if (priv->family)
949     g_object_unref (priv->family);
950   priv->family = family;
951 }
952
953 static void
954 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
955                            PangoFontFace  *face)
956 {
957   GtkFontChooserPrivate *priv = fontchooser->priv;
958
959   if (face)
960     face = g_object_ref (face);
961   if (priv->face)
962     g_object_unref (priv->face);
963   priv->face = face;
964 }
965
966 /**
967  * gtk_font_chooser_get_family:
968  * @fontchooser: a #GtkFontChooser
969  *
970  * Gets the #PangoFontFamily representing the selected font family.
971  * Font families are a collection of font faces.
972  *
973  * Return value: (transfer none): A #PangoFontFamily representing the
974  *     selected font family. The returned object is owned by @fontchooser
975  *     and must not be modified or freed.
976  *
977  * Since: 3.2
978  */
979 PangoFontFamily *
980 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
981 {
982   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
983
984   return fontchooser->priv->family;
985 }
986
987 /**
988  * gtk_font_chooser_get_face:
989  * @fontchooser: a #GtkFontChooser
990  *
991  * Gets the #PangoFontFace representing the selected font group
992  * details (i.e. family, slant, weight, width, etc).
993  *
994  * Return value: (transfer none): A #PangoFontFace representing the
995  *     selected font group details. The returned object is owned by
996  *     @fontchooser and must not be modified or freed.
997  *
998  * Since: 3.2
999  */
1000 PangoFontFace *
1001 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1002 {
1003   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1004
1005   return fontchooser->priv->face;
1006 }
1007
1008 /**
1009  * gtk_font_chooser_get_size:
1010  * @fontchooser: a #GtkFontChooser
1011  *
1012  * The selected font size.
1013  *
1014  * Return value: A n integer representing the selected font size,
1015  *     or -1 if no font size is selected.
1016  *
1017  * Since: 3.2
1018  **/
1019 gint
1020 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1021 {
1022   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1023
1024   return fontchooser->priv->size;
1025 }
1026
1027 /**
1028  * gtk_font_chooser_get_font_name:
1029  * @fontchooser: a #GtkFontChooser
1030  *
1031  * Gets the currently-selected font name.
1032  *
1033  * Note that this can be a different string than what you set with
1034  * gtk_font_chooser_set_font_name(), as the font chooser widget may
1035  * normalize font names and thus return a string with a different
1036  * structure. For example, "Helvetica Italic Bold 12" could be
1037  * normalized to "Helvetica Bold Italic 12".
1038  *
1039  * Use pango_font_description_equal() if you want to compare two
1040  * font descriptions.
1041  *
1042  * Return value: (transfer full) (allow-none): A string with the name
1043  *     of the current font, or %NULL if  no font is selected. You must
1044  *     free this string with g_free().
1045  *
1046  * Since: 3.2
1047  */
1048 gchar *
1049 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1050 {
1051   gchar                *font_name;
1052   gchar                *font_desc_name;
1053   PangoFontDescription *desc;
1054
1055   if (!fontchooser->priv->face)
1056     return NULL;
1057
1058   desc = pango_font_face_describe (fontchooser->priv->face);
1059   font_desc_name = pango_font_description_to_string (desc);
1060   pango_font_description_free (desc);
1061
1062   font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1063   g_free (font_desc_name);
1064   return font_name;
1065 }
1066
1067 /**
1068  * gtk_font_chooser_set_font_name:
1069  * @fontchooser: a #GtkFontChooser
1070  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1071  *
1072  * Sets the currently-selected font.
1073  *
1074  * Note that the @fontchooser needs to know the screen in which
1075  * it will appear for this to work; this can be guaranteed by simply
1076  * making sure that the @fontchooser is inserted in a toplevel window
1077  * before you call this function.
1078  *
1079  * Return value: %TRUE if the font could be set successfully; %FALSE
1080  *     if no such font exists or if the @fontchooser doesn't belong
1081  *     to a particular screen yet.
1082  *
1083  * Since: 3.2
1084  */
1085 gboolean
1086 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1087                                 const gchar    *fontname)
1088 {
1089   GtkFontChooserPrivate *priv = fontchooser->priv;
1090   GtkTreeIter           iter;
1091   gboolean              valid;
1092   gchar                *family_name;
1093   PangoFontDescription *desc;
1094   gboolean              found = FALSE;
1095
1096   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1097   g_return_val_if_fail (fontname != NULL, FALSE);
1098
1099   if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1100     return FALSE;
1101
1102   desc = pango_font_description_from_string (fontname);
1103   family_name = (gchar*)pango_font_description_get_family (desc);
1104
1105   if (!family_name)
1106     {
1107       pango_font_description_free (desc);
1108       return FALSE;
1109     }
1110
1111   /* We make sure the filter is clear */
1112   gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1113
1114   /* We find the matching family/face */
1115   for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1116        valid;
1117        valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1118     {
1119       PangoFontFace        *face;
1120       PangoFontDescription *tmp_desc;
1121
1122       gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1123                           FACE_COLUMN,   &face,
1124                           -1);
1125
1126       tmp_desc = pango_font_face_describe (face);
1127       if (pango_font_description_get_size_is_absolute (desc))
1128         pango_font_description_set_absolute_size (tmp_desc,
1129                                                   pango_font_description_get_size (desc));
1130       else
1131         pango_font_description_set_size (tmp_desc,
1132                                          pango_font_description_get_size (desc));
1133
1134       if (pango_font_description_equal (desc, tmp_desc))
1135         {
1136           GtkTreePath *path;
1137           gint size = pango_font_description_get_size (desc);
1138
1139           if (size)
1140             {
1141               if (pango_font_description_get_size_is_absolute (desc))
1142                 size = size * PANGO_SCALE;
1143               gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1144                                          size / PANGO_SCALE);
1145             }
1146
1147           path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1148                                           &iter);
1149
1150           if (path)
1151             {
1152               gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1153                                         path,
1154                                         NULL,
1155                                         FALSE);
1156               gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1157                                             path,
1158                                             NULL,
1159                                             FALSE,
1160                                             0.5,
1161                                             0.5);
1162               gtk_tree_path_free (path);
1163             }
1164
1165           found = TRUE;
1166         }
1167
1168       g_object_unref (face);
1169       pango_font_description_free (tmp_desc);
1170
1171       if (found)
1172         break;
1173     }
1174
1175   pango_font_description_free (desc);
1176   g_object_notify (G_OBJECT (fontchooser), "font-name");
1177
1178   return found;
1179 }
1180
1181 /**
1182  * gtk_font_chooser_get_preview_text:
1183  * @fontchooser: a #GtkFontChooser
1184  *
1185  * Gets the text displayed in the preview area.
1186  *
1187  * Return value: (transfer none): the text displayed in the
1188  *     preview area. This string is owned by the widget and
1189  *     should not be modified or freed
1190  *
1191  * Since: 3.2
1192  */
1193 const gchar*
1194 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1195 {
1196   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1197
1198   return (const gchar*)fontchooser->priv->preview_text;
1199 }
1200
1201
1202 /**
1203  * gtk_font_chooser_set_preview_text:
1204  * @fontchooser: a #GtkFontChooser
1205  * @text: (transfer none): the text to display in the preview area
1206  *
1207  * Sets the text displayed in the preview area.
1208  * The @text is used to show how the selected font looks.
1209  *
1210  * Since: 3.2
1211  */
1212 void
1213 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1214                                    const gchar    *text)
1215 {
1216   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1217   g_return_if_fail (text != NULL);
1218
1219   g_free (fontchooser->priv->preview_text);
1220   fontchooser->priv->preview_text = g_strdup (text);
1221
1222   populate_list (fontchooser,
1223                  GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1224                  fontchooser->priv->model);
1225
1226   gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1227
1228   g_object_notify (G_OBJECT (fontchooser), "preview-text");
1229 }
1230
1231 /**
1232  * gtk_font_chooser_get_show_preview_entry:
1233  * @fontchooser: a #GtkFontChooser
1234  *
1235  * Returns whether the preview entry is shown or not.
1236  *
1237  * Return value: %TRUE if the preview entry is shown
1238  *     or %FALSE if it is hidden.
1239  *
1240  * Since: 3.2
1241  */
1242 gboolean
1243 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1244 {
1245   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1246
1247   return fontchooser->priv->show_preview_entry;
1248 }
1249
1250 /**
1251  * gtk_font_chooser_set_show_preview_entry:
1252  * @fontchooser: a #GtkFontChooser
1253  * @show_preview_entry: whether to show the editable preview entry or not
1254  *
1255  * Shows or hides the editable preview entry.
1256  *
1257  * Since: 3.2
1258  */
1259 void
1260 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1261                                          gboolean        show_preview_entry)
1262 {
1263   GtkFontChooserPrivate *priv = fontchooser->priv;
1264
1265   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1266
1267   if (priv->show_preview_entry != show_preview_entry)
1268     {
1269       fontchooser->priv->show_preview_entry = show_preview_entry;
1270
1271       if (show_preview_entry)
1272         gtk_widget_show (fontchooser->priv->preview);
1273       else
1274         gtk_widget_hide (fontchooser->priv->preview);
1275
1276       g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1277     }
1278 }