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, see <http://www.gnu.org/licenses/>.
21 #include <glib/gprintf.h>
26 #include "gtkfontchooserwidget.h"
28 #include "gtkadjustment.h"
29 #include "gtkbuildable.h"
31 #include "gtkcellrenderertext.h"
34 #include "gtkfontchooser.h"
35 #include "gtkfontchooserutils.h"
38 #include "gtkliststore.h"
39 #include "gtknotebook.h"
40 #include "gtkprivate.h"
42 #include "gtkscrolledwindow.h"
43 #include "gtkspinbutton.h"
44 #include "gtktextview.h"
45 #include "gtktreeselection.h"
46 #include "gtktreeview.h"
47 #include "gtkwidget.h"
50 * SECTION:gtkfontchooserwidget
51 * @Short_description: A widget for selecting fonts
52 * @Title: GtkFontChooserWidget
53 * @See_also: #GtkFontChooserDialog
55 * The #GtkFontChooserWidget widget lists the available fonts,
56 * styles and sizes, allowing the user to select a font. It is
57 * used in the #GtkFontChooserDialog widget to provide a
58 * dialog box for selecting fonts.
60 * To set the font which is initially selected, use
61 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
63 * To get the selected font use gtk_font_chooser_get_font() or
64 * gtk_font_chooser_get_font_desc().
66 * To change the text which is shown in the preview area, use
67 * gtk_font_chooser_set_preview_text().
73 struct _GtkFontChooserWidgetPrivate
75 GtkWidget *search_entry;
76 GtkWidget *family_face_list;
77 GtkCellRenderer *family_face_cell;
78 GtkWidget *list_scrolled_window;
79 GtkWidget *empty_list;
80 GtkWidget *list_notebook;
82 GtkTreeModel *filter_model;
86 gboolean show_preview_entry;
89 GtkWidget *size_slider;
91 PangoFontDescription *font_desc;
92 GtkTreeIter font_iter; /* invalid if font not available or pointer into model
93 (not filter_model) to the row containing font */
94 GtkFontFilterFunc filter_func;
96 GDestroyNotify filter_data_destroy;
99 /* This is the initial fixed height and the top padding of the preview entry */
100 #define PREVIEW_HEIGHT 72
101 #define PREVIEW_TOP_PADDING 6
103 /* These are the sizes of the font, style & size lists. */
104 #define FONT_LIST_HEIGHT 136
105 #define FONT_LIST_WIDTH 190
106 #define FONT_STYLE_LIST_WIDTH 170
107 #define FONT_SIZE_LIST_WIDTH 60
109 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
118 static void gtk_font_chooser_widget_set_property (GObject *object,
122 static void gtk_font_chooser_widget_get_property (GObject *object,
126 static void gtk_font_chooser_widget_finalize (GObject *object);
128 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
129 GdkScreen *previous_screen);
131 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
133 static gboolean gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
134 const PangoFontDescription *font_desc,
136 static void gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
138 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
139 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
140 const gchar *fontname);
142 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
143 static void gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget *fontchooser,
144 PangoFontDescription *font_desc,
146 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
147 PangoFontDescription *font_desc);
150 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
151 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
154 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
155 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
156 gboolean show_preview_entry);
158 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
160 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
162 gtk_font_chooser_widget_iface_init))
165 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
168 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
170 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
172 gobject_class->finalize = gtk_font_chooser_widget_finalize;
173 gobject_class->set_property = gtk_font_chooser_widget_set_property;
174 gobject_class->get_property = gtk_font_chooser_widget_get_property;
176 _gtk_font_chooser_install_properties (gobject_class);
178 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
182 gtk_font_chooser_widget_set_property (GObject *object,
187 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
191 case GTK_FONT_CHOOSER_PROP_FONT:
192 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
194 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
195 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
197 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
198 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
200 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
201 gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gtk_font_chooser_widget_get_property (GObject *object,
215 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
219 case GTK_FONT_CHOOSER_PROP_FONT:
220 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
222 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
223 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
225 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
226 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
228 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
229 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
240 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
241 gtk_font_chooser_widget_ensure_selection (fontchooser);
245 text_changed_cb (GtkEntry *entry,
247 GtkFontChooserWidget *fc)
249 GtkFontChooserWidgetPrivate *priv = fc->priv;
252 text = gtk_entry_get_text (entry);
254 if (text == NULL || text[0] == '\0')
258 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
259 g_object_set (G_OBJECT (priv->search_entry),
260 "secondary-icon-gicon", icon,
261 "secondary-icon-activatable", FALSE,
262 "secondary-icon-sensitive", FALSE,
264 g_object_unref (icon);
268 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
272 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
273 g_object_set (G_OBJECT (priv->search_entry),
274 "secondary-icon-gicon", icon,
275 "secondary-icon-activatable", TRUE,
276 "secondary-icon-sensitive", TRUE,
278 g_object_unref (icon);
282 gtk_font_chooser_widget_refilter_font_list (fc);
286 icon_press_cb (GtkEntry *entry,
287 GtkEntryIconPosition pos,
291 gtk_entry_set_text (entry, "");
295 size_change_cb (GtkAdjustment *adjustment,
298 GtkFontChooserWidget *fontchooser = user_data;
299 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
300 PangoFontDescription *font_desc;
301 gdouble size = gtk_adjustment_get_value (adjustment);
303 font_desc = pango_font_description_new ();
304 if (pango_font_description_get_size_is_absolute (priv->font_desc))
305 pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
307 pango_font_description_set_size (font_desc, size * PANGO_SCALE);
309 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
313 gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
315 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
321 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
325 gtk_tree_model_get (priv->model, &priv->font_iter,
329 pango_font_face_list_sizes (face, &font_sizes, &n_sizes);
331 /* It seems not many fonts actually have a sane set of sizes */
332 for (i = 0; i < n_sizes; i++)
333 font_sizes[i] = font_sizes[i] / PANGO_SCALE;
335 g_object_unref (face);
345 static const gint fallback_sizes[] = {
346 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
349 sizes = fallback_sizes;
350 n_sizes = G_N_ELEMENTS (fallback_sizes);
357 gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
359 adj = gtk_range_get_adjustment(GTK_RANGE (priv->size_slider));
361 /* ensure clamping doesn't callback into font resizing code */
362 g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
363 gtk_adjustment_configure (adj,
364 gtk_adjustment_get_value (adj),
367 gtk_adjustment_get_step_increment (adj),
368 gtk_adjustment_get_page_increment (adj),
369 gtk_adjustment_get_page_size (adj));
370 g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);
372 for (i = 0; i < n_sizes; i++)
374 gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
376 GTK_POS_BOTTOM, NULL);
383 row_activated_cb (GtkTreeView *view,
385 GtkTreeViewColumn *column,
388 GtkFontChooserWidget *fontchooser = user_data;
391 fontname = gtk_font_chooser_widget_get_font (fontchooser);
392 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
396 static PangoFontDescription *
397 tree_model_get_font_description (GtkTreeModel *model,
400 PangoFontDescription *desc;
402 GtkTreeIter child_iter;
404 gtk_tree_model_get (model, iter,
405 FONT_DESC_COLUMN, &desc,
410 gtk_tree_model_get (model, iter,
413 desc = pango_font_face_describe (face);
414 g_object_unref (face);
416 if (GTK_IS_TREE_MODEL_FILTER (model))
418 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
422 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
425 gtk_list_store_set (GTK_LIST_STORE (model), iter,
426 FONT_DESC_COLUMN, desc,
433 cursor_changed_cb (GtkTreeView *treeview,
436 GtkFontChooserWidget *fontchooser = user_data;
437 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
438 PangoFontDescription *desc;
439 GtkTreeIter filter_iter, iter;
440 GtkTreePath *path = NULL;
442 gtk_tree_view_get_cursor (treeview, &path, NULL);
447 if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
449 gtk_tree_path_free (path);
453 gtk_tree_path_free (path);
455 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
458 desc = tree_model_get_font_description (priv->model, &iter);
460 gtk_font_chooser_widget_merge_font_desc (fontchooser, desc, &iter);
464 zoom_preview_cb (GtkWidget *scrolled_window,
465 GdkEventScroll *event,
468 GtkFontChooserWidget *fc = user_data;
469 GtkFontChooserWidgetPrivate *priv = fc->priv;
470 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
472 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
473 gtk_adjustment_set_value (adj,
474 gtk_adjustment_get_value (adj) +
475 gtk_adjustment_get_step_increment (adj));
476 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
477 gtk_adjustment_set_value (adj,
478 gtk_adjustment_get_value (adj) -
479 gtk_adjustment_get_step_increment (adj));
484 row_inserted_cb (GtkTreeModel *model,
489 GtkFontChooserWidget *fontchooser = user_data;
490 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
492 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
496 row_deleted_cb (GtkTreeModel *model,
500 GtkFontChooserWidget *fontchooser = user_data;
501 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
503 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
504 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
508 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
511 GtkFontChooserWidgetPrivate *priv;
512 GtkWidget *scrolled_win;
515 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
516 GTK_TYPE_FONT_CHOOSER_WIDGET,
517 GtkFontChooserWidgetPrivate);
519 priv = fontchooser->priv;
521 /* Default preview string */
522 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
523 priv->show_preview_entry = TRUE;
524 priv->font_desc = pango_font_description_new ();
526 gtk_widget_push_composite_child ();
528 /* Creating fundamental widgets for the private struct */
529 priv->search_entry = gtk_entry_new ();
530 priv->family_face_list = gtk_tree_view_new ();
531 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
532 priv->preview = gtk_entry_new ();
533 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
535 (gdouble)(G_MAXINT / PANGO_SCALE),
538 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
540 /** Bootstrapping widget layout **/
541 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
543 /* Main font family/face view */
544 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
545 scrolled_win = priv->list_scrolled_window;
546 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
547 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
548 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
549 GTK_SHADOW_ETCHED_IN);
550 gtk_widget_set_size_request (scrolled_win, 400, 300);
551 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
553 /* Text to display when list is empty */
554 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
555 gtk_widget_set_margin_top (priv->empty_list, 12);
556 gtk_widget_set_margin_left (priv->empty_list, 12);
557 gtk_widget_set_margin_right (priv->empty_list, 12);
558 gtk_widget_set_margin_bottom (priv->empty_list, 12);
559 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
560 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
562 priv->list_notebook = gtk_notebook_new ();
563 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
564 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
565 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
568 grid = gtk_grid_new ();
570 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
571 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
573 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
574 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
575 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
577 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
578 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
580 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
581 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
582 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
584 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
585 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
587 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
589 gtk_widget_show_all (GTK_WIDGET (fontchooser));
590 gtk_widget_hide (GTK_WIDGET (fontchooser));
592 /* Treeview column and model bootstrapping */
593 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
595 /* Set default preview text */
596 gtk_entry_set_text (GTK_ENTRY (priv->preview),
597 pango_language_get_sample_string (NULL));
599 /* Set search icon and place holder text */
600 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
601 g_object_set (G_OBJECT (priv->search_entry),
602 "secondary-icon-gicon", icon,
603 "secondary-icon-activatable", FALSE,
604 "secondary-icon-sensitive", FALSE,
606 g_object_unref (icon);
608 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
610 /** Callback connections **/
611 g_signal_connect (priv->search_entry, "notify::text",
612 G_CALLBACK (text_changed_cb), fontchooser);
613 g_signal_connect (priv->search_entry,
614 "icon-press", G_CALLBACK (icon_press_cb), NULL);
616 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
617 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
618 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
619 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
621 g_signal_connect (priv->family_face_list, "cursor-changed",
622 G_CALLBACK (cursor_changed_cb), fontchooser);
623 g_signal_connect (priv->family_face_list, "row-activated",
624 G_CALLBACK (row_activated_cb), fontchooser);
626 /* Zoom on preview scroll */
627 g_signal_connect (priv->preview, "scroll-event",
628 G_CALLBACK (zoom_preview_cb), fontchooser);
630 g_signal_connect (priv->size_slider, "scroll-event",
631 G_CALLBACK (zoom_preview_cb), fontchooser);
633 /* Font list empty hides the scrolledwindow */
634 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
635 G_CALLBACK (row_deleted_cb), fontchooser);
636 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
637 G_CALLBACK (row_inserted_cb), fontchooser);
639 /* Set default focus */
640 gtk_widget_pop_composite_child ();
642 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
646 * gtk_font_chooser_widget_new:
648 * Creates a new #GtkFontChooserWidget.
650 * Return value: a new #GtkFontChooserWidget
655 gtk_font_chooser_widget_new (void)
657 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
661 cmp_families (const void *a,
664 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
665 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
667 return g_utf8_collate (a_name, b_name);
671 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
673 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
674 GtkListStore *list_store;
676 PangoFontFamily **families;
677 gchar *family_and_face;
679 list_store = GTK_LIST_STORE (priv->model);
681 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
685 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
687 gtk_list_store_clear (list_store);
689 /* Iterate over families and faces */
690 for (i = 0; i < n_families; i++)
693 PangoFontFace **faces;
695 const gchar *fam_name = pango_font_family_get_name (families[i]);
697 pango_font_family_list_faces (families[i], &faces, &n_faces);
699 for (j = 0; j < n_faces; j++)
701 const gchar *face_name;
703 face_name = pango_font_face_get_face_name (faces[j]);
705 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
707 gtk_list_store_insert_with_values (list_store, &iter, -1,
708 FAMILY_COLUMN, families[i],
709 FACE_COLUMN, faces[j],
710 PREVIEW_TITLE_COLUMN, family_and_face,
713 g_free (family_and_face);
721 /* now make sure the font list looks right */
722 if (!gtk_font_chooser_widget_find_font (fontchooser,
725 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
727 gtk_font_chooser_widget_ensure_selection (fontchooser);
731 visible_func (GtkTreeModel *model,
735 GtkFontChooserWidgetPrivate *priv = user_data;
736 gboolean result = TRUE;
737 const gchar *search_text;
739 gchar *font_name, *font_name_casefold;
742 if (priv->filter_func != NULL)
744 PangoFontFamily *family;
747 gtk_tree_model_get (model, iter,
748 FAMILY_COLUMN, &family,
752 result = priv->filter_func (family, face, priv->filter_data);
754 g_object_unref (family);
755 g_object_unref (face);
761 /* If there's no filter string we show the item */
762 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
763 if (strlen (search_text) == 0)
766 gtk_tree_model_get (model, iter,
767 PREVIEW_TITLE_COLUMN, &font_name,
770 if (font_name == NULL)
773 split_terms = g_strsplit (search_text, " ", 0);
774 font_name_casefold = g_utf8_casefold (font_name, -1);
776 for (i = 0; split_terms[i] && result; i++)
778 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
780 if (!strstr (font_name_casefold, term_casefold))
783 g_free (term_casefold);
786 g_free (font_name_casefold);
788 g_strfreev (split_terms);
795 gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
797 GtkWidget *treeview = fontchooser->priv->family_face_list;
798 double dpi, font_size;
800 dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
801 gtk_style_context_get (gtk_widget_get_style_context (treeview),
802 gtk_widget_get_state_flags (treeview),
803 "font-size", &font_size,
806 return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
809 static PangoAttrList *
810 gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget *fontchooser,
811 const PangoFontDescription *font_desc,
812 gsize first_line_len)
814 PangoAttribute *attribute;
815 PangoAttrList *attrs;
817 attrs = pango_attr_list_new ();
819 attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
820 attribute->end_index = first_line_len;
821 pango_attr_list_insert (attrs, attribute);
823 attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
824 attribute->end_index = first_line_len;
825 pango_attr_list_insert (attrs, attribute);
829 attribute = pango_attr_font_desc_new (font_desc);
830 attribute->start_index = first_line_len;
831 pango_attr_list_insert (attrs, attribute);
834 attribute = pango_attr_fallback_new (FALSE);
835 attribute->start_index = first_line_len;
836 pango_attr_list_insert (attrs, attribute);
838 attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
839 attribute->start_index = first_line_len;
840 pango_attr_list_insert (attrs, attribute);
846 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
847 GtkCellRenderer *cell,
848 GtkTreeModel *tree_model,
852 GtkFontChooserWidget *fontchooser = user_data;
853 PangoFontDescription *font_desc;
854 PangoAttrList *attrs;
855 char *to_string, *text;
856 gsize first_line_len;
858 font_desc = tree_model_get_font_description (tree_model, iter);
860 to_string = pango_font_description_to_string (font_desc);
862 text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
863 first_line_len = strlen (to_string) + 1;
865 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
874 pango_font_description_free (font_desc);
875 pango_attr_list_unref (attrs);
881 gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
883 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
884 PangoAttrList *attrs;
887 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
889 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
893 g_object_set (priv->family_face_cell,
898 pango_attr_list_unref (attrs);
900 gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
901 priv->family_face_list,
904 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
908 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
910 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
911 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
912 GtkTreeViewColumn *col;
914 g_signal_connect_data (priv->family_face_list,
916 G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
919 G_CONNECT_AFTER | G_CONNECT_SWAPPED);
921 priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
922 PANGO_TYPE_FONT_FAMILY,
923 PANGO_TYPE_FONT_FACE,
924 PANGO_TYPE_FONT_DESCRIPTION,
927 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
928 g_object_unref (priv->model);
930 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
931 visible_func, (gpointer)priv, NULL);
933 gtk_tree_view_set_model (treeview, priv->filter_model);
934 g_object_unref (priv->filter_model);
936 gtk_tree_view_set_rules_hint (treeview, TRUE);
937 gtk_tree_view_set_headers_visible (treeview, FALSE);
938 gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
940 priv->family_face_cell = gtk_cell_renderer_text_new ();
941 g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
943 col = gtk_tree_view_column_new ();
944 gtk_tree_view_column_set_title (col, _("Font Family"));
945 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
946 gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
947 gtk_tree_view_column_set_cell_data_func (col,
948 priv->family_face_cell,
949 gtk_font_chooser_widget_cell_data_func,
953 gtk_tree_view_append_column (treeview, col);
955 gtk_font_chooser_widget_load_fonts (fontchooser);
957 gtk_font_chooser_widget_set_cell_size (fontchooser);
961 gtk_font_chooser_widget_finalize (GObject *object)
963 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
964 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
967 pango_font_description_free (priv->font_desc);
969 if (priv->filter_data_destroy)
970 priv->filter_data_destroy (priv->filter_data);
972 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
976 my_pango_font_family_equal (const char *familya,
979 return g_ascii_strcasecmp (familya, familyb) == 0;
983 gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
984 const PangoFontDescription *font_desc,
988 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
989 PangoFontDescription *desc;
990 PangoFontFamily *family;
993 if (pango_font_description_get_family (font_desc) == NULL)
996 for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
998 valid = gtk_tree_model_iter_next (priv->model, iter))
1000 gtk_tree_model_get (priv->model, iter,
1001 FAMILY_COLUMN, &family,
1004 if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
1005 pango_font_family_get_name (family)))
1008 desc = tree_model_get_font_description (priv->model, iter);
1010 pango_font_description_merge_static (desc, font_desc, FALSE);
1011 if (pango_font_description_equal (desc, font_desc))
1014 pango_font_description_free (desc);
1021 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
1022 GdkScreen *previous_screen)
1024 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
1026 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
1027 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
1029 if (previous_screen == NULL)
1030 previous_screen = gdk_screen_get_default ();
1032 if (previous_screen == gtk_widget_get_screen (widget))
1035 gtk_font_chooser_widget_load_fonts (fontchooser);
1038 static PangoFontFamily *
1039 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
1041 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1042 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1043 PangoFontFamily *family;
1045 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1048 gtk_tree_model_get (priv->model, &priv->font_iter,
1049 FAMILY_COLUMN, &family,
1051 g_object_unref (family);
1056 static PangoFontFace *
1057 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
1059 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1060 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1061 PangoFontFace *face;
1063 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1066 gtk_tree_model_get (priv->model, &priv->font_iter,
1069 g_object_unref (face);
1075 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1077 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1079 return pango_font_description_get_size (fontchooser->priv->font_desc);
1083 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1085 return pango_font_description_to_string (fontchooser->priv->font_desc);
1088 static PangoFontDescription *
1089 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1091 return fontchooser->priv->font_desc;
1095 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1096 const gchar *fontname)
1098 PangoFontDescription *font_desc;
1100 font_desc = pango_font_description_from_string (fontname);
1101 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1105 gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
1107 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1108 GtkTreeSelection *selection;
1109 GtkTreeIter filter_iter;
1111 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
1113 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
1114 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1118 gtk_tree_selection_select_iter (selection, &filter_iter);
1122 gtk_tree_selection_unselect_all (selection);
1127 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser,
1128 PangoFontDescription *font_desc,
1131 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1134 g_assert (font_desc != NULL);
1135 /* iter may be NULL if the font doesn't exist on the list */
1137 mask = pango_font_description_get_set_fields (font_desc);
1139 /* sucky test, because we can't restrict the comparison to
1140 * only the parts that actually do get merged */
1141 if (pango_font_description_equal (font_desc, priv->font_desc))
1143 pango_font_description_free (font_desc);
1147 pango_font_description_merge (priv->font_desc, font_desc, TRUE);
1149 if (mask & PANGO_FONT_MASK_SIZE)
1151 double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1152 /* XXX: This clamps, which can cause it to reloop into here, do we need
1153 * to block its signal handler? */
1154 gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1155 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1157 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1158 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1160 if (&priv->font_iter != iter)
1163 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1165 memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
1167 gtk_font_chooser_widget_ensure_selection (fontchooser);
1170 gtk_font_chooser_widget_update_marks (fontchooser);
1173 gtk_widget_override_font (priv->preview, priv->font_desc);
1175 pango_font_description_free (font_desc); /* adopted */
1177 g_object_notify (G_OBJECT (fontchooser), "font");
1178 g_object_notify (G_OBJECT (fontchooser), "font-desc");
1182 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1183 PangoFontDescription *font_desc)
1185 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1188 if (font_desc == NULL)
1189 font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1191 mask = pango_font_description_get_set_fields (font_desc);
1192 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1193 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1197 if (gtk_font_chooser_widget_find_font (fontchooser,
1201 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1207 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1214 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1222 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1224 return fontchooser->priv->preview_text;
1228 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1231 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1233 g_free (priv->preview_text);
1234 priv->preview_text = g_strdup (text);
1236 gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1238 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1240 /* XXX: There's no API to tell the treeview that a column has changed,
1242 gtk_widget_queue_draw (priv->family_face_list);
1246 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1248 return fontchooser->priv->show_preview_entry;
1252 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1253 gboolean show_preview_entry)
1255 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1257 if (priv->show_preview_entry != show_preview_entry)
1259 fontchooser->priv->show_preview_entry = show_preview_entry;
1261 if (show_preview_entry)
1262 gtk_widget_show (fontchooser->priv->preview);
1264 gtk_widget_hide (fontchooser->priv->preview);
1266 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1271 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1272 GtkFontFilterFunc filter,
1274 GDestroyNotify destroy)
1276 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1277 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1279 if (priv->filter_data_destroy)
1280 priv->filter_data_destroy (priv->filter_data);
1282 priv->filter_func = filter;
1283 priv->filter_data = data;
1284 priv->filter_data_destroy = destroy;
1286 gtk_font_chooser_widget_refilter_font_list (fontchooser);
1290 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1292 iface->get_font_family = gtk_font_chooser_widget_get_family;
1293 iface->get_font_face = gtk_font_chooser_widget_get_face;
1294 iface->get_font_size = gtk_font_chooser_widget_get_size;
1295 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;