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"
27 #include "gtkfontchooser.h"
28 #include "gtkfontchooserutils.h"
29 #include "gtkcellrenderertext.h"
35 #include "gtkliststore.h"
37 #include "gtktextview.h"
38 #include "gtktreeselection.h"
39 #include "gtktreeview.h"
40 #include "gtkscrolledwindow.h"
42 #include "gtkaccessible.h"
43 #include "gtkbuildable.h"
44 #include "gtkprivate.h"
46 #include "gtkspinbutton.h"
47 #include "gtknotebook.h"
48 #include "gtkwidget.h"
52 * SECTION:gtkfontchooserwidget
53 * @Short_description: A widget for selecting fonts
54 * @Title: GtkFontChooserWidget
55 * @See_also: #GtkFontChooserDialog
57 * The #GtkFontChooserWidget widget lists the available fonts,
58 * styles and sizes, allowing the user to select a font. It is
59 * used in the #GtkFontChooserDialog widget to provide a
60 * dialog box for selecting fonts.
62 * To set the font which is initially selected, use
63 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
65 * To get the selected font use gtk_font_chooser_get_font() or
66 * gtk_font_chooser_get_font_desc().
68 * To change the text which is shown in the preview area, use
69 * gtk_font_chooser_set_preview_text().
75 struct _GtkFontChooserWidgetPrivate
77 GtkWidget *search_entry;
78 GtkWidget *family_face_list;
79 GtkCellRenderer *family_face_cell;
80 GtkWidget *list_scrolled_window;
81 GtkWidget *empty_list;
82 GtkWidget *list_notebook;
84 GtkTreeModel *filter_model;
88 gboolean show_preview_entry;
91 GtkWidget *size_slider;
93 PangoFontDescription *font_desc;
94 GtkTreeIter font_iter; /* invalid if font not available or pointer into model
95 (not filter_model) to the row containing font */
96 GtkFontFilterFunc filter_func;
98 GDestroyNotify filter_data_destroy;
101 /* This is the initial fixed height and the top padding of the preview entry */
102 #define PREVIEW_HEIGHT 72
103 #define PREVIEW_TOP_PADDING 6
105 /* These are the sizes of the font, style & size lists. */
106 #define FONT_LIST_HEIGHT 136
107 #define FONT_LIST_WIDTH 190
108 #define FONT_STYLE_LIST_WIDTH 170
109 #define FONT_SIZE_LIST_WIDTH 60
111 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
120 static void gtk_font_chooser_widget_set_property (GObject *object,
124 static void gtk_font_chooser_widget_get_property (GObject *object,
128 static void gtk_font_chooser_widget_finalize (GObject *object);
130 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
131 GdkScreen *previous_screen);
133 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
135 static gboolean gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
136 const PangoFontDescription *font_desc,
138 static void gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
140 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
141 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
142 const gchar *fontname);
144 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
145 static void gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget *fontchooser,
146 PangoFontDescription *font_desc,
148 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
149 PangoFontDescription *font_desc);
152 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
153 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
156 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
157 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
158 gboolean show_preview_entry);
160 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
162 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
163 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
164 gtk_font_chooser_widget_iface_init))
167 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
170 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
172 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
174 gobject_class->finalize = gtk_font_chooser_widget_finalize;
175 gobject_class->set_property = gtk_font_chooser_widget_set_property;
176 gobject_class->get_property = gtk_font_chooser_widget_get_property;
178 _gtk_font_chooser_install_properties (gobject_class);
180 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
184 gtk_font_chooser_widget_set_property (GObject *object,
189 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
193 case GTK_FONT_CHOOSER_PROP_FONT:
194 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
196 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
197 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
199 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
200 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
202 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
203 gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 gtk_font_chooser_widget_get_property (GObject *object,
217 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
221 case GTK_FONT_CHOOSER_PROP_FONT:
222 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
224 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
225 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
227 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
228 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
230 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
231 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
242 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
243 gtk_font_chooser_widget_ensure_selection (fontchooser);
247 text_changed_cb (GtkEntry *entry,
249 GtkFontChooserWidget *fc)
251 GtkFontChooserWidgetPrivate *priv = fc->priv;
254 text = gtk_entry_get_text (entry);
256 if (text == NULL || text[0] == '\0')
260 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
261 g_object_set (G_OBJECT (priv->search_entry),
262 "secondary-icon-gicon", icon,
263 "secondary-icon-activatable", FALSE,
264 "secondary-icon-sensitive", FALSE,
266 g_object_unref (icon);
270 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
274 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
275 g_object_set (G_OBJECT (priv->search_entry),
276 "secondary-icon-gicon", icon,
277 "secondary-icon-activatable", TRUE,
278 "secondary-icon-sensitive", TRUE,
280 g_object_unref (icon);
284 gtk_font_chooser_widget_refilter_font_list (fc);
288 icon_press_cb (GtkEntry *entry,
289 GtkEntryIconPosition pos,
293 gtk_entry_set_text (entry, "");
297 size_change_cb (GtkAdjustment *adjustment,
300 GtkFontChooserWidget *fontchooser = user_data;
301 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
302 PangoFontDescription *font_desc;
303 gdouble size = gtk_adjustment_get_value (adjustment);
305 font_desc = pango_font_description_new ();
306 if (pango_font_description_get_size_is_absolute (priv->font_desc))
307 pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
309 pango_font_description_set_size (font_desc, size * PANGO_SCALE);
311 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
315 gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
317 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
323 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
327 gtk_tree_model_get (priv->model, &priv->font_iter,
331 pango_font_face_list_sizes (face, &font_sizes, &n_sizes);
333 /* It seems not many fonts actually have a sane set of sizes */
334 for (i = 0; i < n_sizes; i++)
335 font_sizes[i] = font_sizes[i] / PANGO_SCALE;
337 g_object_unref (face);
347 static const gint fallback_sizes[] = {
348 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
351 sizes = fallback_sizes;
352 n_sizes = G_N_ELEMENTS (fallback_sizes);
359 gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
361 adj = gtk_range_get_adjustment(GTK_RANGE (priv->size_slider));
363 /* ensure clamping doesn't callback into font resizing code */
364 g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
365 gtk_adjustment_configure (adj,
366 gtk_adjustment_get_value (adj),
369 gtk_adjustment_get_step_increment (adj),
370 gtk_adjustment_get_page_increment (adj),
371 gtk_adjustment_get_page_size (adj));
372 g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);
374 for (i = 0; i < n_sizes; i++)
376 gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
378 GTK_POS_BOTTOM, NULL);
385 row_activated_cb (GtkTreeView *view,
387 GtkTreeViewColumn *column,
390 GtkFontChooserWidget *fontchooser = user_data;
393 fontname = gtk_font_chooser_widget_get_font (fontchooser);
394 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
398 static PangoFontDescription *
399 tree_model_get_font_description (GtkTreeModel *model,
402 PangoFontDescription *desc;
404 GtkTreeIter child_iter;
406 gtk_tree_model_get (model, iter,
407 FONT_DESC_COLUMN, &desc,
412 gtk_tree_model_get (model, iter,
415 desc = pango_font_face_describe (face);
416 g_object_unref (face);
418 if (GTK_IS_TREE_MODEL_FILTER (model))
420 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
424 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
427 gtk_list_store_set (GTK_LIST_STORE (model), iter,
428 FONT_DESC_COLUMN, desc,
435 cursor_changed_cb (GtkTreeView *treeview,
438 GtkFontChooserWidget *fontchooser = user_data;
439 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
440 PangoFontDescription *desc;
441 GtkTreeIter filter_iter, iter;
442 GtkTreePath *path = NULL;
444 gtk_tree_view_get_cursor (treeview, &path, NULL);
449 if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
451 gtk_tree_path_free (path);
455 gtk_tree_path_free (path);
457 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
460 desc = tree_model_get_font_description (priv->model, &iter);
462 gtk_font_chooser_widget_merge_font_desc (fontchooser, desc, &iter);
466 zoom_preview_cb (GtkWidget *scrolled_window,
467 GdkEventScroll *event,
470 GtkFontChooserWidget *fc = user_data;
471 GtkFontChooserWidgetPrivate *priv = fc->priv;
472 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
474 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
475 gtk_adjustment_set_value (adj,
476 gtk_adjustment_get_value (adj) +
477 gtk_adjustment_get_step_increment (adj));
478 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
479 gtk_adjustment_set_value (adj,
480 gtk_adjustment_get_value (adj) -
481 gtk_adjustment_get_step_increment (adj));
486 row_inserted_cb (GtkTreeModel *model,
491 GtkFontChooserWidget *fontchooser = user_data;
492 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
494 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
498 row_deleted_cb (GtkTreeModel *model,
502 GtkFontChooserWidget *fontchooser = user_data;
503 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
505 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
506 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
510 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
513 GtkFontChooserWidgetPrivate *priv;
514 GtkWidget *scrolled_win;
517 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
518 GTK_TYPE_FONT_CHOOSER_WIDGET,
519 GtkFontChooserWidgetPrivate);
521 priv = fontchooser->priv;
523 /* Default preview string */
524 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
525 priv->show_preview_entry = TRUE;
526 priv->font_desc = pango_font_description_new ();
528 gtk_widget_push_composite_child ();
530 /* Creating fundamental widgets for the private struct */
531 priv->search_entry = gtk_entry_new ();
532 priv->family_face_list = gtk_tree_view_new ();
533 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
534 priv->preview = gtk_entry_new ();
535 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
537 (gdouble)(G_MAXINT / PANGO_SCALE),
540 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
542 /** Bootstrapping widget layout **/
543 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
545 /* Main font family/face view */
546 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
547 scrolled_win = priv->list_scrolled_window;
548 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
549 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
550 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
551 GTK_SHADOW_ETCHED_IN);
552 gtk_widget_set_size_request (scrolled_win, 400, 300);
553 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
555 /* Text to display when list is empty */
556 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
557 gtk_widget_set_margin_top (priv->empty_list, 12);
558 gtk_widget_set_margin_left (priv->empty_list, 12);
559 gtk_widget_set_margin_right (priv->empty_list, 12);
560 gtk_widget_set_margin_bottom (priv->empty_list, 12);
561 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
562 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
564 priv->list_notebook = gtk_notebook_new ();
565 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
566 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
567 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
570 grid = gtk_grid_new ();
572 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
573 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
575 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
576 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
577 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
579 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
580 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
582 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
583 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
584 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
586 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
587 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
589 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
591 gtk_widget_show_all (GTK_WIDGET (fontchooser));
592 gtk_widget_hide (GTK_WIDGET (fontchooser));
594 /* Treeview column and model bootstrapping */
595 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
597 /* Set default preview text */
598 gtk_entry_set_text (GTK_ENTRY (priv->preview),
599 pango_language_get_sample_string (NULL));
601 /* Set search icon and place holder text */
602 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
603 g_object_set (G_OBJECT (priv->search_entry),
604 "secondary-icon-gicon", icon,
605 "secondary-icon-activatable", FALSE,
606 "secondary-icon-sensitive", FALSE,
608 g_object_unref (icon);
610 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
612 /** Callback connections **/
613 g_signal_connect (priv->search_entry, "notify::text",
614 G_CALLBACK (text_changed_cb), fontchooser);
615 g_signal_connect (priv->search_entry,
616 "icon-press", G_CALLBACK (icon_press_cb), NULL);
618 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
619 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
620 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
621 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
623 g_signal_connect (priv->family_face_list, "cursor-changed",
624 G_CALLBACK (cursor_changed_cb), fontchooser);
625 g_signal_connect (priv->family_face_list, "row-activated",
626 G_CALLBACK (row_activated_cb), fontchooser);
628 /* Zoom on preview scroll */
629 g_signal_connect (priv->preview, "scroll-event",
630 G_CALLBACK (zoom_preview_cb), fontchooser);
632 g_signal_connect (priv->size_slider, "scroll-event",
633 G_CALLBACK (zoom_preview_cb), fontchooser);
635 /* Font list empty hides the scrolledwindow */
636 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
637 G_CALLBACK (row_deleted_cb), fontchooser);
638 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
639 G_CALLBACK (row_inserted_cb), fontchooser);
641 /* Set default focus */
642 gtk_widget_pop_composite_child ();
644 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
648 * gtk_font_chooser_widget_new:
650 * Creates a new #GtkFontChooserWidget.
652 * Return value: a new #GtkFontChooserWidget
657 gtk_font_chooser_widget_new (void)
659 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
663 cmp_families (const void *a,
666 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
667 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
669 return g_utf8_collate (a_name, b_name);
673 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
675 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
676 GtkListStore *list_store;
678 PangoFontFamily **families;
679 gchar *family_and_face;
681 list_store = GTK_LIST_STORE (priv->model);
683 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
687 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
689 gtk_list_store_clear (list_store);
691 /* Iterate over families and faces */
692 for (i = 0; i < n_families; i++)
695 PangoFontFace **faces;
697 const gchar *fam_name = pango_font_family_get_name (families[i]);
699 pango_font_family_list_faces (families[i], &faces, &n_faces);
701 for (j = 0; j < n_faces; j++)
703 const gchar *face_name;
705 face_name = pango_font_face_get_face_name (faces[j]);
707 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
709 gtk_list_store_insert_with_values (list_store, &iter, -1,
710 FAMILY_COLUMN, families[i],
711 FACE_COLUMN, faces[j],
712 PREVIEW_TITLE_COLUMN, family_and_face,
715 g_free (family_and_face);
723 /* now make sure the font list looks right */
724 if (!gtk_font_chooser_widget_find_font (fontchooser,
727 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
729 gtk_font_chooser_widget_ensure_selection (fontchooser);
733 visible_func (GtkTreeModel *model,
737 GtkFontChooserWidgetPrivate *priv = user_data;
738 gboolean result = TRUE;
739 const gchar *search_text;
741 gchar *font_name, *font_name_casefold;
744 if (priv->filter_func != NULL)
746 PangoFontFamily *family;
749 gtk_tree_model_get (model, iter,
750 FAMILY_COLUMN, &family,
754 result = priv->filter_func (family, face, priv->filter_data);
756 g_object_unref (family);
757 g_object_unref (face);
763 /* If there's no filter string we show the item */
764 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
765 if (strlen (search_text) == 0)
768 gtk_tree_model_get (model, iter,
769 PREVIEW_TITLE_COLUMN, &font_name,
772 if (font_name == NULL)
775 split_terms = g_strsplit (search_text, " ", 0);
776 font_name_casefold = g_utf8_casefold (font_name, -1);
778 for (i = 0; split_terms[i] && result; i++)
780 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
782 if (!strstr (font_name_casefold, term_casefold))
785 g_free (term_casefold);
788 g_free (font_name_casefold);
790 g_strfreev (split_terms);
797 gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
799 GtkWidget *treeview = fontchooser->priv->family_face_list;
800 double dpi, font_size;
802 dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
803 gtk_style_context_get (gtk_widget_get_style_context (treeview),
804 gtk_widget_get_state_flags (treeview),
805 "font-size", &font_size,
808 return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
811 static PangoAttrList *
812 gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget *fontchooser,
813 const PangoFontDescription *font_desc,
814 gsize first_line_len)
816 PangoAttribute *attribute;
817 PangoAttrList *attrs;
819 attrs = pango_attr_list_new ();
821 attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
822 attribute->end_index = first_line_len;
823 pango_attr_list_insert (attrs, attribute);
825 attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
826 attribute->end_index = first_line_len;
827 pango_attr_list_insert (attrs, attribute);
831 attribute = pango_attr_font_desc_new (font_desc);
832 attribute->start_index = first_line_len;
833 pango_attr_list_insert (attrs, attribute);
836 attribute = pango_attr_fallback_new (FALSE);
837 attribute->start_index = first_line_len;
838 pango_attr_list_insert (attrs, attribute);
840 attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
841 attribute->start_index = first_line_len;
842 pango_attr_list_insert (attrs, attribute);
848 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
849 GtkCellRenderer *cell,
850 GtkTreeModel *tree_model,
854 GtkFontChooserWidget *fontchooser = user_data;
855 PangoFontDescription *font_desc;
856 PangoAttrList *attrs;
857 char *to_string, *text;
858 gsize first_line_len;
860 font_desc = tree_model_get_font_description (tree_model, iter);
862 to_string = pango_font_description_to_string (font_desc);
864 text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
865 first_line_len = strlen (to_string) + 1;
867 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
876 pango_font_description_free (font_desc);
877 pango_attr_list_unref (attrs);
883 gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
885 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
886 PangoAttrList *attrs;
889 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
891 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
895 g_object_set (priv->family_face_cell,
900 pango_attr_list_unref (attrs);
902 gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
903 priv->family_face_list,
906 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
910 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
912 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
913 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
914 GtkTreeViewColumn *col;
916 g_signal_connect_data (priv->family_face_list,
918 G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
921 G_CONNECT_AFTER | G_CONNECT_SWAPPED);
923 priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
924 PANGO_TYPE_FONT_FAMILY,
925 PANGO_TYPE_FONT_FACE,
926 PANGO_TYPE_FONT_DESCRIPTION,
929 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
930 g_object_unref (priv->model);
932 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
933 visible_func, (gpointer)priv, NULL);
935 gtk_tree_view_set_model (treeview, priv->filter_model);
936 g_object_unref (priv->filter_model);
938 gtk_tree_view_set_rules_hint (treeview, TRUE);
939 gtk_tree_view_set_headers_visible (treeview, FALSE);
940 gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
942 priv->family_face_cell = gtk_cell_renderer_text_new ();
943 g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
945 col = gtk_tree_view_column_new ();
946 gtk_tree_view_column_set_title (col, _("Font Family"));
947 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
948 gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
949 gtk_tree_view_column_set_cell_data_func (col,
950 priv->family_face_cell,
951 gtk_font_chooser_widget_cell_data_func,
955 gtk_tree_view_append_column (treeview, col);
957 gtk_font_chooser_widget_load_fonts (fontchooser);
959 gtk_font_chooser_widget_set_cell_size (fontchooser);
963 gtk_font_chooser_widget_finalize (GObject *object)
965 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
966 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
969 pango_font_description_free (priv->font_desc);
971 if (priv->filter_data_destroy)
972 priv->filter_data_destroy (priv->filter_data);
974 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
978 my_pango_font_family_equal (const char *familya,
981 return g_ascii_strcasecmp (familya, familyb) == 0;
985 gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
986 const PangoFontDescription *font_desc,
990 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
991 PangoFontDescription *desc;
992 PangoFontFamily *family;
995 if (pango_font_description_get_family (font_desc) == NULL)
998 for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
1000 valid = gtk_tree_model_iter_next (priv->model, iter))
1002 gtk_tree_model_get (priv->model, iter,
1003 FAMILY_COLUMN, &family,
1006 if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
1007 pango_font_family_get_name (family)))
1010 desc = tree_model_get_font_description (priv->model, iter);
1012 pango_font_description_merge_static (desc, font_desc, FALSE);
1013 if (pango_font_description_equal (desc, font_desc))
1016 pango_font_description_free (desc);
1023 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
1024 GdkScreen *previous_screen)
1026 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
1028 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
1029 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
1031 if (previous_screen == NULL)
1032 previous_screen = gdk_screen_get_default ();
1034 if (previous_screen == gtk_widget_get_screen (widget))
1037 gtk_font_chooser_widget_load_fonts (fontchooser);
1040 static PangoFontFamily *
1041 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
1043 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1044 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1045 PangoFontFamily *family;
1047 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1050 gtk_tree_model_get (priv->model, &priv->font_iter,
1051 FAMILY_COLUMN, &family,
1053 g_object_unref (family);
1058 static PangoFontFace *
1059 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
1061 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1062 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1063 PangoFontFace *face;
1065 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1068 gtk_tree_model_get (priv->model, &priv->font_iter,
1071 g_object_unref (face);
1077 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1079 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1081 return pango_font_description_get_size (fontchooser->priv->font_desc);
1085 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1087 return pango_font_description_to_string (fontchooser->priv->font_desc);
1090 static PangoFontDescription *
1091 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1093 return fontchooser->priv->font_desc;
1097 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1098 const gchar *fontname)
1100 PangoFontDescription *font_desc;
1102 font_desc = pango_font_description_from_string (fontname);
1103 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1107 gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
1109 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1110 GtkTreeSelection *selection;
1111 GtkTreeIter filter_iter;
1113 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
1115 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
1116 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1120 gtk_tree_selection_select_iter (selection, &filter_iter);
1124 gtk_tree_selection_unselect_all (selection);
1129 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser,
1130 PangoFontDescription *font_desc,
1133 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1136 g_assert (font_desc != NULL);
1137 /* iter may be NULL if the font doesn't exist on the list */
1139 mask = pango_font_description_get_set_fields (font_desc);
1141 /* sucky test, because we can't restrict the comparison to
1142 * only the parts that actually do get merged */
1143 if (pango_font_description_equal (font_desc, priv->font_desc))
1145 pango_font_description_free (font_desc);
1149 pango_font_description_merge (priv->font_desc, font_desc, TRUE);
1151 if (mask & PANGO_FONT_MASK_SIZE)
1153 double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1154 /* XXX: This clamps, which can cause it to reloop into here, do we need
1155 * to block its signal handler? */
1156 gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1157 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1159 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1160 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1162 if (&priv->font_iter != iter)
1165 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1167 memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
1169 gtk_font_chooser_widget_ensure_selection (fontchooser);
1172 gtk_font_chooser_widget_update_marks (fontchooser);
1175 gtk_widget_override_font (priv->preview, priv->font_desc);
1177 pango_font_description_free (font_desc); /* adopted */
1179 g_object_notify (G_OBJECT (fontchooser), "font");
1180 g_object_notify (G_OBJECT (fontchooser), "font-desc");
1184 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1185 PangoFontDescription *font_desc)
1187 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1190 if (font_desc == NULL)
1191 font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1193 mask = pango_font_description_get_set_fields (font_desc);
1194 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1195 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1199 if (gtk_font_chooser_widget_find_font (fontchooser,
1203 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1209 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1216 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1224 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1226 return fontchooser->priv->preview_text;
1230 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1233 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1235 g_free (priv->preview_text);
1236 priv->preview_text = g_strdup (text);
1238 gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1240 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1242 /* XXX: There's no API to tell the treeview that a column has changed,
1244 gtk_widget_queue_draw (priv->family_face_list);
1248 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1250 return fontchooser->priv->show_preview_entry;
1254 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1255 gboolean show_preview_entry)
1257 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1259 if (priv->show_preview_entry != show_preview_entry)
1261 fontchooser->priv->show_preview_entry = show_preview_entry;
1263 if (show_preview_entry)
1264 gtk_widget_show (fontchooser->priv->preview);
1266 gtk_widget_hide (fontchooser->priv->preview);
1268 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1273 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1274 GtkFontFilterFunc filter,
1276 GDestroyNotify destroy)
1278 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1279 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1281 if (priv->filter_data_destroy)
1282 priv->filter_data_destroy (priv->filter_data);
1284 priv->filter_func = filter;
1285 priv->filter_data = data;
1286 priv->filter_data_destroy = destroy;
1288 gtk_font_chooser_widget_refilter_font_list (fontchooser);
1292 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1294 iface->get_font_family = gtk_font_chooser_widget_get_family;
1295 iface->get_font_face = gtk_font_chooser_widget_get_face;
1296 iface->get_font_size = gtk_font_chooser_widget_get_size;
1297 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;