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