1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <glib/gprintf.h>
28 #include "gtkfontchooser.h"
29 #include "gtkcellrenderertext.h"
35 #include "gtkliststore.h"
37 #include "gtktextview.h"
38 #include "gtktreeselection.h"
39 #include "gtktreeview.h"
41 #include "gtkscrolledwindow.h"
43 #include "gtkaccessible.h"
44 #include "gtkbuildable.h"
45 #include "gtkprivate.h"
46 #include "gtkalignment.h"
49 #include "gtkspinbutton.h"
50 #include "gtknotebook.h"
51 #include "gtkwidget.h"
55 * SECTION:gtkfontchooser
56 * @Short_description: A widget for selecting fonts
57 * @Title: GtkFontChooser
58 * @See_also: #GtkFontChooserDialog
60 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
61 * allowing the user to select a font.
62 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
65 * To set the font which is initially selected, use
66 * gtk_font_chooser_set_font_name().
68 * To get the selected font use gtk_font_chooser_get_font_name().
70 * To change the text which is shown in the preview area, use
71 * gtk_font_chooser_set_preview_text().
77 struct _GtkFontChooserPrivate
79 GtkWidget *search_entry;
80 GtkWidget *family_face_list;
81 GtkWidget *list_scrolled_window;
82 GtkWidget *empty_list;
83 GtkWidget *list_notebook;
85 GtkTreeModel *filter_model;
89 gboolean show_preview_entry;
92 GtkWidget *size_slider;
97 PangoFontFamily *family;
99 gulong cursor_changed_handler;
101 GtkFontFilterFunc filter_func;
102 gpointer filter_data;
103 GDestroyNotify filter_data_destroy;
106 #define DEFAULT_FONT_NAME "Sans 10"
107 #define MAX_FONT_SIZE 999
109 /* This is the initial fixed height and the top padding of the preview entry */
110 #define PREVIEW_HEIGHT 72
111 #define PREVIEW_TOP_PADDING 6
113 /* These are the sizes of the font, style & size lists. */
114 #define FONT_LIST_HEIGHT 136
115 #define FONT_LIST_WIDTH 190
116 #define FONT_STYLE_LIST_WIDTH 170
117 #define FONT_SIZE_LIST_WIDTH 60
119 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
121 #define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
123 /* These are what we use as the standard font sizes, for the size list.
125 static const gint font_sizes[] = {
126 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
133 PROP_SHOW_PREVIEW_ENTRY
144 static void gtk_font_chooser_set_property (GObject *object,
148 static void gtk_font_chooser_get_property (GObject *object,
152 static void gtk_font_chooser_finalize (GObject *object);
153 static void gtk_font_chooser_dispose (GObject *object);
155 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
156 GdkScreen *previous_screen);
157 static void gtk_font_chooser_style_updated (GtkWidget *widget);
159 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
161 static gboolean gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser);
163 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
166 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
171 widget_class->screen_changed = gtk_font_chooser_screen_changed;
172 widget_class->style_updated = gtk_font_chooser_style_updated;
174 gobject_class->dispose = gtk_font_chooser_dispose;
175 gobject_class->finalize = gtk_font_chooser_finalize;
176 gobject_class->set_property = gtk_font_chooser_set_property;
177 gobject_class->get_property = gtk_font_chooser_get_property;
179 g_object_class_install_property (gobject_class,
181 g_param_spec_string ("font-name",
183 P_("The string that represents this font"),
185 GTK_PARAM_READWRITE));
186 g_object_class_install_property (gobject_class,
188 g_param_spec_string ("preview-text",
190 P_("The text to display in order to demonstrate the selected font"),
191 pango_language_get_sample_string (NULL),
192 GTK_PARAM_READWRITE));
194 g_object_class_install_property (gobject_class,
195 PROP_SHOW_PREVIEW_ENTRY,
196 g_param_spec_boolean ("show-preview-entry",
197 P_("Show preview text entry"),
198 P_("Whether the preview text entry is shown or not"),
200 GTK_PARAM_READWRITE));
202 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
206 gtk_font_chooser_set_property (GObject *object,
211 GtkFontChooser *fontchooser;
213 fontchooser = GTK_FONT_CHOOSER (object);
218 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
220 case PROP_PREVIEW_TEXT:
221 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
223 case PROP_SHOW_PREVIEW_ENTRY:
224 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gtk_font_chooser_get_property (GObject *object,
238 GtkFontChooser *fontchooser;
240 fontchooser = GTK_FONT_CHOOSER (object);
245 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
247 case PROP_PREVIEW_TEXT:
248 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
250 case PROP_SHOW_PREVIEW_ENTRY:
251 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 text_changed_cb (GtkEntry *entry,
264 GtkFontChooserPrivate *priv = fc->priv;
267 text = gtk_entry_get_text (entry);
269 if (text == NULL || text[0] == '\0')
273 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
274 g_object_set (G_OBJECT (priv->search_entry),
275 "secondary-icon-gicon", icon,
276 "secondary-icon-activatable", FALSE,
277 "secondary-icon-sensitive", FALSE,
279 g_object_unref (icon);
283 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
287 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
288 g_object_set (G_OBJECT (priv->search_entry),
289 "secondary-icon-gicon", icon,
290 "secondary-icon-activatable", TRUE,
291 "secondary-icon-sensitive", TRUE,
293 g_object_unref (icon);
297 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
301 icon_press_cb (GtkEntry *entry,
302 GtkEntryIconPosition pos,
306 gtk_entry_set_text (entry, "");
310 slider_change_cb (GtkAdjustment *adjustment,
313 GtkFontChooser *fc = (GtkFontChooser*)user_data;
314 GtkFontChooserPrivate *priv = fc->priv;
315 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
316 gdouble slider_value = gtk_adjustment_get_value (adjustment);
317 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
319 if (slider_value != spin_value)
320 gtk_adjustment_set_value (spin_adj,
321 gtk_adjustment_get_value (adjustment));
325 spin_change_cb (GtkAdjustment *adjustment,
328 PangoFontDescription *desc;
329 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
330 GtkFontChooserPrivate *priv = fontchooser->priv;
331 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
333 gdouble size = gtk_adjustment_get_value (adjustment);
334 priv->size = ((gint)size) * PANGO_SCALE;
336 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
337 pango_font_description_set_size (desc, priv->size);
338 gtk_widget_override_font (priv->preview, desc);
340 g_object_notify (G_OBJECT (fontchooser), "font-name");
342 /* If the new value is lower than the lower bound of the slider, we set
343 * the slider adjustment to the lower bound value if it is not already set
345 if (size < gtk_adjustment_get_lower (slider_adj) &&
346 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
347 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
349 /* If the new value is upper than the upper bound of the slider, we set
350 * the slider adjustment to the upper bound value if it is not already set
352 else if (size > gtk_adjustment_get_upper (slider_adj) &&
353 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
354 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
356 /* If the new value is not already set on the slider we set it */
357 else if (size != gtk_adjustment_get_value (slider_adj))
358 gtk_adjustment_set_value (slider_adj, size);
360 gtk_widget_queue_draw (priv->preview);
364 set_range_marks (GtkFontChooserPrivate *priv,
365 GtkWidget *size_slider,
375 sizes = (gint*)font_sizes;
376 length = G_N_ELEMENTS (font_sizes);
379 gtk_scale_clear_marks (GTK_SCALE (size_slider));
381 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
383 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
384 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
386 value = gtk_adjustment_get_value (adj);
387 if (value > (gdouble) sizes[length-1])
388 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
389 else if (value < (gdouble) sizes[0])
390 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
392 for (i = 0; i < length; i++)
393 gtk_scale_add_mark (GTK_SCALE (size_slider),
395 GTK_POS_BOTTOM, NULL);
399 cursor_changed_cb (GtkTreeView *treeview,
402 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
403 GtkFontChooserPrivate *priv = fontchooser->priv;
405 PangoFontFamily *family;
407 PangoFontDescription *desc;
413 GtkTreePath *path = gtk_tree_path_new ();
415 gtk_tree_view_get_cursor (treeview, &path, NULL);
420 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
422 gtk_tree_path_free (path);
427 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
429 FAMILY_COLUMN, &family,
432 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
434 gtk_tree_path_free (path);
437 if (face == NULL || family == NULL)
440 g_object_unref (face);
442 g_object_unref (family);
447 desc = pango_font_face_describe (face);
448 pango_font_description_set_size (desc, priv->size);
449 gtk_widget_override_font (priv->preview, desc);
451 pango_font_face_list_sizes (face, &sizes, &n_sizes);
452 /* It seems not many fonts actually have a sane set of sizes */
453 for (i = 0; i < n_sizes; i++)
454 sizes[i] = sizes[i] / PANGO_SCALE;
456 set_range_marks (priv, priv->size_slider, sizes, n_sizes);
459 g_object_unref (priv->family);
460 priv->family = family;
463 g_object_unref (priv->face);
466 pango_font_description_free (desc);
468 g_object_notify (G_OBJECT (fontchooser), "font-name");
472 zoom_preview_cb (GtkWidget *scrolled_window,
473 GdkEventScroll *event,
476 GtkFontChooser *fc = (GtkFontChooser*)user_data;
477 GtkFontChooserPrivate *priv = fc->priv;
479 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
481 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
482 gtk_adjustment_set_value (adj,
483 gtk_adjustment_get_value (adj) +
484 gtk_adjustment_get_step_increment (adj));
485 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
486 gtk_adjustment_set_value (adj,
487 gtk_adjustment_get_value (adj) -
488 gtk_adjustment_get_step_increment (adj));
493 row_inserted_cb (GtkTreeModel *model,
498 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
499 GtkFontChooserPrivate *priv = fontchooser->priv;
501 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
505 row_deleted_cb (GtkTreeModel *model,
509 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
510 GtkFontChooserPrivate *priv = fontchooser->priv;
512 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
513 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
517 gtk_font_chooser_init (GtkFontChooser *fontchooser)
520 GtkFontChooserPrivate *priv;
521 PangoFontDescription *font_desc;
522 GtkWidget *scrolled_win;
525 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
526 GTK_TYPE_FONT_CHOOSER,
527 GtkFontChooserPrivate);
529 priv = fontchooser->priv;
531 /* Default preview string */
532 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
533 priv->show_preview_entry = TRUE;
535 /* Getting the default size */
536 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
537 priv->size = pango_font_description_get_size (font_desc);
541 gtk_widget_push_composite_child ();
543 /* Creating fundamental widgets for the private struct */
544 priv->search_entry = gtk_entry_new ();
545 priv->family_face_list = gtk_tree_view_new ();
546 priv->preview = gtk_entry_new ();
547 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
548 (gdouble) font_sizes[0],
549 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
552 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
554 /** Bootstrapping widget layout **/
555 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
557 /* Main font family/face view */
558 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
559 scrolled_win = priv->list_scrolled_window;
560 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
561 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
562 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
563 GTK_SHADOW_ETCHED_IN);
564 gtk_widget_set_size_request (scrolled_win, 400, 300);
565 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
567 /* Text to display when list is empty */
568 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
569 gtk_widget_set_margin_top (priv->empty_list, 12);
570 gtk_widget_set_margin_left (priv->empty_list, 12);
571 gtk_widget_set_margin_right (priv->empty_list, 12);
572 gtk_widget_set_margin_bottom (priv->empty_list, 12);
573 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
574 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
576 priv->list_notebook = gtk_notebook_new ();
577 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
578 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
579 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
582 grid = gtk_grid_new ();
584 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
585 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
587 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
588 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
589 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
591 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
592 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
594 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
595 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
596 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
598 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
599 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
601 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
603 /* Setting the adjustment values for the size slider */
604 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
605 (gdouble)(priv->size / PANGO_SCALE));
606 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
607 (gdouble)(priv->size / PANGO_SCALE));
609 gtk_widget_show_all (GTK_WIDGET (fontchooser));
610 gtk_widget_hide (GTK_WIDGET (fontchooser));
612 /* Treeview column and model bootstrapping */
613 gtk_font_chooser_bootstrap_fontlist (fontchooser);
615 /* Set default preview text */
616 gtk_entry_set_text (GTK_ENTRY (priv->preview),
617 pango_language_get_sample_string (NULL));
619 /* Set search icon and place holder text */
620 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
621 g_object_set (G_OBJECT (priv->search_entry),
622 "secondary-icon-gicon", icon,
623 "secondary-icon-activatable", FALSE,
624 "secondary-icon-sensitive", FALSE,
626 g_object_unref (icon);
628 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
630 /** Callback connections **/
631 g_signal_connect (priv->search_entry, "notify::text",
632 G_CALLBACK (text_changed_cb), fontchooser);
633 g_signal_connect (priv->search_entry,
634 "icon-press", G_CALLBACK (icon_press_cb), NULL);
636 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
637 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
638 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
639 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
641 priv->cursor_changed_handler =
642 g_signal_connect (priv->family_face_list, "cursor-changed",
643 G_CALLBACK (cursor_changed_cb), fontchooser);
645 /* Zoom on preview scroll*/
646 g_signal_connect (priv->preview, "scroll-event",
647 G_CALLBACK (zoom_preview_cb), fontchooser);
649 g_signal_connect (priv->size_slider, "scroll-event",
650 G_CALLBACK (zoom_preview_cb), fontchooser);
652 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
654 /* Font list empty hides the scrolledwindow */
655 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
656 G_CALLBACK (row_deleted_cb), fontchooser);
657 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
658 G_CALLBACK (row_inserted_cb), fontchooser);
660 /* Set default focus */
661 gtk_widget_pop_composite_child ();
665 * gtk_font_chooser_new:
667 * Creates a new #GtkFontChooser.
669 * Return value: a new #GtkFontChooser
674 gtk_font_chooser_new (void)
676 GtkFontChooser *fontchooser;
678 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
680 return GTK_WIDGET (fontchooser);
684 cmp_families (const void *a,
687 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
688 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
690 return g_utf8_collate (a_name, b_name);
694 populate_list (GtkFontChooser *fontchooser,
695 GtkTreeView *treeview,
698 GtkFontChooserPrivate *priv = fontchooser->priv;
699 GtkStyleContext *style_context;
700 PangoFontDescription *default_font;
701 PangoFontDescription *selected_font;
704 GtkTreeIter match_row;
708 PangoFontFamily **families;
710 GString *family_and_face;
712 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
715 tmp = g_string_new (NULL);
716 family_and_face = g_string_new (NULL);
718 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
722 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
724 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
725 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
729 selected_font = pango_font_face_describe (priv->face);
731 selected_font = NULL;
733 gtk_list_store_clear (model);
737 /* Iterate over families and faces */
738 for (i = 0; i < n_families; i++)
741 PangoFontFace **faces;
743 const gchar *fam_name = pango_font_family_get_name (families[i]);
745 pango_font_family_list_faces (families[i], &faces, &n_faces);
747 for (j = 0; j < n_faces; j++)
749 PangoFontDescription *pango_desc;
750 const gchar *face_name;
753 if (priv->filter_func != NULL &&
754 !priv->filter_func (families[i], faces[j], priv->filter_data))
757 pango_desc = pango_font_face_describe (faces[j]);
758 face_name = pango_font_face_get_face_name (faces[j]);
759 font_desc = pango_font_description_to_string (pango_desc);
761 g_string_printf (family_and_face, "%s %s", fam_name, face_name);
762 g_string_printf (tmp, ROW_FORMAT_STRING,
763 family_and_face->str,
765 fontchooser->priv->preview_text);
767 gtk_list_store_append (model, &iter);
768 gtk_list_store_set (model, &iter,
769 FAMILY_COLUMN, families[i],
770 FACE_COLUMN, faces[j],
771 PREVIEW_TITLE_COLUMN, family_and_face->str,
772 PREVIEW_TEXT_COLUMN, tmp->str,
775 /* Select the current font,
776 * the default font/face from the theme,
780 selected_font != NULL &&
781 pango_font_description_equal (selected_font, pango_desc))
787 strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
798 pango_font_description_free (pango_desc);
805 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
808 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
809 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
810 gtk_tree_path_free (path);
814 pango_font_description_free (selected_font);
816 g_string_free (family_and_face, TRUE);
817 g_string_free (tmp, TRUE);
822 visible_func (GtkTreeModel *model,
826 gboolean result = TRUE;
827 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
829 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
835 /* If there's no filter string we show the item */
836 if (strlen (search_text) == 0)
839 gtk_tree_model_get (model, iter,
840 PREVIEW_TITLE_COLUMN, &font_name,
843 if (font_name == NULL)
846 split_terms = g_strsplit (search_text, " ", 0);
847 term = split_terms[0];
849 while (term && result)
851 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
852 gchar* term_casefold = g_utf8_casefold (term, -1);
854 if (g_strrstr (font_name_casefold, term_casefold))
855 result = result && TRUE;
860 term = split_terms[n_terms];
862 g_free (term_casefold);
863 g_free (font_name_casefold);
867 g_strfreev (split_terms);
873 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
875 GtkFontChooserPrivate *priv = fontchooser->priv;
876 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
877 GtkCellRenderer *cell;
878 GtkTreeViewColumn *col;
880 priv->model = gtk_list_store_new (4,
881 PANGO_TYPE_FONT_FAMILY,
882 PANGO_TYPE_FONT_FACE,
886 priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
887 g_object_unref (priv->model);
889 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
890 visible_func, (gpointer)priv, NULL);
892 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
893 g_object_unref (priv->filter_model);
895 gtk_tree_view_set_rules_hint (treeview, TRUE);
896 gtk_tree_view_set_headers_visible (treeview, FALSE);
898 cell = gtk_cell_renderer_text_new ();
899 col = gtk_tree_view_column_new_with_attributes ("Family",
901 "markup", PREVIEW_TEXT_COLUMN,
904 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
906 gtk_tree_view_append_column (treeview, col);
908 populate_list (fontchooser, treeview, priv->model);
912 gtk_font_chooser_dispose (GObject *object)
914 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
915 GtkFontChooserPrivate *priv = fontchooser->priv;
917 if (priv->cursor_changed_handler != 0)
919 g_signal_handler_disconnect (priv->family_face_list,
920 priv->cursor_changed_handler);
921 priv->cursor_changed_handler = 0;
924 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
928 gtk_font_chooser_finalize (GObject *object)
930 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
931 GtkFontChooserPrivate *priv = fontchooser->priv;
934 g_free (priv->fontname);
937 g_object_unref (priv->family);
940 g_object_unref (priv->face);
942 if (priv->filter_data_destroy)
943 priv->filter_data_destroy (priv->filter_data);
945 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
949 gtk_font_chooser_screen_changed (GtkWidget *widget,
950 GdkScreen *previous_screen)
952 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
953 GtkFontChooserPrivate *priv = fontchooser->priv;
955 populate_list (fontchooser,
956 GTK_TREE_VIEW (priv->family_face_list),
960 gtk_font_chooser_select_font_name (fontchooser);
964 gtk_font_chooser_style_updated (GtkWidget *widget)
966 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
968 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
970 populate_list (fontchooser,
971 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
972 fontchooser->priv->model);
976 * gtk_font_chooser_get_family:
977 * @fontchooser: a #GtkFontChooser
979 * Gets the #PangoFontFamily representing the selected font family.
980 * Font families are a collection of font faces.
982 * Return value: (transfer none): A #PangoFontFamily representing the
983 * selected font family. The returned object is owned by @fontchooser
984 * and must not be modified or freed.
989 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
991 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
993 return fontchooser->priv->family;
997 * gtk_font_chooser_get_face:
998 * @fontchooser: a #GtkFontChooser
1000 * Gets the #PangoFontFace representing the selected font group
1001 * details (i.e. family, slant, weight, width, etc).
1003 * Return value: (transfer none): A #PangoFontFace representing the
1004 * selected font group details. The returned object is owned by
1005 * @fontchooser and must not be modified or freed.
1010 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1012 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1014 return fontchooser->priv->face;
1018 * gtk_font_chooser_get_size:
1019 * @fontchooser: a #GtkFontChooser
1021 * The selected font size.
1023 * Return value: A n integer representing the selected font size,
1024 * or -1 if no font size is selected.
1029 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1031 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1033 return fontchooser->priv->size;
1037 * gtk_font_chooser_get_font_name:
1038 * @fontchooser: a #GtkFontChooser
1040 * Gets the currently-selected font name.
1042 * Note that this can be a different string than what you set with
1043 * gtk_font_chooser_set_font_name(), as the font chooser widget may
1044 * normalize font names and thus return a string with a different
1045 * structure. For example, "Helvetica Italic Bold 12" could be
1046 * normalized to "Helvetica Bold Italic 12".
1048 * Use pango_font_description_equal() if you want to compare two
1049 * font descriptions.
1051 * Return value: (transfer full) (allow-none): A string with the name
1052 * of the current font, or %NULL if no font is selected. You must
1053 * free this string with g_free().
1058 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1061 gchar *font_desc_name;
1062 PangoFontDescription *desc;
1064 if (!fontchooser->priv->face)
1067 desc = pango_font_face_describe (fontchooser->priv->face);
1068 font_desc_name = pango_font_description_to_string (desc);
1069 pango_font_description_free (desc);
1071 font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1072 g_free (font_desc_name);
1077 * gtk_font_chooser_set_font_name:
1078 * @fontchooser: a #GtkFontChooser
1079 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1081 * Sets the currently-selected font.
1083 * Return value: %TRUE if the font could be set successfully; %FALSE
1084 * if no such font exists or if the @fontchooser doesn't belong
1085 * to a particular screen yet.
1090 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1091 const gchar *fontname)
1093 GtkFontChooserPrivate *priv = fontchooser->priv;
1094 gboolean found = FALSE;
1096 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1097 g_return_val_if_fail (fontname != NULL, FALSE);
1100 g_free (priv->fontname);
1101 priv->fontname = g_strdup (fontname);
1103 if (gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1104 found = gtk_font_chooser_select_font_name (fontchooser);
1106 g_object_notify (G_OBJECT (fontchooser), "font-name");
1112 gtk_font_chooser_select_font_name (GtkFontChooser *fontchooser)
1114 GtkFontChooserPrivate *priv = fontchooser->priv;
1118 PangoFontDescription *desc;
1119 gboolean found = FALSE;
1121 desc = pango_font_description_from_string (priv->fontname);
1122 family_name = (gchar*)pango_font_description_get_family (desc);
1124 g_free (priv->fontname);
1125 priv->fontname = NULL;
1129 pango_font_description_free (desc);
1133 /* We make sure the filter is clear */
1134 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1136 /* We find the matching family/face */
1137 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter_model), &iter);
1139 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter_model), &iter))
1141 PangoFontFace *face;
1142 PangoFontDescription *tmp_desc;
1144 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
1148 tmp_desc = pango_font_face_describe (face);
1149 if (pango_font_description_get_size_is_absolute (desc))
1150 pango_font_description_set_absolute_size (tmp_desc,
1151 pango_font_description_get_size (desc));
1153 pango_font_description_set_size (tmp_desc,
1154 pango_font_description_get_size (desc));
1156 if (pango_font_description_equal (desc, tmp_desc))
1159 gint size = pango_font_description_get_size (desc);
1163 if (pango_font_description_get_size_is_absolute (desc))
1164 size = size * PANGO_SCALE;
1165 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1166 size / PANGO_SCALE);
1169 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model), &iter);
1173 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1177 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1183 gtk_tree_path_free (path);
1189 g_object_unref (face);
1190 pango_font_description_free (tmp_desc);
1196 pango_font_description_free (desc);
1202 * gtk_font_chooser_get_preview_text:
1203 * @fontchooser: a #GtkFontChooser
1205 * Gets the text displayed in the preview area.
1207 * Return value: (transfer none): the text displayed in the
1208 * preview area. This string is owned by the widget and
1209 * should not be modified or freed
1214 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1216 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1218 return (const gchar*)fontchooser->priv->preview_text;
1223 * gtk_font_chooser_set_preview_text:
1224 * @fontchooser: a #GtkFontChooser
1225 * @text: (transfer none): the text to display in the preview area
1227 * Sets the text displayed in the preview area.
1228 * The @text is used to show how the selected font looks.
1233 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1236 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1237 g_return_if_fail (text != NULL);
1239 g_free (fontchooser->priv->preview_text);
1240 fontchooser->priv->preview_text = g_strdup (text);
1242 populate_list (fontchooser,
1243 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1244 fontchooser->priv->model);
1246 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1248 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1252 * gtk_font_chooser_get_show_preview_entry:
1253 * @fontchooser: a #GtkFontChooser
1255 * Returns whether the preview entry is shown or not.
1257 * Return value: %TRUE if the preview entry is shown
1258 * or %FALSE if it is hidden.
1263 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1265 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1267 return fontchooser->priv->show_preview_entry;
1271 * gtk_font_chooser_set_show_preview_entry:
1272 * @fontchooser: a #GtkFontChooser
1273 * @show_preview_entry: whether to show the editable preview entry or not
1275 * Shows or hides the editable preview entry.
1280 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1281 gboolean show_preview_entry)
1283 GtkFontChooserPrivate *priv = fontchooser->priv;
1285 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1287 if (priv->show_preview_entry != show_preview_entry)
1289 fontchooser->priv->show_preview_entry = show_preview_entry;
1291 if (show_preview_entry)
1292 gtk_widget_show (fontchooser->priv->preview);
1294 gtk_widget_hide (fontchooser->priv->preview);
1296 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1301 * gtk_font_chooser_set_filter_func:
1302 * @fontchooser: a #GtkFontChooser
1303 * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
1304 * @data: data to pass to @filter
1305 * @destroy: function to call to free @data when it is no longer needed
1307 * Adds a filter function that decides which fonts to display
1308 * in the font chooser.
1313 gtk_font_chooser_set_filter_func (GtkFontChooser *fontchooser,
1314 GtkFontFilterFunc filter,
1316 GDestroyNotify destroy)
1318 GtkFontChooserPrivate *priv = fontchooser->priv;
1320 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1322 if (priv->filter_data_destroy)
1323 priv->filter_data_destroy (priv->filter_data);
1325 priv->filter_func = filter;
1326 priv->filter_data = data;
1327 priv->filter_data_destroy = destroy;
1329 populate_list (fontchooser,
1330 GTK_TREE_VIEW (priv->family_face_list),