1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
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.
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.
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.
23 #include <glib/gprintf.h>
28 #include "gtkfontchooser.h"
29 #include "gtkcellrenderertext.h"
35 #include "gtkliststore.h"
37 #include "gtktextview.h"
38 #include "gtktreeselection.h"
39 #include "gtktreeview.h"
41 #include "gtkscrolledwindow.h"
43 #include "gtkaccessible.h"
44 #include "gtkbuildable.h"
45 #include "gtkprivate.h"
46 #include "gtkalignment.h"
49 #include "gtkspinbutton.h"
50 #include "gtknotebook.h"
51 #include "gtkwidget.h"
55 * SECTION:gtkfontchooser
56 * @Short_description: A widget for selecting fonts
57 * @Title: GtkFontChooser
58 * @See_also: #GtkFontChooserDialog
60 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
61 * allowing the user to select a font.
62 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
65 * To set the font which is initially selected, use
66 * gtk_font_chooser_set_font_name().
68 * To get the selected font use gtk_font_chooser_get_font_name().
70 * To change the text which is shown in the preview area, use
71 * gtk_font_chooser_set_preview_text().
77 struct _GtkFontChooserPrivate
79 GtkWidget *search_entry;
80 GtkWidget *family_face_list;
81 GtkWidget *list_scrolled_window;
82 GtkWidget *empty_list;
83 GtkWidget *list_notebook;
85 GtkTreeModel *filter_model;
89 gboolean show_preview_entry;
92 GtkWidget *size_slider;
96 PangoFontFamily *family;
98 gulong cursor_changed_handler;
100 GtkFontFilterFunc filter_func;
101 gpointer filter_data;
102 GDestroyNotify filter_data_destroy;
105 #define DEFAULT_FONT_NAME "Sans 10"
106 #define MAX_FONT_SIZE 999
108 /* This is the initial fixed height and the top padding of the preview entry */
109 #define PREVIEW_HEIGHT 72
110 #define PREVIEW_TOP_PADDING 6
112 /* These are the sizes of the font, style & size lists. */
113 #define FONT_LIST_HEIGHT 136
114 #define FONT_LIST_WIDTH 190
115 #define FONT_STYLE_LIST_WIDTH 170
116 #define FONT_SIZE_LIST_WIDTH 60
118 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
120 #define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
122 /* These are what we use as the standard font sizes, for the size list.
124 static const gint font_sizes[] = {
125 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
132 PROP_SHOW_PREVIEW_ENTRY
143 static void gtk_font_chooser_set_property (GObject *object,
147 static void gtk_font_chooser_get_property (GObject *object,
151 static void gtk_font_chooser_finalize (GObject *object);
152 static void gtk_font_chooser_dispose (GObject *object);
154 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
155 GdkScreen *previous_screen);
156 static void gtk_font_chooser_style_updated (GtkWidget *widget);
158 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
160 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
163 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
166 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
168 widget_class->screen_changed = gtk_font_chooser_screen_changed;
169 widget_class->style_updated = gtk_font_chooser_style_updated;
171 gobject_class->dispose = gtk_font_chooser_dispose;
172 gobject_class->finalize = gtk_font_chooser_finalize;
173 gobject_class->set_property = gtk_font_chooser_set_property;
174 gobject_class->get_property = gtk_font_chooser_get_property;
176 g_object_class_install_property (gobject_class,
178 g_param_spec_string ("font-name",
180 P_("The string that represents this font"),
182 GTK_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
185 g_param_spec_string ("preview-text",
187 P_("The text to display in order to demonstrate the selected font"),
188 pango_language_get_sample_string (NULL),
189 GTK_PARAM_READWRITE));
191 g_object_class_install_property (gobject_class,
192 PROP_SHOW_PREVIEW_ENTRY,
193 g_param_spec_boolean ("show-preview-entry",
194 P_("Show preview text entry"),
195 P_("Whether the preview text entry is shown or not"),
197 GTK_PARAM_READWRITE));
199 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
203 gtk_font_chooser_set_property (GObject *object,
208 GtkFontChooser *fontchooser;
210 fontchooser = GTK_FONT_CHOOSER (object);
215 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
217 case PROP_PREVIEW_TEXT:
218 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
220 case PROP_SHOW_PREVIEW_ENTRY:
221 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gtk_font_chooser_get_property (GObject *object,
235 GtkFontChooser *fontchooser;
237 fontchooser = GTK_FONT_CHOOSER (object);
242 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
244 case PROP_PREVIEW_TEXT:
245 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
247 case PROP_SHOW_PREVIEW_ENTRY:
248 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 text_changed_cb (GtkEntry *entry,
261 GtkFontChooserPrivate *priv = fc->priv;
264 text = gtk_entry_get_text (entry);
266 if (text == NULL || text[0] == '\0')
270 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
271 g_object_set (G_OBJECT (priv->search_entry),
272 "secondary-icon-gicon", icon,
273 "secondary-icon-activatable", FALSE,
274 "secondary-icon-sensitive", FALSE,
276 g_object_unref (icon);
280 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
284 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
285 g_object_set (G_OBJECT (priv->search_entry),
286 "secondary-icon-gicon", icon,
287 "secondary-icon-activatable", TRUE,
288 "secondary-icon-sensitive", TRUE,
290 g_object_unref (icon);
294 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
298 icon_press_cb (GtkEntry *entry,
299 GtkEntryIconPosition pos,
303 gtk_entry_set_text (entry, "");
307 slider_change_cb (GtkAdjustment *adjustment,
310 GtkFontChooser *fc = (GtkFontChooser*)user_data;
311 GtkFontChooserPrivate *priv = fc->priv;
312 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
313 gdouble slider_value = gtk_adjustment_get_value (adjustment);
314 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
316 if (slider_value != spin_value)
317 gtk_adjustment_set_value (spin_adj,
318 gtk_adjustment_get_value (adjustment));
322 spin_change_cb (GtkAdjustment *adjustment,
325 PangoFontDescription *desc;
326 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
327 GtkFontChooserPrivate *priv = fontchooser->priv;
328 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
330 gdouble size = gtk_adjustment_get_value (adjustment);
331 priv->size = ((gint)size) * PANGO_SCALE;
333 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
334 pango_font_description_set_size (desc, priv->size);
335 gtk_widget_override_font (priv->preview, desc);
337 g_object_notify (G_OBJECT (fontchooser), "font-name");
339 /* If the new value is lower than the lower bound of the slider, we set
340 * the slider adjustment to the lower bound value if it is not already set
342 if (size < gtk_adjustment_get_lower (slider_adj) &&
343 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
344 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
346 /* If the new value is upper than the upper bound of the slider, we set
347 * the slider adjustment to the upper bound value if it is not already set
349 else if (size > gtk_adjustment_get_upper (slider_adj) &&
350 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
351 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
353 /* If the new value is not already set on the slider we set it */
354 else if (size != gtk_adjustment_get_value (slider_adj))
355 gtk_adjustment_set_value (slider_adj, size);
357 gtk_widget_queue_draw (priv->preview);
361 set_range_marks (GtkFontChooserPrivate *priv,
362 GtkWidget *size_slider,
372 sizes = (gint*)font_sizes;
373 length = G_N_ELEMENTS (font_sizes);
376 gtk_scale_clear_marks (GTK_SCALE (size_slider));
378 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
380 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
381 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
383 value = gtk_adjustment_get_value (adj);
384 if (value > (gdouble) sizes[length-1])
385 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
386 else if (value < (gdouble) sizes[0])
387 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
389 for (i = 0; i < length; i++)
390 gtk_scale_add_mark (GTK_SCALE (size_slider),
392 GTK_POS_BOTTOM, NULL);
396 cursor_changed_cb (GtkTreeView *treeview,
399 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
400 GtkFontChooserPrivate *priv = fontchooser->priv;
402 PangoFontFamily *family;
404 PangoFontDescription *desc;
410 GtkTreePath *path = gtk_tree_path_new ();
412 gtk_tree_view_get_cursor (treeview, &path, NULL);
417 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
419 gtk_tree_path_free (path);
424 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
426 FAMILY_COLUMN, &family,
429 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
431 gtk_tree_path_free (path);
434 if (face == NULL || family == NULL)
437 g_object_unref (face);
439 g_object_unref (family);
444 desc = pango_font_face_describe (face);
445 pango_font_description_set_size (desc, priv->size);
446 gtk_widget_override_font (priv->preview, desc);
448 pango_font_face_list_sizes (face, &sizes, &n_sizes);
449 /* It seems not many fonts actually have a sane set of sizes */
450 for (i = 0; i < n_sizes; i++)
451 sizes[i] = sizes[i] / PANGO_SCALE;
453 set_range_marks (priv, priv->size_slider, sizes, n_sizes);
456 g_object_unref (priv->family);
457 priv->family = family;
460 g_object_unref (priv->face);
463 pango_font_description_free (desc);
465 g_object_notify (G_OBJECT (fontchooser), "font-name");
469 zoom_preview_cb (GtkWidget *scrolled_window,
470 GdkEventScroll *event,
473 GtkFontChooser *fc = (GtkFontChooser*)user_data;
474 GtkFontChooserPrivate *priv = fc->priv;
476 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
478 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
479 gtk_adjustment_set_value (adj,
480 gtk_adjustment_get_value (adj) +
481 gtk_adjustment_get_step_increment (adj));
482 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
483 gtk_adjustment_set_value (adj,
484 gtk_adjustment_get_value (adj) -
485 gtk_adjustment_get_step_increment (adj));
490 row_inserted_cb (GtkTreeModel *model,
495 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
496 GtkFontChooserPrivate *priv = fontchooser->priv;
498 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
502 row_deleted_cb (GtkTreeModel *model,
506 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
507 GtkFontChooserPrivate *priv = fontchooser->priv;
509 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
510 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
514 gtk_font_chooser_init (GtkFontChooser *fontchooser)
517 GtkFontChooserPrivate *priv;
518 PangoFontDescription *font_desc;
519 GtkWidget *scrolled_win;
522 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
523 GTK_TYPE_FONT_CHOOSER,
524 GtkFontChooserPrivate);
526 priv = fontchooser->priv;
528 /* Default preview string */
529 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
530 priv->show_preview_entry = TRUE;
532 /* Getting the default size */
533 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
534 priv->size = pango_font_description_get_size (font_desc);
538 gtk_widget_push_composite_child ();
540 /* Creating fundamental widgets for the private struct */
541 priv->search_entry = gtk_entry_new ();
542 priv->family_face_list = gtk_tree_view_new ();
543 priv->preview = gtk_entry_new ();
544 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
545 (gdouble) font_sizes[0],
546 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
549 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
551 /** Bootstrapping widget layout **/
552 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
554 /* Main font family/face view */
555 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
556 scrolled_win = priv->list_scrolled_window;
557 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
558 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
559 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
560 GTK_SHADOW_ETCHED_IN);
561 gtk_widget_set_size_request (scrolled_win, 400, 300);
562 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
564 /* Text to display when list is empty */
565 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
566 gtk_widget_set_margin_top (priv->empty_list, 12);
567 gtk_widget_set_margin_left (priv->empty_list, 12);
568 gtk_widget_set_margin_right (priv->empty_list, 12);
569 gtk_widget_set_margin_bottom (priv->empty_list, 12);
570 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
571 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
573 priv->list_notebook = gtk_notebook_new ();
574 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
575 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
576 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
579 grid = gtk_grid_new ();
581 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
582 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
584 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
585 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
586 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
588 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
589 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
591 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
592 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
593 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
595 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
596 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
598 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
600 /* Setting the adjustment values for the size slider */
601 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
602 (gdouble)(priv->size / PANGO_SCALE));
603 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
604 (gdouble)(priv->size / PANGO_SCALE));
606 gtk_widget_show_all (GTK_WIDGET (fontchooser));
607 gtk_widget_hide (GTK_WIDGET (fontchooser));
609 /* Treeview column and model bootstrapping */
610 gtk_font_chooser_bootstrap_fontlist (fontchooser);
612 /* Set default preview text */
613 gtk_entry_set_text (GTK_ENTRY (priv->preview),
614 pango_language_get_sample_string (NULL));
616 /* Set search icon and place holder text */
617 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
618 g_object_set (G_OBJECT (priv->search_entry),
619 "secondary-icon-gicon", icon,
620 "secondary-icon-activatable", FALSE,
621 "secondary-icon-sensitive", FALSE,
623 g_object_unref (icon);
625 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
627 /** Callback connections **/
628 g_signal_connect (priv->search_entry, "notify::text",
629 G_CALLBACK (text_changed_cb), fontchooser);
630 g_signal_connect (priv->search_entry,
631 "icon-press", G_CALLBACK (icon_press_cb), NULL);
633 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
634 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
635 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
636 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
638 priv->cursor_changed_handler =
639 g_signal_connect (priv->family_face_list, "cursor-changed",
640 G_CALLBACK (cursor_changed_cb), fontchooser);
642 /* Zoom on preview scroll*/
643 g_signal_connect (priv->preview, "scroll-event",
644 G_CALLBACK (zoom_preview_cb), fontchooser);
646 g_signal_connect (priv->size_slider, "scroll-event",
647 G_CALLBACK (zoom_preview_cb), fontchooser);
649 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
651 /* Font list empty hides the scrolledwindow */
652 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
653 G_CALLBACK (row_deleted_cb), fontchooser);
654 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
655 G_CALLBACK (row_inserted_cb), fontchooser);
657 /* Set default focus */
658 gtk_widget_pop_composite_child ();
662 * gtk_font_chooser_new:
664 * Creates a new #GtkFontChooser.
666 * Return value: a new #GtkFontChooser
671 gtk_font_chooser_new (void)
673 GtkFontChooser *fontchooser;
675 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
677 return GTK_WIDGET (fontchooser);
681 cmp_families (const void *a,
684 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
685 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
687 return g_utf8_collate (a_name, b_name);
691 populate_list (GtkFontChooser *fontchooser,
692 GtkTreeView *treeview,
695 GtkFontChooserPrivate *priv = fontchooser->priv;
696 GtkStyleContext *style_context;
697 PangoFontDescription *default_font;
698 PangoFontDescription *selected_font;
701 GtkTreeIter match_row;
705 PangoFontFamily **families;
707 GString *family_and_face;
709 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
712 tmp = g_string_new (NULL);
713 family_and_face = g_string_new (NULL);
715 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
719 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
722 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
723 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
727 selected_font = pango_font_face_describe (priv->face);
729 selected_font = NULL;
731 gtk_list_store_clear (model);
735 /* Iterate over families and faces */
736 for (i = 0; i < n_families; i++)
739 PangoFontFace **faces;
741 const gchar *fam_name = pango_font_family_get_name (families[i]);
743 pango_font_family_list_faces (families[i], &faces, &n_faces);
745 for (j = 0; j < n_faces; j++)
747 PangoFontDescription *pango_desc;
748 const gchar *face_name;
751 if (priv->filter_func != NULL &&
752 !priv->filter_func (families[i], faces[j], priv->filter_data))
755 pango_desc = pango_font_face_describe (faces[j]);
756 face_name = pango_font_face_get_face_name (faces[j]);
757 font_desc = pango_font_description_to_string (pango_desc);
759 g_string_printf (family_and_face, "%s %s", fam_name, face_name);
760 g_string_printf (tmp, ROW_FORMAT_STRING,
761 family_and_face->str,
763 fontchooser->priv->preview_text);
765 gtk_list_store_append (model, &iter);
766 gtk_list_store_set (model, &iter,
767 FAMILY_COLUMN, families[i],
768 FACE_COLUMN, faces[j],
769 PREVIEW_TITLE_COLUMN, family_and_face->str,
770 PREVIEW_TEXT_COLUMN, tmp->str,
773 /* Select the current font,
774 * the default font/face from the theme,
778 selected_font != NULL &&
779 pango_font_description_equal (selected_font, pango_desc))
785 strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
796 pango_font_description_free (pango_desc);
803 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
806 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
807 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
808 gtk_tree_path_free (path);
812 pango_font_description_free (selected_font);
814 g_string_free (family_and_face, TRUE);
815 g_string_free (tmp, TRUE);
820 visible_func (GtkTreeModel *model,
824 gboolean result = TRUE;
825 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
827 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
833 /* If there's no filter string we show the item */
834 if (strlen (search_text) == 0)
837 gtk_tree_model_get (model, iter,
838 PREVIEW_TITLE_COLUMN, &font_name,
841 if (font_name == NULL)
844 split_terms = g_strsplit (search_text, " ", 0);
845 term = split_terms[0];
847 while (term && result)
849 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
850 gchar* term_casefold = g_utf8_casefold (term, -1);
852 if (g_strrstr (font_name_casefold, term_casefold))
853 result = result && TRUE;
858 term = split_terms[n_terms];
860 g_free (term_casefold);
861 g_free (font_name_casefold);
865 g_strfreev (split_terms);
871 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
873 GtkFontChooserPrivate *priv = fontchooser->priv;
874 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
875 GtkCellRenderer *cell;
876 GtkTreeViewColumn *col;
878 priv->model = gtk_list_store_new (4,
879 PANGO_TYPE_FONT_FAMILY,
880 PANGO_TYPE_FONT_FACE,
884 priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
885 g_object_unref (priv->model);
887 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
888 visible_func, (gpointer)priv, NULL);
890 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
891 g_object_unref (priv->filter_model);
893 gtk_tree_view_set_rules_hint (treeview, TRUE);
894 gtk_tree_view_set_headers_visible (treeview, FALSE);
896 cell = gtk_cell_renderer_text_new ();
897 col = gtk_tree_view_column_new_with_attributes ("Family",
899 "markup", PREVIEW_TEXT_COLUMN,
902 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
904 gtk_tree_view_append_column (treeview, col);
906 populate_list (fontchooser, treeview, priv->model);
910 gtk_font_chooser_dispose (GObject *object)
912 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
913 GtkFontChooserPrivate *priv = fontchooser->priv;
915 if (priv->cursor_changed_handler != 0)
917 g_signal_handler_disconnect (priv->family_face_list,
918 priv->cursor_changed_handler);
919 priv->cursor_changed_handler = 0;
922 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
926 gtk_font_chooser_finalize (GObject *object)
928 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
929 GtkFontChooserPrivate *priv = fontchooser->priv;
932 g_object_unref (priv->family);
935 g_object_unref (priv->face);
937 if (priv->filter_data_destroy)
938 priv->filter_data_destroy (priv->filter_data);
940 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
944 gtk_font_chooser_screen_changed (GtkWidget *widget,
945 GdkScreen *previous_screen)
947 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
949 populate_list (fontchooser,
950 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
951 fontchooser->priv->model);
955 gtk_font_chooser_style_updated (GtkWidget *widget)
957 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
959 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
961 populate_list (fontchooser,
962 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
963 fontchooser->priv->model);
967 * gtk_font_chooser_get_family:
968 * @fontchooser: a #GtkFontChooser
970 * Gets the #PangoFontFamily representing the selected font family.
971 * Font families are a collection of font faces.
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.
980 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
982 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
984 return fontchooser->priv->family;
988 * gtk_font_chooser_get_face:
989 * @fontchooser: a #GtkFontChooser
991 * Gets the #PangoFontFace representing the selected font group
992 * details (i.e. family, slant, weight, width, etc).
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.
1001 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1003 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1005 return fontchooser->priv->face;
1009 * gtk_font_chooser_get_size:
1010 * @fontchooser: a #GtkFontChooser
1012 * The selected font size.
1014 * Return value: A n integer representing the selected font size,
1015 * or -1 if no font size is selected.
1020 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1022 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1024 return fontchooser->priv->size;
1028 * gtk_font_chooser_get_font_name:
1029 * @fontchooser: a #GtkFontChooser
1031 * Gets the currently-selected font name.
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".
1039 * Use pango_font_description_equal() if you want to compare two
1040 * font descriptions.
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().
1049 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1052 gchar *font_desc_name;
1053 PangoFontDescription *desc;
1055 if (!fontchooser->priv->face)
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);
1062 font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1063 g_free (font_desc_name);
1068 * gtk_font_chooser_set_font_name:
1069 * @fontchooser: a #GtkFontChooser
1070 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1072 * Sets the currently-selected font.
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.
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.
1086 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1087 const gchar *fontname)
1089 GtkFontChooserPrivate *priv = fontchooser->priv;
1093 PangoFontDescription *desc;
1094 gboolean found = FALSE;
1096 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1097 g_return_val_if_fail (fontname != NULL, FALSE);
1099 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1102 desc = pango_font_description_from_string (fontname);
1103 family_name = (gchar*)pango_font_description_get_family (desc);
1107 pango_font_description_free (desc);
1111 /* We make sure the filter is clear */
1112 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1114 /* We find the matching family/face */
1115 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter_model), &iter);
1117 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter_model), &iter))
1119 PangoFontFace *face;
1120 PangoFontDescription *tmp_desc;
1122 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
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));
1131 pango_font_description_set_size (tmp_desc,
1132 pango_font_description_get_size (desc));
1134 if (pango_font_description_equal (desc, tmp_desc))
1137 gint size = pango_font_description_get_size (desc);
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);
1147 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model), &iter);
1151 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1155 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1161 gtk_tree_path_free (path);
1167 g_object_unref (face);
1168 pango_font_description_free (tmp_desc);
1174 pango_font_description_free (desc);
1175 g_object_notify (G_OBJECT (fontchooser), "font-name");
1181 * gtk_font_chooser_get_preview_text:
1182 * @fontchooser: a #GtkFontChooser
1184 * Gets the text displayed in the preview area.
1186 * Return value: (transfer none): the text displayed in the
1187 * preview area. This string is owned by the widget and
1188 * should not be modified or freed
1193 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1195 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1197 return (const gchar*)fontchooser->priv->preview_text;
1202 * gtk_font_chooser_set_preview_text:
1203 * @fontchooser: a #GtkFontChooser
1204 * @text: (transfer none): the text to display in the preview area
1206 * Sets the text displayed in the preview area.
1207 * The @text is used to show how the selected font looks.
1212 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1215 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1216 g_return_if_fail (text != NULL);
1218 g_free (fontchooser->priv->preview_text);
1219 fontchooser->priv->preview_text = g_strdup (text);
1221 populate_list (fontchooser,
1222 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1223 fontchooser->priv->model);
1225 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1227 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1231 * gtk_font_chooser_get_show_preview_entry:
1232 * @fontchooser: a #GtkFontChooser
1234 * Returns whether the preview entry is shown or not.
1236 * Return value: %TRUE if the preview entry is shown
1237 * or %FALSE if it is hidden.
1242 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1244 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1246 return fontchooser->priv->show_preview_entry;
1250 * gtk_font_chooser_set_show_preview_entry:
1251 * @fontchooser: a #GtkFontChooser
1252 * @show_preview_entry: whether to show the editable preview entry or not
1254 * Shows or hides the editable preview entry.
1259 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1260 gboolean show_preview_entry)
1262 GtkFontChooserPrivate *priv = fontchooser->priv;
1264 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1266 if (priv->show_preview_entry != show_preview_entry)
1268 fontchooser->priv->show_preview_entry = show_preview_entry;
1270 if (show_preview_entry)
1271 gtk_widget_show (fontchooser->priv->preview);
1273 gtk_widget_hide (fontchooser->priv->preview);
1275 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1280 * gtk_font_chooser_set_filter_func:
1281 * @fontchooser: a #GtkFontChooser
1282 * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
1283 * @data: data to pass to @filter
1284 * @destroy: function to call to free @data when it is no longer needed
1286 * Adds a filter function that decides which fonts to display
1287 * in the font chooser.
1292 gtk_font_chooser_set_filter_func (GtkFontChooser *fontchooser,
1293 GtkFontFilterFunc filter,
1295 GDestroyNotify destroy)
1297 GtkFontChooserPrivate *priv = fontchooser->priv;
1299 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1301 if (priv->filter_data_destroy)
1302 priv->filter_data_destroy (priv->filter_data);
1304 priv->filter_func = filter;
1305 priv->filter_data = data;
1306 priv->filter_data_destroy = destroy;
1308 populate_list (fontchooser,
1309 GTK_TREE_VIEW (priv->family_face_list),