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 "gtktreeselection.h"
38 #include "gtktreeview.h"
40 #include "gtkscrolledwindow.h"
42 #include "gtkaccessible.h"
43 #include "gtkbuildable.h"
44 #include "gtkprivate.h"
45 #include "gtkalignment.h"
48 #include "gtkspinbutton.h"
49 #include "gtkwidget.h"
53 * SECTION:gtkfontchooser
54 * @Short_description: A widget for selecting fonts
55 * @Title: GtkFontChooser
56 * @See_also: #GtkFontChooserDialog
58 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
59 * allowing the user to select a font.
60 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
63 * To set the font which is initially selected, use
64 * gtk_font_chooser_set_font_name().
66 * To get the selected font use gtk_font_chooser_get_font_name().
68 * To change the text which is shown in the preview area, use
69 * gtk_font_chooser_set_preview_text().
75 struct _GtkFontChooserPrivate
77 GtkWidget *search_entry;
78 GtkWidget *family_face_list;
83 GtkWidget *preview_scrolled_window;
85 gboolean show_preview_entry;
88 GtkWidget *size_slider;
92 PangoFontFamily *family;
95 #define DEFAULT_FONT_NAME "Sans 10"
96 #define MAX_FONT_SIZE 999
98 /* This is the initial fixed height and the top padding of the preview entry */
99 #define PREVIEW_HEIGHT 72
100 #define PREVIEW_TOP_PADDING 6
102 /* These are the sizes of the font, style & size lists. */
103 #define FONT_LIST_HEIGHT 136
104 #define FONT_LIST_WIDTH 190
105 #define FONT_STYLE_LIST_WIDTH 170
106 #define FONT_SIZE_LIST_WIDTH 60
108 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
110 /* These are what we use as the standard font sizes, for the size list.
112 static const gint font_sizes[] = {
113 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
120 PROP_SHOW_PREVIEW_ENTRY
131 static void gtk_font_chooser_set_property (GObject *object,
135 static void gtk_font_chooser_get_property (GObject *object,
139 static void gtk_font_chooser_finalize (GObject *object);
141 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
142 GdkScreen *previous_screen);
143 static void gtk_font_chooser_style_updated (GtkWidget *widget);
145 static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
146 PangoFontFamily *family);
147 static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
148 PangoFontFace *face);
150 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
152 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
155 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
157 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
158 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
160 widget_class->screen_changed = gtk_font_chooser_screen_changed;
161 widget_class->style_updated = gtk_font_chooser_style_updated;
163 gobject_class->finalize = gtk_font_chooser_finalize;
164 gobject_class->set_property = gtk_font_chooser_set_property;
165 gobject_class->get_property = gtk_font_chooser_get_property;
167 g_object_class_install_property (gobject_class,
169 g_param_spec_string ("font-name",
171 P_("The string that represents this font"),
173 GTK_PARAM_READWRITE));
174 g_object_class_install_property (gobject_class,
176 g_param_spec_string ("preview-text",
178 P_("The text to display in order to demonstrate the selected font"),
179 pango_language_get_sample_string (NULL),
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (gobject_class,
183 PROP_SHOW_PREVIEW_ENTRY,
184 g_param_spec_boolean ("show-preview-entry",
185 P_("Show preview text entry"),
186 P_("Whether the preview text entry is shown or not"),
188 GTK_PARAM_READWRITE));
190 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
194 gtk_font_chooser_set_property (GObject *object,
199 GtkFontChooser *fontchooser;
201 fontchooser = GTK_FONT_CHOOSER (object);
206 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
208 case PROP_PREVIEW_TEXT:
209 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
211 case PROP_SHOW_PREVIEW_ENTRY:
212 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gtk_font_chooser_get_property (GObject *object,
226 GtkFontChooser *fontchooser;
228 fontchooser = GTK_FONT_CHOOSER (object);
233 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
235 case PROP_PREVIEW_TEXT:
236 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
238 case PROP_SHOW_PREVIEW_ENTRY:
239 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 deleted_text_cb (GtkEntryBuffer *buffer,
252 GtkFontChooser *fc = (GtkFontChooser*)user_data;
253 GtkFontChooserPrivate *priv = fc->priv;
254 GtkWidget *entry = priv->search_entry;
256 if (gtk_entry_buffer_get_length (buffer) == 0)
258 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
259 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
260 GTK_ENTRY_ICON_SECONDARY,
262 g_object_unref (icon);
265 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
269 inserted_text_cb (GtkEntryBuffer *buffer,
275 GtkFontChooser *fc = (GtkFontChooser*)user_data;
276 GtkFontChooserPrivate *priv = fc->priv;
277 GtkWidget *entry = priv->search_entry;
279 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
280 "edit-clear-symbolic") != 0 ||
281 g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
282 GTK_STOCK_CLEAR) != 0)
284 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
285 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
286 GTK_ENTRY_ICON_SECONDARY,
288 g_object_unref (icon);
291 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
295 icon_press_cb (GtkEntry *entry,
296 GtkEntryIconPosition pos,
300 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
304 slider_change_cb (GtkAdjustment *adjustment,
307 GtkFontChooser *fc = (GtkFontChooser*)user_data;
308 GtkFontChooserPrivate *priv = fc->priv;
309 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
310 gdouble slider_value = gtk_adjustment_get_value (adjustment);
311 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
313 if (slider_value != spin_value)
314 gtk_adjustment_set_value (spin_adj,
315 gtk_adjustment_get_value (adjustment));
319 spin_change_cb (GtkAdjustment *adjustment,
322 PangoFontDescription *desc;
323 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
324 GtkFontChooserPrivate *priv = fontchooser->priv;
325 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
327 gdouble size = gtk_adjustment_get_value (adjustment);
328 priv->size = ((gint)size) * PANGO_SCALE;
330 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
331 pango_font_description_set_size (desc, priv->size);
332 gtk_widget_override_font (priv->preview, desc);
334 g_object_notify (G_OBJECT (fontchooser), "font-name");
336 /* If the new value is lower than the lower bound of the slider, we set
337 * the slider adjustment to the lower bound value if it is not already set
339 if (size < gtk_adjustment_get_lower (slider_adj) &&
340 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
341 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
343 /* If the new value is upper than the upper bound of the slider, we set
344 * the slider adjustment to the upper bound value if it is not already set
346 else if (size > gtk_adjustment_get_upper (slider_adj) &&
347 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
348 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
350 /* If the new value is not already set on the slider we set it */
351 else if (size != gtk_adjustment_get_value (slider_adj))
352 gtk_adjustment_set_value (slider_adj, size);
354 gtk_widget_queue_draw (priv->preview);
358 set_range_marks (GtkFontChooserPrivate *priv,
359 GtkWidget *size_slider,
369 sizes = (gint*)font_sizes;
370 length = G_N_ELEMENTS (font_sizes);
373 gtk_scale_clear_marks (GTK_SCALE (size_slider));
375 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
377 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
378 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
380 value = gtk_adjustment_get_value (adj);
381 if (value > (gdouble) sizes[length-1])
382 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
383 else if (value < (gdouble) sizes[0])
384 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
386 for (i = 0; i < length; i++)
387 gtk_scale_add_mark (GTK_SCALE (size_slider),
389 GTK_POS_BOTTOM, NULL);
393 cursor_changed_cb (GtkTreeView *treeview,
396 PangoFontFamily *family;
398 PangoFontDescription *desc;
404 GtkTreePath *path = gtk_tree_path_new ();
406 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
408 gtk_tree_view_get_cursor (treeview, &path, NULL);
413 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
415 gtk_tree_path_free (path);
420 gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
422 FAMILY_COLUMN, &family,
425 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
427 gtk_tree_path_free (path);
430 if (!face || !family)
432 g_object_unref (face);
433 g_object_unref (family);
437 desc = pango_font_face_describe (face);
438 pango_font_description_set_size (desc, fontchooser->priv->size);
439 gtk_widget_override_font (fontchooser->priv->preview, desc);
441 pango_font_face_list_sizes (face, &sizes, &n_sizes);
442 /* It seems not many fonts actually have a sane set of sizes */
443 for (i = 0; i < n_sizes; i++)
444 sizes[i] = sizes[i] / PANGO_SCALE;
446 set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
448 gtk_font_chooser_ref_family (fontchooser, family);
449 gtk_font_chooser_ref_face (fontchooser, face);
452 g_object_unref ((gpointer)family);
453 g_object_unref ((gpointer)face);
454 pango_font_description_free(desc);
456 g_object_notify (G_OBJECT (fontchooser), "font-name");
460 zoom_preview_cb (GtkWidget *scrolled_window,
461 GdkEventScroll *event,
464 GtkFontChooser *fc = (GtkFontChooser*)user_data;
465 GtkFontChooserPrivate *priv = fc->priv;
467 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
469 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
470 gtk_adjustment_set_value (adj,
471 gtk_adjustment_get_value (adj) +
472 gtk_adjustment_get_step_increment (adj));
473 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
474 gtk_adjustment_set_value (adj,
475 gtk_adjustment_get_value (adj) -
476 gtk_adjustment_get_step_increment (adj));
481 gtk_font_chooser_init (GtkFontChooser *fontchooser)
484 GtkFontChooserPrivate *priv;
485 PangoFontDescription *font_desc;
486 GtkWidget *scrolled_win;
490 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
491 GTK_TYPE_FONT_CHOOSER,
492 GtkFontChooserPrivate);
494 priv = fontchooser->priv;
496 /* Default preview string */
497 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
498 priv->show_preview_entry = TRUE;
500 /* Getting the default size */
501 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
502 priv->size = pango_font_description_get_size (font_desc);
506 gtk_widget_push_composite_child ();
508 /* Creating fundamental widgets for the private struct */
509 priv->search_entry = gtk_entry_new ();
510 priv->family_face_list = gtk_tree_view_new ();
511 priv->preview = gtk_entry_new ();
512 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
513 (gdouble) font_sizes[0],
514 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
517 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
519 /** Bootstrapping widget layout **/
520 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
522 /* Main font family/face view */
523 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
524 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
525 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
526 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
527 GTK_SHADOW_ETCHED_IN);
528 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
531 grid = gtk_grid_new ();
532 sub_grid = gtk_grid_new ();
534 gtk_widget_set_margin_bottom (priv->search_entry, 6);
535 gtk_widget_set_margin_bottom (scrolled_win, 6);
536 gtk_widget_set_margin_bottom (priv->preview, 6);
537 gtk_widget_set_margin_right (priv->size_slider, 6);
539 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
540 gtk_grid_attach (GTK_GRID (grid), scrolled_win, 0, 1, 3, 1);
541 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 3, 1);
542 gtk_grid_attach (GTK_GRID (grid), sub_grid, 0, 3, 3, 1);
544 gtk_widget_set_hexpand (GTK_WIDGET (sub_grid), TRUE);
545 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider, 0, 3, 2, 1);
546 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin, 2, 3, 1, 1);
548 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
549 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
550 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
552 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
553 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
555 gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
556 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
558 /* Setting the adjustment values for the size slider */
559 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
560 (gdouble)(priv->size / PANGO_SCALE));
561 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
562 (gdouble)(priv->size / PANGO_SCALE));
564 gtk_widget_show_all (GTK_WIDGET (fontchooser));
565 gtk_widget_hide (GTK_WIDGET (fontchooser));
567 /* Treeview column and model bootstrapping */
568 gtk_font_chooser_bootstrap_fontlist (fontchooser);
570 /* Set default preview text */
571 gtk_entry_set_text (GTK_ENTRY (priv->preview),
572 pango_language_get_sample_string (NULL));
574 /* Set search icon and place holder text */
575 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
576 gtk_entry_set_icon_from_gicon (GTK_ENTRY (priv->search_entry),
577 GTK_ENTRY_ICON_SECONDARY,
579 g_object_unref (icon);
581 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
583 /** Callback connections **/
584 /* Connect to callback for the live search text entry */
585 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
586 "deleted-text", G_CALLBACK (deleted_text_cb), fontchooser);
587 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
588 "inserted-text", G_CALLBACK (inserted_text_cb), fontchooser);
589 g_signal_connect (G_OBJECT (priv->search_entry),
590 "icon-press", G_CALLBACK (icon_press_cb), NULL);
592 /* Size controls callbacks */
593 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
594 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
595 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
596 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
598 /* Font selection callback */
599 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
600 G_CALLBACK (cursor_changed_cb), fontchooser);
602 /* Zoom on preview scroll*/
603 g_signal_connect (G_OBJECT (priv->preview), "scroll-event",
604 G_CALLBACK (zoom_preview_cb), fontchooser);
606 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
607 G_CALLBACK (zoom_preview_cb), fontchooser);
609 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
611 /* Set default focus */
612 gtk_widget_pop_composite_child();
616 * gtk_font_chooser_new:
618 * Creates a new #GtkFontChooser.
620 * Return value: a new #GtkFontChooser
625 gtk_font_chooser_new (void)
627 GtkFontChooser *fontchooser;
629 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
631 return GTK_WIDGET (fontchooser);
635 cmp_families (const void *a,
638 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
639 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
641 return g_utf8_collate (a_name, b_name);
645 populate_list (GtkFontChooser *fontchooser,
646 GtkTreeView *treeview,
649 GtkStyleContext *style_context;
650 PangoFontDescription *default_font;
652 GtkTreeIter match_row;
656 PangoFontFamily **families;
658 GString *tmp = g_string_new (NULL);
659 GString *family_and_face = g_string_new (NULL);
661 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
665 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
667 gtk_list_store_clear (model);
669 /* Get row header font color */
670 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
673 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
676 /* Iterate over families and faces */
677 for (i = 0; i < n_families; i++)
680 PangoFontFace **faces;
683 const gchar *fam_name = pango_font_family_get_name (families[i]);
685 pango_font_family_list_faces (families[i], &faces, &n_faces);
687 for (j = 0; j < n_faces; j++)
689 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
690 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
691 gchar *font_desc = pango_font_description_to_string (pango_desc);
693 /* foreground_color, family_name, face_name, desc, sample string */
694 g_string_printf (family_and_face, "%s %s",
698 g_string_printf (tmp, ROW_FORMAT_STRING,
699 family_and_face->str,
701 fontchooser->priv->preview_text);
703 gtk_list_store_append (model, &iter);
704 gtk_list_store_set (model, &iter,
705 FAMILY_COLUMN, families[i],
706 FACE_COLUMN, faces[j],
707 PREVIEW_TITLE_COLUMN, family_and_face->str,
708 PREVIEW_TEXT_COLUMN, tmp->str,
711 /* Select the first font or the default font/face from the style context */
712 if ((i == 0 && j == 0) ||
713 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
716 pango_font_description_free(pango_desc);
723 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
727 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
728 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
729 gtk_tree_path_free(path);
732 g_string_free (family_and_face, TRUE);
733 g_string_free (tmp, TRUE);
738 visible_func (GtkTreeModel *model,
742 gboolean result = TRUE;
743 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
745 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
751 /* If there's no filter string we show the item */
752 if (strlen (search_text) == 0)
755 gtk_tree_model_get (model, iter,
756 PREVIEW_TITLE_COLUMN, &font_name,
759 if (font_name == NULL)
762 split_terms = g_strsplit (search_text, " ", 0);
763 term = split_terms[0];
765 while (term && result)
767 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
768 gchar* term_casefold = g_utf8_casefold (term, -1);
770 if (g_strrstr (font_name_casefold, term_casefold))
771 result = result && TRUE;
776 term = split_terms[n_terms];
778 g_free (term_casefold);
779 g_free (font_name_casefold);
783 g_strfreev (split_terms);
789 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser* fontchooser)
791 GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
792 GtkCellRenderer *cell;
793 GtkTreeViewColumn *col;
795 fontchooser->priv->model = gtk_list_store_new (4,
796 PANGO_TYPE_FONT_FAMILY,
797 PANGO_TYPE_FONT_FACE,
801 fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
803 g_object_unref (fontchooser->priv->model);
805 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
807 (gpointer)fontchooser->priv,
810 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
811 g_object_unref (fontchooser->priv->filter);
813 gtk_tree_view_set_rules_hint (treeview, TRUE);
814 gtk_tree_view_set_headers_visible (treeview, FALSE);
816 cell = gtk_cell_renderer_text_new ();
817 col = gtk_tree_view_column_new_with_attributes ("Family",
819 "markup", PREVIEW_TEXT_COLUMN,
822 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
824 gtk_tree_view_append_column (treeview, col);
826 populate_list (fontchooser, treeview, fontchooser->priv->model);
831 gtk_font_chooser_finalize (GObject *object)
833 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
835 gtk_font_chooser_ref_family (fontchooser, NULL);
836 gtk_font_chooser_ref_face (fontchooser, NULL);
838 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
843 gtk_font_chooser_screen_changed (GtkWidget *widget,
844 GdkScreen *previous_screen)
846 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
848 populate_list (fontchooser,
849 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
850 fontchooser->priv->model);
855 gtk_font_chooser_style_updated (GtkWidget *widget)
857 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
859 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
861 populate_list (fontchooser,
862 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
863 fontchooser->priv->model);
868 gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
869 PangoFontFamily *family)
871 GtkFontChooserPrivate *priv = fontchooser->priv;
874 family = g_object_ref (family);
876 g_object_unref (priv->family);
877 priv->family = family;
881 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
884 GtkFontChooserPrivate *priv = fontchooser->priv;
887 face = g_object_ref (face);
889 g_object_unref (priv->face);
895 * These functions are the main public interface for getting/setting the font.
899 * gtk_font_chooser_get_family:
900 * @fontchooser: a #GtkFontChooser
902 * Gets the #PangoFontFamily representing the selected font family.
904 * Return value: (transfer none): A #PangoFontFamily representing the
905 * selected font family. Font families are a collection of font
906 * faces. The returned object is owned by @fontchooser and must not
907 * be modified or freed.
912 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
914 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
916 return fontchooser->priv->family;
920 * gtk_font_chooser_get_face:
921 * @fontchooser: a #GtkFontChooser
923 * Gets the #PangoFontFace representing the selected font group
924 * details (i.e. family, slant, weight, width, etc).
926 * Return value: (transfer none): A #PangoFontFace representing the
927 * selected font group details. The returned object is owned by
928 * @fontchooser and must not be modified or freed.
933 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
935 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
937 return fontchooser->priv->face;
941 * gtk_font_chooser_get_size:
942 * @fontchooser: a #GtkFontChooser
944 * The selected font size.
946 * Return value: A n integer representing the selected font size,
947 * or -1 if no font size is selected.
952 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
954 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
956 return fontchooser->priv->size;
960 * gtk_font_chooser_get_font_name:
961 * @fontchooser: a #GtkFontChooser
963 * Gets the currently-selected font name.
965 * Note that this can be a different string than what you set with
966 * gtk_font_chooser_set_font_name(), as the font chooser widget may
967 * normalize font names and thus return a string with a different structure.
968 * For example, "Helvetica Italic Bold 12" could be normalized to
969 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
970 * if you want to compare two font descriptions.
972 * Return value: (transfer full) (allow-none): A string with the name of the
973 * current font, or %NULL if no font is selected. You must free this
974 * string with g_free().
979 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
982 PangoFontDescription *desc;
984 if (!fontchooser->priv->face)
987 desc = pango_font_face_describe (fontchooser->priv->face);
988 font_name = pango_font_description_to_string (desc);
989 pango_font_description_free (desc);
993 /* This sets the current font, then selecting the appropriate list rows. */
996 * gtk_font_chooser_set_font_name:
997 * @fontchooser: a #GtkFontChooser
998 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1000 * Sets the currently-selected font.
1002 * Note that the @fontchooser needs to know the screen in which it will appear
1003 * for this to work; this can be guaranteed by simply making sure that the
1004 * @fontchooser is inserted in a toplevel window before you call this function.
1006 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1007 * such font exists or if the @fontchooser doesn't belong to a particular
1013 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1014 const gchar *fontname)
1016 GtkFontChooserPrivate *priv = fontchooser->priv;
1020 PangoFontDescription *desc;
1021 gboolean found = FALSE;
1023 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1024 g_return_val_if_fail (fontname != NULL, FALSE);
1026 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1029 desc = pango_font_description_from_string (fontname);
1030 family_name = (gchar*)pango_font_description_get_family (desc);
1034 pango_font_description_free (desc);
1038 /* We make sure the filter is clear */
1039 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1041 /* We find the matching family/face */
1042 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1044 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1046 PangoFontFace *face;
1047 PangoFontDescription *tmp_desc;
1049 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1053 tmp_desc = pango_font_face_describe (face);
1054 if (pango_font_description_get_size_is_absolute (desc))
1055 pango_font_description_set_absolute_size (tmp_desc,
1056 pango_font_description_get_size (desc));
1058 pango_font_description_set_size (tmp_desc,
1059 pango_font_description_get_size (desc));
1062 if (pango_font_description_equal (desc, tmp_desc))
1065 gint size = pango_font_description_get_size (desc);
1069 if (pango_font_description_get_size_is_absolute (desc))
1070 size = size * PANGO_SCALE;
1071 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1072 size / PANGO_SCALE);
1075 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1080 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1084 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1090 gtk_tree_path_free (path);
1096 g_object_unref (face);
1097 pango_font_description_free (tmp_desc);
1103 pango_font_description_free (desc);
1104 g_object_notify (G_OBJECT (fontchooser), "font-name");
1110 * gtk_font_chooser_get_preview_text:
1111 * @fontchooser: a #GtkFontChooser
1113 * Gets the text displayed in the preview area.
1115 * Return value: (transfer none): the text displayed in the
1116 * preview area. This string is owned by the widget and
1117 * should not be modified or freed
1122 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1124 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1125 return (const gchar*)fontchooser->priv->preview_text;
1130 * gtk_font_chooser_set_preview_text:
1131 * @fontchooser: a #GtkFontChooser
1132 * @text: (transfer none): the text to display in the preview area
1134 * Sets the text displayed in the preview area.
1135 * The @text is used to show how the selected font looks.
1140 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1143 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1144 g_return_if_fail (text != NULL);
1146 g_free (fontchooser->priv->preview_text);
1147 fontchooser->priv->preview_text = g_strdup (text);
1149 populate_list (fontchooser,
1150 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1151 fontchooser->priv->model);
1153 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1155 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1159 * gtk_font_chooser_get_show_preview_entry:
1160 * @fontchooser: a #GtkFontChooser
1162 * Return value: %TRUE if the preview entry is shown or %FALSE if
1167 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1169 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1171 return fontchooser->priv->show_preview_entry;
1175 * gtk_font_chooser_set_show_preview_entry:
1176 * @fontchooser: a #GtkFontChooser
1177 * @show_preview_entry: whether to show the editable preview entry or not
1179 * Shows or hides the editable preview entry.
1183 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1184 gboolean show_preview_entry)
1186 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1188 if (show_preview_entry)
1189 gtk_widget_show (fontchooser->priv->preview_scrolled_window);
1191 gtk_widget_hide (fontchooser->priv->preview_scrolled_window);
1193 fontchooser->priv->show_preview_entry = show_preview_entry;
1194 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");