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 "gtkwidget.h"
54 * SECTION:gtkfontchooser
55 * @Short_description: A widget for selecting fonts
56 * @Title: GtkFontChooser
57 * @See_also: #GtkFontChooserDialog
59 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
60 * allowing the user to select a font.
61 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
64 * To set the font which is initially selected, use
65 * gtk_font_chooser_set_font_name().
67 * To get the selected font use gtk_font_chooser_get_font_name().
69 * To change the text which is shown in the preview area, use
70 * gtk_font_chooser_set_preview_text().
76 struct _GtkFontChooserPrivate
78 GtkWidget *search_entry;
79 GtkWidget *family_face_list;
80 GtkWidget *list_scrolled_window;
81 GtkWidget *empty_list;
87 gboolean show_preview_entry;
90 GtkWidget *size_slider;
94 PangoFontFamily *family;
96 gulong cursor_changed_handler;
99 #define DEFAULT_FONT_NAME "Sans 10"
100 #define MAX_FONT_SIZE 999
102 /* This is the initial fixed height and the top padding of the preview entry */
103 #define PREVIEW_HEIGHT 72
104 #define PREVIEW_TOP_PADDING 6
106 /* These are the sizes of the font, style & size lists. */
107 #define FONT_LIST_HEIGHT 136
108 #define FONT_LIST_WIDTH 190
109 #define FONT_STYLE_LIST_WIDTH 170
110 #define FONT_SIZE_LIST_WIDTH 60
112 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
114 #define NO_FONT_MATCHED_SEARCH "No fonts matched your search. You can revise your search and try again."
116 /* These are what we use as the standard font sizes, for the size list.
118 static const gint font_sizes[] = {
119 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
126 PROP_SHOW_PREVIEW_ENTRY
137 static void gtk_font_chooser_set_property (GObject *object,
141 static void gtk_font_chooser_get_property (GObject *object,
145 static void gtk_font_chooser_finalize (GObject *object);
146 static void gtk_font_chooser_dispose (GObject *object);
148 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
149 GdkScreen *previous_screen);
150 static void gtk_font_chooser_style_updated (GtkWidget *widget);
152 static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
153 PangoFontFamily *family);
154 static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
155 PangoFontFace *face);
157 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
159 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
162 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
167 widget_class->screen_changed = gtk_font_chooser_screen_changed;
168 widget_class->style_updated = gtk_font_chooser_style_updated;
170 gobject_class->dispose = gtk_font_chooser_dispose;
171 gobject_class->finalize = gtk_font_chooser_finalize;
172 gobject_class->set_property = gtk_font_chooser_set_property;
173 gobject_class->get_property = gtk_font_chooser_get_property;
175 g_object_class_install_property (gobject_class,
177 g_param_spec_string ("font-name",
179 P_("The string that represents this font"),
181 GTK_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_string ("preview-text",
186 P_("The text to display in order to demonstrate the selected font"),
187 pango_language_get_sample_string (NULL),
188 GTK_PARAM_READWRITE));
190 g_object_class_install_property (gobject_class,
191 PROP_SHOW_PREVIEW_ENTRY,
192 g_param_spec_boolean ("show-preview-entry",
193 P_("Show preview text entry"),
194 P_("Whether the preview text entry is shown or not"),
196 GTK_PARAM_READWRITE));
198 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
202 gtk_font_chooser_set_property (GObject *object,
207 GtkFontChooser *fontchooser;
209 fontchooser = GTK_FONT_CHOOSER (object);
214 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
216 case PROP_PREVIEW_TEXT:
217 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
219 case PROP_SHOW_PREVIEW_ENTRY:
220 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 gtk_font_chooser_get_property (GObject *object,
234 GtkFontChooser *fontchooser;
236 fontchooser = GTK_FONT_CHOOSER (object);
241 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
243 case PROP_PREVIEW_TEXT:
244 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
246 case PROP_SHOW_PREVIEW_ENTRY:
247 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 text_changed_cb (GtkEntry *entry,
260 GtkFontChooserPrivate *priv = fc->priv;
263 text = gtk_entry_get_text (entry);
265 if (text == NULL || text[0] == '\0')
269 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
270 g_object_set (G_OBJECT (priv->search_entry),
271 "secondary-icon-gicon", icon,
272 "secondary-icon-activatable", FALSE,
273 "secondary-icon-sensitive", FALSE,
275 g_object_unref (icon);
279 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
283 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
284 g_object_set (G_OBJECT (priv->search_entry),
285 "secondary-icon-gicon", icon,
286 "secondary-icon-activatable", TRUE,
287 "secondary-icon-sensitive", TRUE,
289 g_object_unref (icon);
293 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
297 icon_press_cb (GtkEntry *entry,
298 GtkEntryIconPosition pos,
302 gtk_entry_set_text (entry, "");
306 slider_change_cb (GtkAdjustment *adjustment,
309 GtkFontChooser *fc = (GtkFontChooser*)user_data;
310 GtkFontChooserPrivate *priv = fc->priv;
311 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
312 gdouble slider_value = gtk_adjustment_get_value (adjustment);
313 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
315 if (slider_value != spin_value)
316 gtk_adjustment_set_value (spin_adj,
317 gtk_adjustment_get_value (adjustment));
321 spin_change_cb (GtkAdjustment *adjustment,
324 PangoFontDescription *desc;
325 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
326 GtkFontChooserPrivate *priv = fontchooser->priv;
327 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
329 gdouble size = gtk_adjustment_get_value (adjustment);
330 priv->size = ((gint)size) * PANGO_SCALE;
332 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
333 pango_font_description_set_size (desc, priv->size);
334 gtk_widget_override_font (priv->preview, desc);
336 g_object_notify (G_OBJECT (fontchooser), "font-name");
338 /* If the new value is lower than the lower bound of the slider, we set
339 * the slider adjustment to the lower bound value if it is not already set
341 if (size < gtk_adjustment_get_lower (slider_adj) &&
342 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
343 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
345 /* If the new value is upper than the upper bound of the slider, we set
346 * the slider adjustment to the upper bound value if it is not already set
348 else if (size > gtk_adjustment_get_upper (slider_adj) &&
349 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
350 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
352 /* If the new value is not already set on the slider we set it */
353 else if (size != gtk_adjustment_get_value (slider_adj))
354 gtk_adjustment_set_value (slider_adj, size);
356 gtk_widget_queue_draw (priv->preview);
360 set_range_marks (GtkFontChooserPrivate *priv,
361 GtkWidget *size_slider,
371 sizes = (gint*)font_sizes;
372 length = G_N_ELEMENTS (font_sizes);
375 gtk_scale_clear_marks (GTK_SCALE (size_slider));
377 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
379 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
380 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
382 value = gtk_adjustment_get_value (adj);
383 if (value > (gdouble) sizes[length-1])
384 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
385 else if (value < (gdouble) sizes[0])
386 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
388 for (i = 0; i < length; i++)
389 gtk_scale_add_mark (GTK_SCALE (size_slider),
391 GTK_POS_BOTTOM, NULL);
395 cursor_changed_cb (GtkTreeView *treeview,
398 PangoFontFamily *family;
400 PangoFontDescription *desc;
406 GtkTreePath *path = gtk_tree_path_new ();
408 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
410 gtk_tree_view_get_cursor (treeview, &path, NULL);
415 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
417 gtk_tree_path_free (path);
422 gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
424 FAMILY_COLUMN, &family,
427 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
429 gtk_tree_path_free (path);
432 if (!face || !family)
434 g_object_unref (face);
435 g_object_unref (family);
439 desc = pango_font_face_describe (face);
440 pango_font_description_set_size (desc, fontchooser->priv->size);
441 gtk_widget_override_font (fontchooser->priv->preview, desc);
443 pango_font_face_list_sizes (face, &sizes, &n_sizes);
444 /* It seems not many fonts actually have a sane set of sizes */
445 for (i = 0; i < n_sizes; i++)
446 sizes[i] = sizes[i] / PANGO_SCALE;
448 set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
450 gtk_font_chooser_ref_family (fontchooser, family);
451 gtk_font_chooser_ref_face (fontchooser, face);
454 g_object_unref ((gpointer)family);
455 g_object_unref ((gpointer)face);
456 pango_font_description_free(desc);
458 g_object_notify (G_OBJECT (fontchooser), "font-name");
462 zoom_preview_cb (GtkWidget *scrolled_window,
463 GdkEventScroll *event,
466 GtkFontChooser *fc = (GtkFontChooser*)user_data;
467 GtkFontChooserPrivate *priv = fc->priv;
469 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
471 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
472 gtk_adjustment_set_value (adj,
473 gtk_adjustment_get_value (adj) +
474 gtk_adjustment_get_step_increment (adj));
475 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
476 gtk_adjustment_set_value (adj,
477 gtk_adjustment_get_value (adj) -
478 gtk_adjustment_get_step_increment (adj));
484 row_inserted_cb (GtkTreeModel *model,
489 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
490 GtkFontChooserPrivate *priv = fontchooser->priv;
492 if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
495 g_object_ref (priv->empty_list);
496 gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
498 gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
499 priv->family_face_list);
504 row_deleted_cb (GtkTreeModel *model,
508 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
509 GtkFontChooserPrivate *priv = fontchooser->priv;
511 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
513 if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
514 priv->family_face_list)
516 g_object_ref (priv->family_face_list);
517 gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
518 priv->family_face_list);
519 gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
527 gtk_font_chooser_init (GtkFontChooser *fontchooser)
530 GtkFontChooserPrivate *priv;
531 PangoFontDescription *font_desc;
532 GtkWidget *scrolled_win;
536 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
537 GTK_TYPE_FONT_CHOOSER,
538 GtkFontChooserPrivate);
540 priv = fontchooser->priv;
542 /* Default preview string */
543 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
544 priv->show_preview_entry = TRUE;
546 /* Getting the default size */
547 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
548 priv->size = pango_font_description_get_size (font_desc);
552 gtk_widget_push_composite_child ();
554 /* Creating fundamental widgets for the private struct */
555 priv->search_entry = gtk_entry_new ();
556 priv->family_face_list = gtk_tree_view_new ();
557 priv->preview = gtk_entry_new ();
558 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
559 (gdouble) font_sizes[0],
560 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
563 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
565 /** Bootstrapping widget layout **/
566 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
568 /* Main font family/face view */
569 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
570 scrolled_win = priv->list_scrolled_window;
571 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
572 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
573 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
574 GTK_SHADOW_ETCHED_IN);
575 gtk_container_add (GTK_CONTAINER (scrolled_win),
576 priv->family_face_list);
578 /* Text to display when list is empty */
579 priv->empty_list = gtk_text_view_new ();
580 gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->empty_list)),
581 _(NO_FONT_MATCHED_SEARCH),
585 grid = gtk_grid_new ();
586 sub_grid = gtk_grid_new ();
588 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
589 gtk_grid_set_row_spacing (GTK_GRID (sub_grid), 6);
591 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
592 gtk_grid_attach (GTK_GRID (grid), scrolled_win, 0, 1, 3, 1);
593 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 3, 1);
594 gtk_grid_attach (GTK_GRID (grid), sub_grid, 0, 4, 3, 1);
596 gtk_widget_set_hexpand (GTK_WIDGET (sub_grid), TRUE);
597 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider, 0, 3, 2, 1);
598 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin, 2, 3, 1, 1);
600 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
601 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
602 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
604 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
605 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
607 gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
608 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
610 /* Setting the adjustment values for the size slider */
611 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
612 (gdouble)(priv->size / PANGO_SCALE));
613 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
614 (gdouble)(priv->size / PANGO_SCALE));
616 gtk_widget_show_all (GTK_WIDGET (fontchooser));
617 gtk_widget_hide (GTK_WIDGET (fontchooser));
619 /* Treeview column and model bootstrapping */
620 gtk_font_chooser_bootstrap_fontlist (fontchooser);
622 /* Set default preview text */
623 gtk_entry_set_text (GTK_ENTRY (priv->preview),
624 pango_language_get_sample_string (NULL));
626 /* Set search icon and place holder text */
627 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
628 g_object_set (G_OBJECT (priv->search_entry),
629 "secondary-icon-gicon", icon,
630 "secondary-icon-activatable", FALSE,
631 "secondary-icon-sensitive", FALSE,
633 g_object_unref (icon);
635 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
637 /** Callback connections **/
638 g_signal_connect (priv->search_entry, "notify::text",
639 G_CALLBACK (text_changed_cb), fontchooser);
640 g_signal_connect (priv->search_entry,
641 "icon-press", G_CALLBACK (icon_press_cb), NULL);
643 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
644 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
645 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
646 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
648 priv->cursor_changed_handler =
649 g_signal_connect (priv->family_face_list, "cursor-changed",
650 G_CALLBACK (cursor_changed_cb), fontchooser);
652 /* Zoom on preview scroll*/
653 g_signal_connect (priv->preview, "scroll-event",
654 G_CALLBACK (zoom_preview_cb), fontchooser);
656 g_signal_connect (priv->size_slider, "scroll-event",
657 G_CALLBACK (zoom_preview_cb), fontchooser);
659 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
661 /* Font list empty hides the scrolledwindow */
663 g_signal_connect (G_OBJECT (priv->filter), "row-deleted",
664 G_CALLBACK (row_deleted_cb), fontchooser);
665 g_signal_connect (G_OBJECT (priv->filter), "row-inserted",
666 G_CALLBACK (row_inserted_cb), fontchooser);
669 /* Set default focus */
670 gtk_widget_pop_composite_child ();
674 * gtk_font_chooser_new:
676 * Creates a new #GtkFontChooser.
678 * Return value: a new #GtkFontChooser
683 gtk_font_chooser_new (void)
685 GtkFontChooser *fontchooser;
687 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
689 return GTK_WIDGET (fontchooser);
693 cmp_families (const void *a,
696 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
697 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
699 return g_utf8_collate (a_name, b_name);
703 populate_list (GtkFontChooser *fontchooser,
704 GtkTreeView *treeview,
707 GtkStyleContext *style_context;
708 PangoFontDescription *default_font;
710 GtkTreeIter match_row;
714 PangoFontFamily **families;
716 GString *family_and_face;
718 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
721 tmp = g_string_new (NULL);
722 family_and_face = g_string_new (NULL);
724 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
728 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
730 gtk_list_store_clear (model);
732 /* Get row header font color */
733 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
736 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
739 /* Iterate over families and faces */
740 for (i = 0; i < n_families; i++)
743 PangoFontFace **faces;
746 const gchar *fam_name = pango_font_family_get_name (families[i]);
748 pango_font_family_list_faces (families[i], &faces, &n_faces);
750 for (j = 0; j < n_faces; j++)
752 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
753 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
754 gchar *font_desc = pango_font_description_to_string (pango_desc);
756 /* foreground_color, family_name, face_name, desc, sample string */
757 g_string_printf (family_and_face, "%s %s", fam_name, face_name);
759 g_string_printf (tmp, ROW_FORMAT_STRING,
760 family_and_face->str,
762 fontchooser->priv->preview_text);
764 gtk_list_store_append (model, &iter);
765 gtk_list_store_set (model, &iter,
766 FAMILY_COLUMN, families[i],
767 FACE_COLUMN, faces[j],
768 PREVIEW_TITLE_COLUMN, family_and_face->str,
769 PREVIEW_TEXT_COLUMN, tmp->str,
772 /* Select the first font or the default font/face from the style context */
773 if ((i == 0 && j == 0) ||
774 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
777 pango_font_description_free(pango_desc);
784 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
788 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
789 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
790 gtk_tree_path_free(path);
793 g_string_free (family_and_face, TRUE);
794 g_string_free (tmp, TRUE);
799 visible_func (GtkTreeModel *model,
803 gboolean result = TRUE;
804 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
806 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
812 /* If there's no filter string we show the item */
813 if (strlen (search_text) == 0)
816 gtk_tree_model_get (model, iter,
817 PREVIEW_TITLE_COLUMN, &font_name,
820 if (font_name == NULL)
823 split_terms = g_strsplit (search_text, " ", 0);
824 term = split_terms[0];
826 while (term && result)
828 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
829 gchar* term_casefold = g_utf8_casefold (term, -1);
831 if (g_strrstr (font_name_casefold, term_casefold))
832 result = result && TRUE;
837 term = split_terms[n_terms];
839 g_free (term_casefold);
840 g_free (font_name_casefold);
844 g_strfreev (split_terms);
850 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser)
852 GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
853 GtkCellRenderer *cell;
854 GtkTreeViewColumn *col;
856 fontchooser->priv->model = gtk_list_store_new (4,
857 PANGO_TYPE_FONT_FAMILY,
858 PANGO_TYPE_FONT_FACE,
862 fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
864 g_object_unref (fontchooser->priv->model);
866 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
868 (gpointer)fontchooser->priv,
871 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
872 g_object_unref (fontchooser->priv->filter);
874 gtk_tree_view_set_rules_hint (treeview, TRUE);
875 gtk_tree_view_set_headers_visible (treeview, FALSE);
877 cell = gtk_cell_renderer_text_new ();
878 col = gtk_tree_view_column_new_with_attributes ("Family",
880 "markup", PREVIEW_TEXT_COLUMN,
883 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
885 gtk_tree_view_append_column (treeview, col);
887 populate_list (fontchooser, treeview, fontchooser->priv->model);
891 gtk_font_chooser_dispose (GObject *object)
893 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
894 GtkFontChooserPrivate *priv = fontchooser->priv;
896 if (priv->cursor_changed_handler != 0)
898 g_signal_handler_disconnect (priv->family_face_list,
899 priv->cursor_changed_handler);
900 priv->cursor_changed_handler = 0;
903 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->dispose (object);
907 gtk_font_chooser_finalize (GObject *object)
909 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
911 gtk_font_chooser_ref_family (fontchooser, NULL);
912 gtk_font_chooser_ref_face (fontchooser, NULL);
914 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
918 gtk_font_chooser_screen_changed (GtkWidget *widget,
919 GdkScreen *previous_screen)
921 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
923 populate_list (fontchooser,
924 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
925 fontchooser->priv->model);
929 gtk_font_chooser_style_updated (GtkWidget *widget)
931 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
933 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
935 populate_list (fontchooser,
936 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
937 fontchooser->priv->model);
941 gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
942 PangoFontFamily *family)
944 GtkFontChooserPrivate *priv = fontchooser->priv;
947 family = g_object_ref (family);
949 g_object_unref (priv->family);
950 priv->family = family;
954 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
957 GtkFontChooserPrivate *priv = fontchooser->priv;
960 face = g_object_ref (face);
962 g_object_unref (priv->face);
967 * gtk_font_chooser_get_family:
968 * @fontchooser: a #GtkFontChooser
970 * Gets the #PangoFontFamily representing the selected font family.
971 * Font families are a collection of font faces.
973 * Return value: (transfer none): A #PangoFontFamily representing the
974 * selected font family. The returned object is owned by @fontchooser
975 * and must not be modified or freed.
980 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
982 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
984 return fontchooser->priv->family;
988 * gtk_font_chooser_get_face:
989 * @fontchooser: a #GtkFontChooser
991 * Gets the #PangoFontFace representing the selected font group
992 * details (i.e. family, slant, weight, width, etc).
994 * Return value: (transfer none): A #PangoFontFace representing the
995 * selected font group details. The returned object is owned by
996 * @fontchooser and must not be modified or freed.
1001 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1003 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1005 return fontchooser->priv->face;
1009 * gtk_font_chooser_get_size:
1010 * @fontchooser: a #GtkFontChooser
1012 * The selected font size.
1014 * Return value: A n integer representing the selected font size,
1015 * or -1 if no font size is selected.
1020 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1022 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1024 return fontchooser->priv->size;
1028 * gtk_font_chooser_get_font_name:
1029 * @fontchooser: a #GtkFontChooser
1031 * Gets the currently-selected font name.
1033 * Note that this can be a different string than what you set with
1034 * gtk_font_chooser_set_font_name(), as the font chooser widget may
1035 * normalize font names and thus return a string with a different
1036 * structure. For example, "Helvetica Italic Bold 12" could be
1037 * normalized to "Helvetica Bold Italic 12".
1039 * Use pango_font_description_equal() if you want to compare two
1040 * font descriptions.
1042 * Return value: (transfer full) (allow-none): A string with the name
1043 * of the current font, or %NULL if no font is selected. You must
1044 * free this string with g_free().
1049 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1052 gchar *font_desc_name;
1053 PangoFontDescription *desc;
1055 if (!fontchooser->priv->face)
1058 desc = pango_font_face_describe (fontchooser->priv->face);
1059 font_desc_name = pango_font_description_to_string (desc);
1060 pango_font_description_free (desc);
1062 font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1063 g_free (font_desc_name);
1068 * gtk_font_chooser_set_font_name:
1069 * @fontchooser: a #GtkFontChooser
1070 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1072 * Sets the currently-selected font.
1074 * Note that the @fontchooser needs to know the screen in which
1075 * it will appear for this to work; this can be guaranteed by simply
1076 * making sure that the @fontchooser is inserted in a toplevel window
1077 * before you call this function.
1079 * Return value: %TRUE if the font could be set successfully; %FALSE
1080 * if no such font exists or if the @fontchooser doesn't belong
1081 * to a particular screen yet.
1086 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1087 const gchar *fontname)
1089 GtkFontChooserPrivate *priv = fontchooser->priv;
1093 PangoFontDescription *desc;
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);
1099 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1102 desc = pango_font_description_from_string (fontname);
1103 family_name = (gchar*)pango_font_description_get_family (desc);
1107 pango_font_description_free (desc);
1111 /* We make sure the filter is clear */
1112 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1114 /* We find the matching family/face */
1115 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1117 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1119 PangoFontFace *face;
1120 PangoFontDescription *tmp_desc;
1122 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1126 tmp_desc = pango_font_face_describe (face);
1127 if (pango_font_description_get_size_is_absolute (desc))
1128 pango_font_description_set_absolute_size (tmp_desc,
1129 pango_font_description_get_size (desc));
1131 pango_font_description_set_size (tmp_desc,
1132 pango_font_description_get_size (desc));
1134 if (pango_font_description_equal (desc, tmp_desc))
1137 gint size = pango_font_description_get_size (desc);
1141 if (pango_font_description_get_size_is_absolute (desc))
1142 size = size * PANGO_SCALE;
1143 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1144 size / PANGO_SCALE);
1147 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1152 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1156 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1162 gtk_tree_path_free (path);
1168 g_object_unref (face);
1169 pango_font_description_free (tmp_desc);
1175 pango_font_description_free (desc);
1176 g_object_notify (G_OBJECT (fontchooser), "font-name");
1182 * gtk_font_chooser_get_preview_text:
1183 * @fontchooser: a #GtkFontChooser
1185 * Gets the text displayed in the preview area.
1187 * Return value: (transfer none): the text displayed in the
1188 * preview area. This string is owned by the widget and
1189 * should not be modified or freed
1194 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1196 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1198 return (const gchar*)fontchooser->priv->preview_text;
1203 * gtk_font_chooser_set_preview_text:
1204 * @fontchooser: a #GtkFontChooser
1205 * @text: (transfer none): the text to display in the preview area
1207 * Sets the text displayed in the preview area.
1208 * The @text is used to show how the selected font looks.
1213 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1216 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1217 g_return_if_fail (text != NULL);
1219 g_free (fontchooser->priv->preview_text);
1220 fontchooser->priv->preview_text = g_strdup (text);
1222 populate_list (fontchooser,
1223 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1224 fontchooser->priv->model);
1226 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1228 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1232 * gtk_font_chooser_get_show_preview_entry:
1233 * @fontchooser: a #GtkFontChooser
1235 * Returns whether the preview entry is shown or not.
1237 * Return value: %TRUE if the preview entry is shown
1238 * or %FALSE if it is hidden.
1243 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1245 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1247 return fontchooser->priv->show_preview_entry;
1251 * gtk_font_chooser_set_show_preview_entry:
1252 * @fontchooser: a #GtkFontChooser
1253 * @show_preview_entry: whether to show the editable preview entry or not
1255 * Shows or hides the editable preview entry.
1260 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1261 gboolean show_preview_entry)
1263 GtkFontChooserPrivate *priv = fontchooser->priv;
1265 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1267 if (priv->show_preview_entry != show_preview_entry)
1269 fontchooser->priv->show_preview_entry = show_preview_entry;
1271 if (show_preview_entry)
1272 gtk_widget_show (fontchooser->priv->preview);
1274 gtk_widget_hide (fontchooser->priv->preview);
1276 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");