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 GtkWidget *list_scrolled_window;
82 GtkWidget *empty_list;
83 GtkWidget *list_notebook;
85 GtkTreeModel *filter_model;
89 gboolean show_preview_entry;
92 GtkWidget *size_slider;
94 PangoFontDescription *font_desc;
96 PangoFontFamily *family;
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 ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
115 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
117 /* These are what we use as the standard font sizes, for the size list.
119 static const gint font_sizes[] = {
120 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
131 static void gtk_font_chooser_widget_set_property (GObject *object,
135 static void gtk_font_chooser_widget_get_property (GObject *object,
139 static void gtk_font_chooser_widget_finalize (GObject *object);
141 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
142 GdkScreen *previous_screen);
144 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
146 static void gtk_font_chooser_widget_select_font (GtkFontChooserWidget *fontchooser);
148 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
149 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
150 const gchar *fontname);
152 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
153 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
154 PangoFontDescription *font_desc);
157 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
158 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
161 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
162 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
163 gboolean show_preview_entry);
165 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
167 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
168 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
169 gtk_font_chooser_widget_iface_init))
172 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
174 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
175 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
177 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
179 gobject_class->finalize = gtk_font_chooser_widget_finalize;
180 gobject_class->set_property = gtk_font_chooser_widget_set_property;
181 gobject_class->get_property = gtk_font_chooser_widget_get_property;
183 _gtk_font_chooser_install_properties (gobject_class);
185 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
189 gtk_font_chooser_widget_set_property (GObject *object,
194 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
198 case GTK_FONT_CHOOSER_PROP_FONT:
199 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
201 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
202 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
204 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
205 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
207 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
208 gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 gtk_font_chooser_widget_get_property (GObject *object,
222 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
226 case GTK_FONT_CHOOSER_PROP_FONT:
227 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
229 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
230 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
232 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
233 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
235 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
236 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
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 set_range_marks (GtkFontChooserWidgetPrivate *priv,
314 GtkWidget *size_slider,
324 sizes = (gint*)font_sizes;
325 length = G_N_ELEMENTS (font_sizes);
328 gtk_scale_clear_marks (GTK_SCALE (size_slider));
330 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
332 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
333 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
335 value = gtk_adjustment_get_value (adj);
336 if (value > (gdouble) sizes[length-1])
337 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
338 else if (value < (gdouble) sizes[0])
339 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
341 for (i = 0; i < length; i++)
342 gtk_scale_add_mark (GTK_SCALE (size_slider),
344 GTK_POS_BOTTOM, NULL);
348 row_activated_cb (GtkTreeView *view,
350 GtkTreeViewColumn *column,
353 GtkFontChooserWidget *fontchooser = user_data;
356 fontname = gtk_font_chooser_widget_get_font (fontchooser);
357 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
362 cursor_changed_cb (GtkTreeView *treeview,
365 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
366 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
368 PangoFontFamily *family;
370 PangoFontDescription *desc;
376 GtkTreePath *path = gtk_tree_path_new ();
378 gtk_tree_view_get_cursor (treeview, &path, NULL);
383 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
385 gtk_tree_path_free (path);
389 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
391 FAMILY_COLUMN, &family,
394 gtk_tree_path_free (path);
397 desc = pango_font_face_describe (face);
398 pango_font_description_set_size (desc, pango_font_description_get_size (priv->font_desc));
399 gtk_widget_override_font (priv->preview, desc);
401 pango_font_face_list_sizes (face, &sizes, &n_sizes);
402 /* It seems not many fonts actually have a sane set of sizes */
403 for (i = 0; i < n_sizes; i++)
404 sizes[i] = sizes[i] / PANGO_SCALE;
406 set_range_marks (priv, priv->size_slider, sizes, n_sizes);
409 g_object_unref (priv->family);
410 priv->family = family;
413 g_object_unref (priv->face);
417 pango_font_description_free (priv->font_desc);
418 priv->font_desc = desc;
420 g_object_notify (G_OBJECT (fontchooser), "font");
421 g_object_notify (G_OBJECT (fontchooser), "font-desc");
425 zoom_preview_cb (GtkWidget *scrolled_window,
426 GdkEventScroll *event,
429 GtkFontChooserWidget *fc = (GtkFontChooserWidget*)user_data;
430 GtkFontChooserWidgetPrivate *priv = fc->priv;
432 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
434 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
435 gtk_adjustment_set_value (adj,
436 gtk_adjustment_get_value (adj) +
437 gtk_adjustment_get_step_increment (adj));
438 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
439 gtk_adjustment_set_value (adj,
440 gtk_adjustment_get_value (adj) -
441 gtk_adjustment_get_step_increment (adj));
446 row_inserted_cb (GtkTreeModel *model,
451 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
452 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
454 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
458 row_deleted_cb (GtkTreeModel *model,
462 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
463 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
465 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
466 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
470 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
473 GtkFontChooserWidgetPrivate *priv;
474 GtkWidget *scrolled_win;
477 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
478 GTK_TYPE_FONT_CHOOSER_WIDGET,
479 GtkFontChooserWidgetPrivate);
481 priv = fontchooser->priv;
483 /* Default preview string */
484 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
485 priv->show_preview_entry = TRUE;
486 priv->font_desc = pango_font_description_new ();
488 gtk_widget_push_composite_child ();
490 /* Creating fundamental widgets for the private struct */
491 priv->search_entry = gtk_entry_new ();
492 priv->family_face_list = gtk_tree_view_new ();
493 priv->preview = gtk_entry_new ();
494 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
495 (gdouble) font_sizes[0],
496 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
499 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
501 /** Bootstrapping widget layout **/
502 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
504 /* Main font family/face view */
505 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
506 scrolled_win = priv->list_scrolled_window;
507 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
508 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
509 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
510 GTK_SHADOW_ETCHED_IN);
511 gtk_widget_set_size_request (scrolled_win, 400, 300);
512 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
514 /* Text to display when list is empty */
515 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
516 gtk_widget_set_margin_top (priv->empty_list, 12);
517 gtk_widget_set_margin_left (priv->empty_list, 12);
518 gtk_widget_set_margin_right (priv->empty_list, 12);
519 gtk_widget_set_margin_bottom (priv->empty_list, 12);
520 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
521 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
523 priv->list_notebook = gtk_notebook_new ();
524 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
525 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
526 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
529 grid = gtk_grid_new ();
531 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
532 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
534 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
535 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
536 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
538 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
539 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
541 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
542 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
543 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
545 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
546 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
548 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
550 gtk_widget_show_all (GTK_WIDGET (fontchooser));
551 gtk_widget_hide (GTK_WIDGET (fontchooser));
553 /* Treeview column and model bootstrapping */
554 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
556 /* Set default preview text */
557 gtk_entry_set_text (GTK_ENTRY (priv->preview),
558 pango_language_get_sample_string (NULL));
560 /* Set search icon and place holder text */
561 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
562 g_object_set (G_OBJECT (priv->search_entry),
563 "secondary-icon-gicon", icon,
564 "secondary-icon-activatable", FALSE,
565 "secondary-icon-sensitive", FALSE,
567 g_object_unref (icon);
569 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
571 /** Callback connections **/
572 g_signal_connect (priv->search_entry, "notify::text",
573 G_CALLBACK (text_changed_cb), fontchooser);
574 g_signal_connect (priv->search_entry,
575 "icon-press", G_CALLBACK (icon_press_cb), NULL);
577 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
578 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
579 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
580 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
582 g_signal_connect (priv->family_face_list, "cursor-changed",
583 G_CALLBACK (cursor_changed_cb), fontchooser);
584 g_signal_connect (priv->family_face_list, "row-activated",
585 G_CALLBACK (row_activated_cb), fontchooser);
587 /* Zoom on preview scroll */
588 g_signal_connect (priv->preview, "scroll-event",
589 G_CALLBACK (zoom_preview_cb), fontchooser);
591 g_signal_connect (priv->size_slider, "scroll-event",
592 G_CALLBACK (zoom_preview_cb), fontchooser);
594 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
596 /* Font list empty hides the scrolledwindow */
597 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
598 G_CALLBACK (row_deleted_cb), fontchooser);
599 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
600 G_CALLBACK (row_inserted_cb), fontchooser);
602 /* Set default focus */
603 gtk_widget_pop_composite_child ();
605 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
609 * gtk_font_chooser_widget_new:
611 * Creates a new #GtkFontChooserWidget.
613 * Return value: a new #GtkFontChooserWidget
618 gtk_font_chooser_widget_new (void)
620 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
624 cmp_families (const void *a,
627 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
628 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
630 return g_utf8_collate (a_name, b_name);
634 populate_list (GtkFontChooserWidget *fontchooser,
635 GtkTreeView *treeview,
638 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
639 GtkStyleContext *style_context;
640 PangoFontDescription *default_font;
641 PangoFontDescription *selected_font;
644 GtkTreeIter match_row;
648 PangoFontFamily **families;
650 gchar *family_and_face;
652 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
656 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
658 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
659 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
663 selected_font = pango_font_face_describe (priv->face);
665 selected_font = NULL;
667 gtk_list_store_clear (model);
671 /* Iterate over families and faces */
672 for (i = 0; i < n_families; i++)
675 PangoFontFace **faces;
677 const gchar *fam_name = pango_font_family_get_name (families[i]);
679 pango_font_family_list_faces (families[i], &faces, &n_faces);
681 for (j = 0; j < n_faces; j++)
683 PangoFontDescription *pango_desc;
684 const gchar *face_name;
687 pango_desc = pango_font_face_describe (faces[j]);
688 face_name = pango_font_face_get_face_name (faces[j]);
689 font_desc = pango_font_description_to_string (pango_desc);
691 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
692 tmp = g_markup_printf_escaped (ROW_FORMAT_STRING,
695 fontchooser->priv->preview_text);
697 gtk_list_store_insert_with_values (model, &iter, -1,
698 FAMILY_COLUMN, families[i],
699 FACE_COLUMN, faces[j],
700 FONT_DESC_COLUMN, pango_desc,
701 PREVIEW_TITLE_COLUMN, family_and_face,
702 PREVIEW_TEXT_COLUMN, tmp,
705 /* Select the current font,
706 * the default font/face from the theme,
710 selected_font != NULL &&
711 pango_font_description_equal (selected_font, pango_desc))
717 strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
728 pango_font_description_free (pango_desc);
729 g_free (family_and_face);
737 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
740 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
741 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
742 gtk_tree_path_free (path);
746 pango_font_description_free (selected_font);
752 visible_func (GtkTreeModel *model,
756 GtkFontChooserWidgetPrivate *priv = user_data;
757 gboolean result = TRUE;
758 const gchar *search_text;
760 gchar *font_name, *font_name_casefold;
763 if (priv->filter_func != NULL)
765 PangoFontFamily *family;
768 gtk_tree_model_get (model, iter,
769 FAMILY_COLUMN, &family,
773 result = priv->filter_func (family, face, priv->filter_data);
775 g_object_unref (family);
776 g_object_unref (face);
782 /* If there's no filter string we show the item */
783 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
784 if (strlen (search_text) == 0)
787 gtk_tree_model_get (model, iter,
788 PREVIEW_TITLE_COLUMN, &font_name,
791 if (font_name == NULL)
794 split_terms = g_strsplit (search_text, " ", 0);
795 font_name_casefold = g_utf8_casefold (font_name, -1);
797 for (i = 0; split_terms[i] && result; i++)
799 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
801 if (!strstr (font_name_casefold, term_casefold))
804 g_free (term_casefold);
807 g_free (font_name_casefold);
809 g_strfreev (split_terms);
815 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
817 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
818 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
819 GtkCellRenderer *cell;
820 GtkTreeViewColumn *col;
822 priv->model = gtk_list_store_new (5,
823 PANGO_TYPE_FONT_FAMILY,
824 PANGO_TYPE_FONT_FACE,
825 PANGO_TYPE_FONT_DESCRIPTION,
829 priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
830 g_object_unref (priv->model);
832 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
833 visible_func, (gpointer)priv, NULL);
835 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
836 g_object_unref (priv->filter_model);
838 gtk_tree_view_set_rules_hint (treeview, TRUE);
839 gtk_tree_view_set_headers_visible (treeview, FALSE);
841 cell = gtk_cell_renderer_text_new ();
842 col = gtk_tree_view_column_new_with_attributes (_("Font Family"),
844 "markup", PREVIEW_TEXT_COLUMN,
847 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
849 gtk_tree_view_append_column (treeview, col);
851 populate_list (fontchooser, treeview, priv->model);
855 gtk_font_chooser_widget_finalize (GObject *object)
857 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
858 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
861 pango_font_description_free (priv->font_desc);
864 g_object_unref (priv->family);
867 g_object_unref (priv->face);
869 if (priv->filter_data_destroy)
870 priv->filter_data_destroy (priv->filter_data);
872 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
876 gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
877 const PangoFontDescription *font_desc,
880 PangoFontFamily **family,
881 PangoFontFace **face)
883 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
884 PangoFontDescription *desc;
887 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->model), iter);
889 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->model), iter))
891 gtk_tree_model_get (GTK_TREE_MODEL (priv->model), iter,
893 FAMILY_COLUMN, family,
894 FONT_DESC_COLUMN, &desc,
897 pango_font_description_merge_static (desc, font_desc, FALSE);
898 if (pango_font_description_equal (desc, font_desc))
901 g_object_unref (face);
902 g_object_unref (family);
903 pango_font_description_free (desc);
910 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
911 GdkScreen *previous_screen)
913 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
914 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
916 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
917 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
919 if (previous_screen == NULL)
920 previous_screen = gdk_screen_get_default ();
922 if (previous_screen == gtk_widget_get_screen (widget))
925 populate_list (fontchooser,
926 GTK_TREE_VIEW (priv->family_face_list),
929 gtk_font_chooser_widget_select_font (fontchooser);
932 static PangoFontFamily *
933 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
935 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
937 return fontchooser->priv->family;
940 static PangoFontFace *
941 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
943 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
945 return fontchooser->priv->face;
949 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
951 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
953 return pango_font_description_get_size (fontchooser->priv->font_desc);
957 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
959 return pango_font_description_to_string (fontchooser->priv->font_desc);
962 static PangoFontDescription *
963 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
965 return fontchooser->priv->font_desc;
969 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
970 const gchar *fontname)
972 PangoFontDescription *font_desc;
974 font_desc = pango_font_description_from_string (fontname);
975 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
979 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
980 PangoFontDescription *font_desc)
982 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
985 if (font_desc && priv->font_desc &&
986 pango_font_description_equal (font_desc, priv->font_desc))
988 pango_font_description_free (font_desc);
992 if (font_desc == NULL)
993 font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
995 pango_font_description_merge (priv->font_desc, font_desc, TRUE);
997 mask = pango_font_description_get_set_fields (font_desc);
999 if (mask & PANGO_FONT_MASK_SIZE)
1001 double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1002 /* XXX: This clamps, which can cause it to reloop into here, do we need
1003 * to block its signal handler? */
1004 gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1005 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1007 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1008 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1009 gtk_font_chooser_widget_select_font (fontchooser);
1011 gtk_widget_override_font (priv->preview, priv->font_desc);
1013 pango_font_description_free (font_desc); /* adopted */
1015 g_object_notify (G_OBJECT (fontchooser), "font");
1016 g_object_notify (G_OBJECT (fontchooser), "font-desc");
1020 gtk_font_chooser_widget_select_font (GtkFontChooserWidget *fontchooser)
1022 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1026 g_object_unref (priv->family);
1028 g_object_unref (priv->face);
1030 if (gtk_font_chooser_widget_find_font (fontchooser,
1036 GtkTreeIter filter_iter;
1038 if (gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1042 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model), &filter_iter);
1043 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1047 gtk_tree_path_free (path);
1052 gtk_tree_selection_unselect_all
1053 (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list)));
1055 priv->family = NULL;
1060 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1062 return fontchooser->priv->preview_text;
1066 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1069 g_free (fontchooser->priv->preview_text);
1070 fontchooser->priv->preview_text = g_strdup (text);
1072 populate_list (fontchooser,
1073 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1074 fontchooser->priv->model);
1076 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1078 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1082 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1084 return fontchooser->priv->show_preview_entry;
1088 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1089 gboolean show_preview_entry)
1091 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1093 if (priv->show_preview_entry != show_preview_entry)
1095 fontchooser->priv->show_preview_entry = show_preview_entry;
1097 if (show_preview_entry)
1098 gtk_widget_show (fontchooser->priv->preview);
1100 gtk_widget_hide (fontchooser->priv->preview);
1102 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1107 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1108 GtkFontFilterFunc filter,
1110 GDestroyNotify destroy)
1112 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1113 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1115 if (priv->filter_data_destroy)
1116 priv->filter_data_destroy (priv->filter_data);
1118 priv->filter_func = filter;
1119 priv->filter_data = data;
1120 priv->filter_data_destroy = destroy;
1122 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1126 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1128 iface->get_font_family = gtk_font_chooser_widget_get_family;
1129 iface->get_font_face = gtk_font_chooser_widget_get_face;
1130 iface->get_font_size = gtk_font_chooser_widget_get_size;
1131 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;