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 "gtkfontchooserwidget.h"
29 #include "gtkfontchooser.h"
30 #include "gtkfontchooserutils.h"
31 #include "gtkcellrenderertext.h"
37 #include "gtkliststore.h"
39 #include "gtktextview.h"
40 #include "gtktreeselection.h"
41 #include "gtktreeview.h"
42 #include "gtkscrolledwindow.h"
44 #include "gtkaccessible.h"
45 #include "gtkbuildable.h"
46 #include "gtkprivate.h"
48 #include "gtkspinbutton.h"
49 #include "gtknotebook.h"
50 #include "gtkwidget.h"
54 * SECTION:gtkfontchooser
55 * @Short_description: A widget for selecting fonts
56 * @Title: GtkFontChooserWidget
57 * @See_also: #GtkFontChooserDialog
59 * The #GtkFontChooserWidget widget lists the available fonts,
60 * styles and sizes, allowing the user to select a font. It is
61 * used in the #GtkFontChooserDialog widget to provide a
62 * dialog box for selecting fonts.
64 * To set the font which is initially selected, use
65 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
67 * To get the selected font use gtk_font_chooser_get_font() or
68 * gtk_font_chooser_get_font_desc().
70 * To change the text which is shown in the preview area, use
71 * gtk_font_chooser_set_preview_text().
77 struct _GtkFontChooserWidgetPrivate
79 GtkWidget *search_entry;
80 GtkWidget *family_face_list;
81 GtkCellRenderer *family_face_cell;
82 GtkWidget *list_scrolled_window;
83 GtkWidget *empty_list;
84 GtkWidget *list_notebook;
86 GtkTreeModel *filter_model;
90 gboolean show_preview_entry;
93 GtkWidget *size_slider;
95 PangoFontDescription *font_desc;
96 GtkTreeIter font_iter; /* invalid if font not available or pointer into model
97 (not filter_model) to the row containing font */
98 GtkFontFilterFunc filter_func;
100 GDestroyNotify filter_data_destroy;
103 /* This is the initial fixed height and the top padding of the preview entry */
104 #define PREVIEW_HEIGHT 72
105 #define PREVIEW_TOP_PADDING 6
107 /* These are the sizes of the font, style & size lists. */
108 #define FONT_LIST_HEIGHT 136
109 #define FONT_LIST_WIDTH 190
110 #define FONT_STYLE_LIST_WIDTH 170
111 #define FONT_SIZE_LIST_WIDTH 60
113 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
122 static void gtk_font_chooser_widget_set_property (GObject *object,
126 static void gtk_font_chooser_widget_get_property (GObject *object,
130 static void gtk_font_chooser_widget_finalize (GObject *object);
132 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
133 GdkScreen *previous_screen);
135 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
137 static gboolean gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
138 const PangoFontDescription *font_desc,
140 static void gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
142 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
143 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
144 const gchar *fontname);
146 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
147 static void gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget *fontchooser,
148 PangoFontDescription *font_desc,
150 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
151 PangoFontDescription *font_desc);
154 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
155 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
158 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
159 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
160 gboolean show_preview_entry);
162 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
164 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
165 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
166 gtk_font_chooser_widget_iface_init))
169 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
174 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
176 gobject_class->finalize = gtk_font_chooser_widget_finalize;
177 gobject_class->set_property = gtk_font_chooser_widget_set_property;
178 gobject_class->get_property = gtk_font_chooser_widget_get_property;
180 _gtk_font_chooser_install_properties (gobject_class);
182 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
186 gtk_font_chooser_widget_set_property (GObject *object,
191 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
195 case GTK_FONT_CHOOSER_PROP_FONT:
196 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
198 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
199 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
201 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
202 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
204 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
205 gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gtk_font_chooser_widget_get_property (GObject *object,
219 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
223 case GTK_FONT_CHOOSER_PROP_FONT:
224 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
226 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
227 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
229 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
230 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
232 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
233 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
244 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
245 gtk_font_chooser_widget_ensure_selection (fontchooser);
249 text_changed_cb (GtkEntry *entry,
251 GtkFontChooserWidget *fc)
253 GtkFontChooserWidgetPrivate *priv = fc->priv;
256 text = gtk_entry_get_text (entry);
258 if (text == NULL || text[0] == '\0')
262 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
263 g_object_set (G_OBJECT (priv->search_entry),
264 "secondary-icon-gicon", icon,
265 "secondary-icon-activatable", FALSE,
266 "secondary-icon-sensitive", FALSE,
268 g_object_unref (icon);
272 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
276 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
277 g_object_set (G_OBJECT (priv->search_entry),
278 "secondary-icon-gicon", icon,
279 "secondary-icon-activatable", TRUE,
280 "secondary-icon-sensitive", TRUE,
282 g_object_unref (icon);
286 gtk_font_chooser_widget_refilter_font_list (fc);
290 icon_press_cb (GtkEntry *entry,
291 GtkEntryIconPosition pos,
295 gtk_entry_set_text (entry, "");
299 size_change_cb (GtkAdjustment *adjustment,
302 GtkFontChooserWidget *fontchooser = user_data;
303 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
304 PangoFontDescription *font_desc;
305 gdouble size = gtk_adjustment_get_value (adjustment);
307 font_desc = pango_font_description_new ();
308 if (pango_font_description_get_size_is_absolute (priv->font_desc))
309 pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
311 pango_font_description_set_size (font_desc, size * PANGO_SCALE);
313 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
317 gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
319 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
325 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
329 gtk_tree_model_get (priv->model, &priv->font_iter,
333 pango_font_face_list_sizes (face, &font_sizes, &n_sizes);
335 /* It seems not many fonts actually have a sane set of sizes */
336 for (i = 0; i < n_sizes; i++)
337 font_sizes[i] = font_sizes[i] / PANGO_SCALE;
339 g_object_unref (face);
349 static const gint fallback_sizes[] = {
350 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
353 sizes = fallback_sizes;
354 n_sizes = G_N_ELEMENTS (fallback_sizes);
361 gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
363 adj = gtk_range_get_adjustment(GTK_RANGE (priv->size_slider));
365 /* ensure clamping doesn't callback into font resizing code */
366 g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
367 gtk_adjustment_configure (adj,
368 gtk_adjustment_get_value (adj),
371 gtk_adjustment_get_step_increment (adj),
372 gtk_adjustment_get_page_increment (adj),
373 gtk_adjustment_get_page_size (adj));
374 g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);
376 for (i = 0; i < n_sizes; i++)
378 gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
380 GTK_POS_BOTTOM, NULL);
387 row_activated_cb (GtkTreeView *view,
389 GtkTreeViewColumn *column,
392 GtkFontChooserWidget *fontchooser = user_data;
395 fontname = gtk_font_chooser_widget_get_font (fontchooser);
396 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
400 static PangoFontDescription *
401 tree_model_get_font_description (GtkTreeModel *model,
404 PangoFontDescription *desc;
406 GtkTreeIter child_iter;
408 gtk_tree_model_get (model, iter,
409 FONT_DESC_COLUMN, &desc,
414 gtk_tree_model_get (model, iter,
417 desc = pango_font_face_describe (face);
418 g_object_unref (face);
420 if (GTK_IS_TREE_MODEL_FILTER (model))
422 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
426 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
429 gtk_list_store_set (GTK_LIST_STORE (model), iter,
430 FONT_DESC_COLUMN, desc,
437 cursor_changed_cb (GtkTreeView *treeview,
440 GtkFontChooserWidget *fontchooser = user_data;
441 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
442 PangoFontDescription *desc;
443 GtkTreeIter filter_iter, iter;
444 GtkTreePath *path = NULL;
446 gtk_tree_view_get_cursor (treeview, &path, NULL);
451 if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
453 gtk_tree_path_free (path);
457 gtk_tree_path_free (path);
459 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
462 desc = tree_model_get_font_description (priv->model, &iter);
464 gtk_font_chooser_widget_merge_font_desc (fontchooser, desc, &iter);
468 zoom_preview_cb (GtkWidget *scrolled_window,
469 GdkEventScroll *event,
472 GtkFontChooserWidget *fc = user_data;
473 GtkFontChooserWidgetPrivate *priv = fc->priv;
474 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
476 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
477 gtk_adjustment_set_value (adj,
478 gtk_adjustment_get_value (adj) +
479 gtk_adjustment_get_step_increment (adj));
480 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
481 gtk_adjustment_set_value (adj,
482 gtk_adjustment_get_value (adj) -
483 gtk_adjustment_get_step_increment (adj));
488 row_inserted_cb (GtkTreeModel *model,
493 GtkFontChooserWidget *fontchooser = user_data;
494 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
496 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
500 row_deleted_cb (GtkTreeModel *model,
504 GtkFontChooserWidget *fontchooser = user_data;
505 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
507 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
508 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
512 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
515 GtkFontChooserWidgetPrivate *priv;
516 GtkWidget *scrolled_win;
519 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
520 GTK_TYPE_FONT_CHOOSER_WIDGET,
521 GtkFontChooserWidgetPrivate);
523 priv = fontchooser->priv;
525 /* Default preview string */
526 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
527 priv->show_preview_entry = TRUE;
528 priv->font_desc = pango_font_description_new ();
530 gtk_widget_push_composite_child ();
532 /* Creating fundamental widgets for the private struct */
533 priv->search_entry = gtk_entry_new ();
534 priv->family_face_list = gtk_tree_view_new ();
535 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
536 priv->preview = gtk_entry_new ();
537 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
539 (gdouble)(G_MAXINT / PANGO_SCALE),
542 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
544 /** Bootstrapping widget layout **/
545 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
547 /* Main font family/face view */
548 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
549 scrolled_win = priv->list_scrolled_window;
550 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
551 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
552 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
553 GTK_SHADOW_ETCHED_IN);
554 gtk_widget_set_size_request (scrolled_win, 400, 300);
555 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
557 /* Text to display when list is empty */
558 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
559 gtk_widget_set_margin_top (priv->empty_list, 12);
560 gtk_widget_set_margin_left (priv->empty_list, 12);
561 gtk_widget_set_margin_right (priv->empty_list, 12);
562 gtk_widget_set_margin_bottom (priv->empty_list, 12);
563 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
564 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
566 priv->list_notebook = gtk_notebook_new ();
567 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
568 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
569 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
572 grid = gtk_grid_new ();
574 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
575 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
577 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
578 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
579 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
581 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
582 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
584 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
585 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
586 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
588 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
589 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
591 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
593 gtk_widget_show_all (GTK_WIDGET (fontchooser));
594 gtk_widget_hide (GTK_WIDGET (fontchooser));
596 /* Treeview column and model bootstrapping */
597 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
599 /* Set default preview text */
600 gtk_entry_set_text (GTK_ENTRY (priv->preview),
601 pango_language_get_sample_string (NULL));
603 /* Set search icon and place holder text */
604 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
605 g_object_set (G_OBJECT (priv->search_entry),
606 "secondary-icon-gicon", icon,
607 "secondary-icon-activatable", FALSE,
608 "secondary-icon-sensitive", FALSE,
610 g_object_unref (icon);
612 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
614 /** Callback connections **/
615 g_signal_connect (priv->search_entry, "notify::text",
616 G_CALLBACK (text_changed_cb), fontchooser);
617 g_signal_connect (priv->search_entry,
618 "icon-press", G_CALLBACK (icon_press_cb), NULL);
620 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
621 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
622 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
623 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
625 g_signal_connect (priv->family_face_list, "cursor-changed",
626 G_CALLBACK (cursor_changed_cb), fontchooser);
627 g_signal_connect (priv->family_face_list, "row-activated",
628 G_CALLBACK (row_activated_cb), fontchooser);
630 /* Zoom on preview scroll */
631 g_signal_connect (priv->preview, "scroll-event",
632 G_CALLBACK (zoom_preview_cb), fontchooser);
634 g_signal_connect (priv->size_slider, "scroll-event",
635 G_CALLBACK (zoom_preview_cb), fontchooser);
637 /* Font list empty hides the scrolledwindow */
638 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
639 G_CALLBACK (row_deleted_cb), fontchooser);
640 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
641 G_CALLBACK (row_inserted_cb), fontchooser);
643 /* Set default focus */
644 gtk_widget_pop_composite_child ();
646 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
650 * gtk_font_chooser_widget_new:
652 * Creates a new #GtkFontChooserWidget.
654 * Return value: a new #GtkFontChooserWidget
659 gtk_font_chooser_widget_new (void)
661 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
665 cmp_families (const void *a,
668 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
669 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
671 return g_utf8_collate (a_name, b_name);
675 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
677 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
678 GtkListStore *list_store;
680 PangoFontFamily **families;
681 gchar *family_and_face;
683 list_store = GTK_LIST_STORE (priv->model);
685 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
689 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
691 gtk_list_store_clear (list_store);
693 /* Iterate over families and faces */
694 for (i = 0; i < n_families; i++)
697 PangoFontFace **faces;
699 const gchar *fam_name = pango_font_family_get_name (families[i]);
701 pango_font_family_list_faces (families[i], &faces, &n_faces);
703 for (j = 0; j < n_faces; j++)
705 const gchar *face_name;
707 face_name = pango_font_face_get_face_name (faces[j]);
709 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
711 gtk_list_store_insert_with_values (list_store, &iter, -1,
712 FAMILY_COLUMN, families[i],
713 FACE_COLUMN, faces[j],
714 PREVIEW_TITLE_COLUMN, family_and_face,
717 g_free (family_and_face);
725 /* now make sure the font list looks right */
726 if (!gtk_font_chooser_widget_find_font (fontchooser,
729 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
731 gtk_font_chooser_widget_ensure_selection (fontchooser);
735 visible_func (GtkTreeModel *model,
739 GtkFontChooserWidgetPrivate *priv = user_data;
740 gboolean result = TRUE;
741 const gchar *search_text;
743 gchar *font_name, *font_name_casefold;
746 if (priv->filter_func != NULL)
748 PangoFontFamily *family;
751 gtk_tree_model_get (model, iter,
752 FAMILY_COLUMN, &family,
756 result = priv->filter_func (family, face, priv->filter_data);
758 g_object_unref (family);
759 g_object_unref (face);
765 /* If there's no filter string we show the item */
766 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
767 if (strlen (search_text) == 0)
770 gtk_tree_model_get (model, iter,
771 PREVIEW_TITLE_COLUMN, &font_name,
774 if (font_name == NULL)
777 split_terms = g_strsplit (search_text, " ", 0);
778 font_name_casefold = g_utf8_casefold (font_name, -1);
780 for (i = 0; split_terms[i] && result; i++)
782 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
784 if (!strstr (font_name_casefold, term_casefold))
787 g_free (term_casefold);
790 g_free (font_name_casefold);
792 g_strfreev (split_terms);
799 gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
801 GtkWidget *treeview = fontchooser->priv->family_face_list;
802 double dpi, font_size;
804 dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
805 gtk_style_context_get (gtk_widget_get_style_context (treeview),
806 gtk_widget_get_state_flags (treeview),
807 "font-size", &font_size,
810 return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
813 static PangoAttrList *
814 gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget *fontchooser,
815 const PangoFontDescription *font_desc,
816 gsize first_line_len)
818 PangoAttribute *attribute;
819 PangoAttrList *attrs;
821 attrs = pango_attr_list_new ();
823 attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
824 attribute->end_index = first_line_len;
825 pango_attr_list_insert (attrs, attribute);
827 attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
828 attribute->end_index = first_line_len;
829 pango_attr_list_insert (attrs, attribute);
833 attribute = pango_attr_font_desc_new (font_desc);
834 attribute->start_index = first_line_len;
835 pango_attr_list_insert (attrs, attribute);
838 attribute = pango_attr_fallback_new (FALSE);
839 attribute->start_index = first_line_len;
840 pango_attr_list_insert (attrs, attribute);
842 attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
843 attribute->start_index = first_line_len;
844 pango_attr_list_insert (attrs, attribute);
850 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
851 GtkCellRenderer *cell,
852 GtkTreeModel *tree_model,
856 GtkFontChooserWidget *fontchooser = user_data;
857 PangoFontDescription *font_desc;
858 PangoAttrList *attrs;
859 char *to_string, *text;
860 gsize first_line_len;
862 font_desc = tree_model_get_font_description (tree_model, iter);
864 to_string = pango_font_description_to_string (font_desc);
866 text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
867 first_line_len = strlen (to_string) + 1;
869 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
878 pango_font_description_free (font_desc);
879 pango_attr_list_unref (attrs);
885 gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
887 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
888 PangoAttrList *attrs;
891 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
893 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
897 g_object_set (priv->family_face_cell,
902 pango_attr_list_unref (attrs);
904 gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
905 priv->family_face_list,
908 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
912 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
914 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
915 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
916 GtkTreeViewColumn *col;
918 g_signal_connect_data (priv->family_face_list,
920 G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
923 G_CONNECT_AFTER | G_CONNECT_SWAPPED);
925 priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
926 PANGO_TYPE_FONT_FAMILY,
927 PANGO_TYPE_FONT_FACE,
928 PANGO_TYPE_FONT_DESCRIPTION,
931 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
932 g_object_unref (priv->model);
934 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
935 visible_func, (gpointer)priv, NULL);
937 gtk_tree_view_set_model (treeview, priv->filter_model);
938 g_object_unref (priv->filter_model);
940 gtk_tree_view_set_rules_hint (treeview, TRUE);
941 gtk_tree_view_set_headers_visible (treeview, FALSE);
942 gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
944 priv->family_face_cell = gtk_cell_renderer_text_new ();
945 g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
947 col = gtk_tree_view_column_new ();
948 gtk_tree_view_column_set_title (col, _("Font Family"));
949 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
950 gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
951 gtk_tree_view_column_set_cell_data_func (col,
952 priv->family_face_cell,
953 gtk_font_chooser_widget_cell_data_func,
957 gtk_tree_view_append_column (treeview, col);
959 gtk_font_chooser_widget_load_fonts (fontchooser);
961 gtk_font_chooser_widget_set_cell_size (fontchooser);
965 gtk_font_chooser_widget_finalize (GObject *object)
967 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
968 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
971 pango_font_description_free (priv->font_desc);
973 if (priv->filter_data_destroy)
974 priv->filter_data_destroy (priv->filter_data);
976 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
980 my_pango_font_family_equal (const char *familya,
983 return g_ascii_strcasecmp (familya, familyb) == 0;
987 gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
988 const PangoFontDescription *font_desc,
992 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
993 PangoFontDescription *desc;
994 PangoFontFamily *family;
997 if (pango_font_description_get_family (font_desc) == NULL)
1000 for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
1002 valid = gtk_tree_model_iter_next (priv->model, iter))
1004 gtk_tree_model_get (priv->model, iter,
1005 FAMILY_COLUMN, &family,
1008 if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
1009 pango_font_family_get_name (family)))
1012 desc = tree_model_get_font_description (priv->model, iter);
1014 pango_font_description_merge_static (desc, font_desc, FALSE);
1015 if (pango_font_description_equal (desc, font_desc))
1018 pango_font_description_free (desc);
1025 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
1026 GdkScreen *previous_screen)
1028 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
1030 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
1031 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
1033 if (previous_screen == NULL)
1034 previous_screen = gdk_screen_get_default ();
1036 if (previous_screen == gtk_widget_get_screen (widget))
1039 gtk_font_chooser_widget_load_fonts (fontchooser);
1042 static PangoFontFamily *
1043 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
1045 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1046 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1047 PangoFontFamily *family;
1049 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1052 gtk_tree_model_get (priv->model, &priv->font_iter,
1053 FAMILY_COLUMN, &family,
1055 g_object_unref (family);
1060 static PangoFontFace *
1061 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
1063 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1064 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1065 PangoFontFace *face;
1067 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1070 gtk_tree_model_get (priv->model, &priv->font_iter,
1073 g_object_unref (face);
1079 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1081 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1083 return pango_font_description_get_size (fontchooser->priv->font_desc);
1087 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1089 return pango_font_description_to_string (fontchooser->priv->font_desc);
1092 static PangoFontDescription *
1093 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1095 return fontchooser->priv->font_desc;
1099 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1100 const gchar *fontname)
1102 PangoFontDescription *font_desc;
1104 font_desc = pango_font_description_from_string (fontname);
1105 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1109 gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
1111 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1112 GtkTreeSelection *selection;
1113 GtkTreeIter filter_iter;
1115 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
1117 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
1118 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1122 gtk_tree_selection_select_iter (selection, &filter_iter);
1126 gtk_tree_selection_unselect_all (selection);
1131 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser,
1132 PangoFontDescription *font_desc,
1135 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1138 g_assert (font_desc != NULL);
1139 /* iter may be NULL if the font doesn't exist on the list */
1141 mask = pango_font_description_get_set_fields (font_desc);
1143 /* sucky test, because we can't restrict the comparison to
1144 * only the parts that actually do get merged */
1145 if (pango_font_description_equal (font_desc, priv->font_desc))
1147 pango_font_description_free (font_desc);
1151 pango_font_description_merge (priv->font_desc, font_desc, TRUE);
1153 if (mask & PANGO_FONT_MASK_SIZE)
1155 double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1156 /* XXX: This clamps, which can cause it to reloop into here, do we need
1157 * to block its signal handler? */
1158 gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1159 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1161 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1162 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1164 if (&priv->font_iter != iter)
1167 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1169 memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
1171 gtk_font_chooser_widget_ensure_selection (fontchooser);
1174 gtk_font_chooser_widget_update_marks (fontchooser);
1177 gtk_widget_override_font (priv->preview, priv->font_desc);
1179 pango_font_description_free (font_desc); /* adopted */
1181 g_object_notify (G_OBJECT (fontchooser), "font");
1182 g_object_notify (G_OBJECT (fontchooser), "font-desc");
1186 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1187 PangoFontDescription *font_desc)
1189 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1192 if (font_desc == NULL)
1193 font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1195 mask = pango_font_description_get_set_fields (font_desc);
1196 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1197 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1201 if (gtk_font_chooser_widget_find_font (fontchooser,
1205 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1211 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1218 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1226 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1228 return fontchooser->priv->preview_text;
1232 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1235 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1237 g_free (priv->preview_text);
1238 priv->preview_text = g_strdup (text);
1240 gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1242 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1244 /* XXX: There's no API to tell the treeview that a column has changed,
1246 gtk_widget_queue_draw (priv->family_face_list);
1250 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1252 return fontchooser->priv->show_preview_entry;
1256 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1257 gboolean show_preview_entry)
1259 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1261 if (priv->show_preview_entry != show_preview_entry)
1263 fontchooser->priv->show_preview_entry = show_preview_entry;
1265 if (show_preview_entry)
1266 gtk_widget_show (fontchooser->priv->preview);
1268 gtk_widget_hide (fontchooser->priv->preview);
1270 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1275 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1276 GtkFontFilterFunc filter,
1278 GDestroyNotify destroy)
1280 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1281 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1283 if (priv->filter_data_destroy)
1284 priv->filter_data_destroy (priv->filter_data);
1286 priv->filter_func = filter;
1287 priv->filter_data = data;
1288 priv->filter_data_destroy = destroy;
1290 gtk_font_chooser_widget_refilter_font_list (fontchooser);
1294 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1296 iface->get_font_family = gtk_font_chooser_widget_get_family;
1297 iface->get_font_face = gtk_font_chooser_widget_get_face;
1298 iface->get_font_size = gtk_font_chooser_widget_get_size;
1299 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;