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;
93 PangoFontFamily *family;
96 #define DEFAULT_FONT_NAME "Sans 10"
97 #define MAX_FONT_SIZE 999
99 /* This is the initial fixed height and the top padding of the preview entry */
100 #define PREVIEW_HEIGHT 72
101 #define PREVIEW_TOP_PADDING 6
103 /* These are the sizes of the font, style & size lists. */
104 #define FONT_LIST_HEIGHT 136
105 #define FONT_LIST_WIDTH 190
106 #define FONT_STYLE_LIST_WIDTH 170
107 #define FONT_SIZE_LIST_WIDTH 60
109 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
111 /* These are what we use as the standard font sizes, for the size list.
113 #define FONT_SIZES_LENGTH 14
114 static const gint font_sizes[] = {
115 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
122 PROP_SHOW_PREVIEW_ENTRY
133 static void gtk_font_chooser_set_property (GObject *object,
137 static void gtk_font_chooser_get_property (GObject *object,
141 static void gtk_font_chooser_finalize (GObject *object);
143 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
144 GdkScreen *previous_screen);
145 static void gtk_font_chooser_style_updated (GtkWidget *widget);
147 static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
148 PangoFontFamily *family);
149 static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
150 PangoFontFace *face);
152 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
154 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
157 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
162 widget_class->screen_changed = gtk_font_chooser_screen_changed;
163 widget_class->style_updated = gtk_font_chooser_style_updated;
165 gobject_class->finalize = gtk_font_chooser_finalize;
166 gobject_class->set_property = gtk_font_chooser_set_property;
167 gobject_class->get_property = gtk_font_chooser_get_property;
169 g_object_class_install_property (gobject_class,
171 g_param_spec_string ("font-name",
173 P_("The string that represents this font"),
175 GTK_PARAM_READWRITE));
176 g_object_class_install_property (gobject_class,
178 g_param_spec_string ("preview-text",
180 P_("The text to display in order to demonstrate the selected font"),
181 pango_language_get_sample_string (NULL),
182 GTK_PARAM_READWRITE));
184 g_object_class_install_property (gobject_class,
185 PROP_SHOW_PREVIEW_ENTRY,
186 g_param_spec_boolean ("show-preview-entry",
187 P_("Show preview text entry"),
188 P_("Whether the preview text entry is shown or not"),
190 GTK_PARAM_READWRITE));
192 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
196 gtk_font_chooser_set_property (GObject *object,
201 GtkFontChooser *fontchooser;
203 fontchooser = GTK_FONT_CHOOSER (object);
208 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
210 case PROP_PREVIEW_TEXT:
211 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
213 case PROP_SHOW_PREVIEW_ENTRY:
214 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gtk_font_chooser_get_property (GObject *object,
228 GtkFontChooser *fontchooser;
230 fontchooser = GTK_FONT_CHOOSER (object);
235 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
237 case PROP_PREVIEW_TEXT:
238 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
240 case PROP_SHOW_PREVIEW_ENTRY:
241 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 deleted_text_cb (GtkEntryBuffer *buffer,
254 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
255 GtkWidget *entry = priv->search_entry;
257 if (gtk_entry_buffer_get_length (buffer) == 0)
259 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
260 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
261 GTK_ENTRY_ICON_SECONDARY,
263 g_object_unref (icon);
266 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
270 inserted_text_cb (GtkEntryBuffer *buffer,
276 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
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, gpointer data)
306 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
307 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
308 gdouble slider_value = gtk_adjustment_get_value (adjustment);
309 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
311 if (slider_value != spin_value)
312 gtk_adjustment_set_value (spin_adj,
313 gtk_adjustment_get_value (adjustment));
317 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
319 PangoFontDescription *desc;
320 GtkFontChooser *fontchooser = (GtkFontChooser*)data;
321 GtkFontChooserPrivate *priv = fontchooser->priv;
322 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
324 gdouble size = gtk_adjustment_get_value (adjustment);
325 priv->size = ((gint)size) * PANGO_SCALE;
327 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
328 pango_font_description_set_size (desc, priv->size);
329 gtk_widget_override_font (priv->preview, desc);
331 if (priv->stop_notify)
332 priv->stop_notify = FALSE;
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))
342 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
343 priv->stop_notify = TRUE;
346 /* If the new value is upper than the upper bound of the slider, we set
347 * the slider adjustment to the upper bound value if it is not already set
349 else if (size > gtk_adjustment_get_upper (slider_adj) &&
350 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
352 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
353 priv->stop_notify = TRUE;
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))
359 gtk_adjustment_set_value (slider_adj, size);
360 priv->stop_notify = TRUE;
363 gtk_widget_queue_draw (priv->preview);
367 set_range_marks (GtkFontChooserPrivate *priv,
368 GtkWidget *size_slider,
378 sizes = (gint*)font_sizes;
379 length = FONT_SIZES_LENGTH;
382 gtk_scale_clear_marks (GTK_SCALE (size_slider));
384 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
386 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
387 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
389 value = gtk_adjustment_get_value (adj);
390 if (value > (gdouble) sizes[length-1])
391 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
392 else if (value < (gdouble) sizes[0])
393 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
395 for (i=0; i<length; i++)
396 gtk_scale_add_mark (GTK_SCALE (size_slider),
398 GTK_POS_BOTTOM, NULL);
402 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
404 PangoFontFamily *family;
406 PangoFontDescription *desc;
412 GtkTreePath *path = gtk_tree_path_new ();
414 GtkFontChooser *fontchooser = (GtkFontChooser*)data;
416 gtk_tree_view_get_cursor (treeview, &path, NULL);
421 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
423 gtk_tree_path_free (path);
428 gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
430 FAMILY_COLUMN, &family,
433 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
435 gtk_tree_path_free (path);
438 if (!face || !family)
440 g_object_unref (face);
441 g_object_unref (family);
445 desc = pango_font_face_describe (face);
446 pango_font_description_set_size (desc, fontchooser->priv->size);
447 gtk_widget_override_font (fontchooser->priv->preview, desc);
449 pango_font_face_list_sizes (face, &sizes, &n_sizes);
450 /* It seems not many fonts actually have a sane set of sizes */
451 for (i=0; i<n_sizes; i++)
452 sizes[i] = sizes[i] / PANGO_SCALE;
454 set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
456 gtk_font_chooser_ref_family (fontchooser, family);
457 gtk_font_chooser_ref_face (fontchooser, face);
460 g_object_unref ((gpointer)family);
461 g_object_unref ((gpointer)face);
462 pango_font_description_free(desc);
464 g_object_notify (G_OBJECT (fontchooser), "font-name");
468 zoom_preview_cb (GtkWidget *scrolled_window,
469 GdkEventScroll *event,
472 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
474 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
476 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
477 gtk_adjustment_set_value (adj,
478 gtk_adjustment_get_value (adj) +
479 gtk_adjustment_get_step_increment (adj));
480 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
481 gtk_adjustment_set_value (adj,
482 gtk_adjustment_get_value (adj) -
483 gtk_adjustment_get_step_increment (adj));
488 gtk_font_chooser_init (GtkFontChooser *fontchooser)
491 GtkFontChooserPrivate *priv;
492 PangoFontDescription *font_desc;
493 GtkWidget *scrolled_win;
497 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
498 GTK_TYPE_FONT_CHOOSER,
499 GtkFontChooserPrivate);
501 priv = fontchooser->priv;
503 /* Default preview string */
504 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
505 priv->show_preview_entry = TRUE;
507 /* Getting the default size */
508 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
509 priv->size = pango_font_description_get_size (font_desc);
513 gtk_widget_push_composite_child ();
515 /* Creating fundamental widgets for the private struct */
516 priv->search_entry = gtk_entry_new ();
517 priv->family_face_list = gtk_tree_view_new ();
518 priv->preview = gtk_entry_new ();
519 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
520 (gdouble) font_sizes[0],
521 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
524 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
526 /** Bootstrapping widget layout **/
527 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
529 /* Main font family/face view */
530 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
531 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
532 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
533 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
534 GTK_SHADOW_ETCHED_IN);
535 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
538 grid = gtk_grid_new ();
539 sub_grid = gtk_grid_new ();
541 gtk_widget_set_margin_bottom (priv->search_entry, 6);
542 gtk_widget_set_margin_bottom (scrolled_win, 6);
543 gtk_widget_set_margin_bottom (priv->preview, 6);
544 gtk_widget_set_margin_right (priv->size_slider, 6);
546 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
547 gtk_grid_attach (GTK_GRID (grid), scrolled_win, 0, 1, 3, 1);
548 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 3, 1);
549 gtk_grid_attach (GTK_GRID (grid), sub_grid, 0, 3, 3, 1);
551 gtk_widget_set_hexpand (GTK_WIDGET (sub_grid), TRUE);
552 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider, 0, 3, 2, 1);
553 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin, 2, 3, 1, 1);
555 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
556 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
557 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
559 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
560 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
562 gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
563 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
565 /* Setting the adjustment values for the size slider */
566 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
567 (gdouble)(priv->size / PANGO_SCALE));
568 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
569 (gdouble)(priv->size / PANGO_SCALE));
571 gtk_widget_show_all (GTK_WIDGET (fontchooser));
572 gtk_widget_hide (GTK_WIDGET (fontchooser));
574 /* Treeview column and model bootstrapping */
575 gtk_font_chooser_bootstrap_fontlist (fontchooser);
577 /* Set default preview text */
578 gtk_entry_set_text (GTK_ENTRY (priv->preview),
579 pango_language_get_sample_string (NULL));
581 /* Set search icon and place holder text */
582 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
583 gtk_entry_set_icon_from_gicon (GTK_ENTRY (priv->search_entry),
584 GTK_ENTRY_ICON_SECONDARY,
586 g_object_unref (icon);
588 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
590 /** Callback connections **/
591 /* Connect to callback for the live search text entry */
592 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
593 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
594 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
595 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
596 g_signal_connect (G_OBJECT (priv->search_entry),
597 "icon-press", G_CALLBACK (icon_press_cb), priv);
599 /* Size controls callbacks */
600 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
601 "value-changed", G_CALLBACK (slider_change_cb), priv);
602 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
603 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
604 priv->stop_notify = FALSE;
606 /* Font selection callback */
607 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
608 G_CALLBACK (cursor_changed_cb), fontchooser);
610 /* Zoom on preview scroll*/
611 g_signal_connect (G_OBJECT (priv->preview), "scroll-event",
612 G_CALLBACK (zoom_preview_cb), priv);
614 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
615 G_CALLBACK (zoom_preview_cb), priv);
617 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
619 /* Set default focus */
620 gtk_widget_pop_composite_child();
624 * gtk_font_chooser_new:
626 * Creates a new #GtkFontChooser.
628 * Return value: a new #GtkFontChooser
633 gtk_font_chooser_new (void)
635 GtkFontChooser *fontchooser;
637 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
639 return GTK_WIDGET (fontchooser);
643 cmp_families (const void *a, const void *b)
645 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
646 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
648 return g_utf8_collate (a_name, b_name);
652 populate_list (GtkFontChooser *fontchooser, GtkTreeView* treeview, GtkListStore* model)
654 GtkStyleContext *style_context;
655 PangoFontDescription *default_font;
657 GtkTreeIter match_row;
661 PangoFontFamily **families;
663 GString *tmp = g_string_new (NULL);
664 GString *family_and_face = g_string_new (NULL);
666 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
670 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
672 gtk_list_store_clear (model);
674 /* Get row header font color */
675 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
678 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
681 /* Iterate over families and faces */
682 for (i=0; i<n_families; i++)
685 PangoFontFace **faces;
688 const gchar *fam_name = pango_font_family_get_name (families[i]);
690 pango_font_family_list_faces (families[i], &faces, &n_faces);
692 for (j=0; j<n_faces; j++)
694 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
695 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
696 gchar *font_desc = pango_font_description_to_string (pango_desc);
698 /* foreground_color, family_name, face_name, desc, sample string */
699 g_string_printf (family_and_face, "%s %s",
703 g_string_printf (tmp, ROW_FORMAT_STRING,
704 family_and_face->str,
706 fontchooser->priv->preview_text);
708 gtk_list_store_append (model, &iter);
709 gtk_list_store_set (model, &iter,
710 FAMILY_COLUMN, families[i],
711 FACE_COLUMN, faces[j],
712 PREVIEW_TITLE_COLUMN, family_and_face->str,
713 PREVIEW_TEXT_COLUMN, tmp->str,
716 /* Select the first font or the default font/face from the style context */
717 if ((i == 0 && j == 0) ||
718 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
721 pango_font_description_free(pango_desc);
728 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
732 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
733 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
734 gtk_tree_path_free(path);
737 g_string_free (family_and_face, TRUE);
738 g_string_free (tmp, TRUE);
743 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
745 gboolean result = TRUE;
746 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*) data;
748 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
754 /* If there's no filter string we show the item */
755 if (strlen (search_text) == 0)
758 gtk_tree_model_get (model, iter,
759 PREVIEW_TITLE_COLUMN, &font_name,
762 if (font_name == NULL)
765 split_terms = g_strsplit (search_text, " ", 0);
766 term = split_terms[0];
768 while (term && result)
770 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
771 gchar* term_casefold = g_utf8_casefold (term, -1);
773 if (g_strrstr (font_name_casefold, term_casefold))
774 result = result && TRUE;
779 term = split_terms[n_terms];
781 g_free (term_casefold);
782 g_free (font_name_casefold);
786 g_strfreev (split_terms);
792 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser* fontchooser)
794 GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
795 GtkCellRenderer *cell;
796 GtkTreeViewColumn *col;
798 fontchooser->priv->model = gtk_list_store_new (4,
799 PANGO_TYPE_FONT_FAMILY,
800 PANGO_TYPE_FONT_FACE,
804 fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
806 g_object_unref (fontchooser->priv->model);
808 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
810 (gpointer)fontchooser->priv,
813 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
814 g_object_unref (fontchooser->priv->filter);
816 gtk_tree_view_set_rules_hint (treeview, TRUE);
817 gtk_tree_view_set_headers_visible (treeview, FALSE);
819 cell = gtk_cell_renderer_text_new ();
820 col = gtk_tree_view_column_new_with_attributes ("Family",
822 "markup", PREVIEW_TEXT_COLUMN,
825 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
827 gtk_tree_view_append_column (treeview, col);
829 populate_list (fontchooser, treeview, fontchooser->priv->model);
834 gtk_font_chooser_finalize (GObject *object)
836 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
838 gtk_font_chooser_ref_family (fontchooser, NULL);
839 gtk_font_chooser_ref_face (fontchooser, NULL);
841 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
846 gtk_font_chooser_screen_changed (GtkWidget *widget,
847 GdkScreen *previous_screen)
849 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
851 populate_list (fontchooser,
852 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
853 fontchooser->priv->model);
858 gtk_font_chooser_style_updated (GtkWidget *widget)
860 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
862 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
864 populate_list (fontchooser,
865 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
866 fontchooser->priv->model);
871 gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
872 PangoFontFamily *family)
874 GtkFontChooserPrivate *priv = fontchooser->priv;
877 family = g_object_ref (family);
879 g_object_unref (priv->family);
880 priv->family = family;
884 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
887 GtkFontChooserPrivate *priv = fontchooser->priv;
890 face = g_object_ref (face);
892 g_object_unref (priv->face);
898 * These functions are the main public interface for getting/setting the font.
902 * gtk_font_chooser_get_family:
903 * @fontchooser: a #GtkFontChooser
905 * Gets the #PangoFontFamily representing the selected font family.
907 * Return value: (transfer none): A #PangoFontFamily representing the
908 * selected font family. Font families are a collection of font
909 * faces. The returned object is owned by @fontchooser and must not
910 * be modified or freed.
915 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
917 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
919 return fontchooser->priv->family;
923 * gtk_font_chooser_get_face:
924 * @fontchooser: a #GtkFontChooser
926 * Gets the #PangoFontFace representing the selected font group
927 * details (i.e. family, slant, weight, width, etc).
929 * Return value: (transfer none): A #PangoFontFace representing the
930 * selected font group details. The returned object is owned by
931 * @fontchooser and must not be modified or freed.
936 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
938 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
940 return fontchooser->priv->face;
944 * gtk_font_chooser_get_size:
945 * @fontchooser: a #GtkFontChooser
947 * The selected font size.
949 * Return value: A n integer representing the selected font size,
950 * or -1 if no font size is selected.
955 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
957 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
959 return fontchooser->priv->size;
963 * gtk_font_chooser_get_font_name:
964 * @fontchooser: a #GtkFontChooser
966 * Gets the currently-selected font name.
968 * Note that this can be a different string than what you set with
969 * gtk_font_chooser_set_font_name(), as the font chooser widget may
970 * normalize font names and thus return a string with a different structure.
971 * For example, "Helvetica Italic Bold 12" could be normalized to
972 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
973 * if you want to compare two font descriptions.
975 * Return value: (transfer full) (allow-none): A string with the name of the
976 * current font, or %NULL if no font is selected. You must free this
977 * string with g_free().
982 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
985 PangoFontDescription *desc;
987 if (!fontchooser->priv->face)
990 desc = pango_font_face_describe (fontchooser->priv->face);
991 font_name = pango_font_description_to_string (desc);
992 pango_font_description_free (desc);
996 /* This sets the current font, then selecting the appropriate list rows. */
999 * gtk_font_chooser_set_font_name:
1000 * @fontchooser: a #GtkFontChooser
1001 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1003 * Sets the currently-selected font.
1005 * Note that the @fontchooser needs to know the screen in which it will appear
1006 * for this to work; this can be guaranteed by simply making sure that the
1007 * @fontchooser is inserted in a toplevel window before you call this function.
1009 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1010 * such font exists or if the @fontchooser doesn't belong to a particular
1016 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1017 const gchar *fontname)
1019 GtkFontChooserPrivate *priv = fontchooser->priv;
1023 PangoFontDescription *desc;
1024 gboolean found = FALSE;
1026 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1027 g_return_val_if_fail (fontname != NULL, FALSE);
1029 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1032 desc = pango_font_description_from_string (fontname);
1033 family_name = (gchar*)pango_font_description_get_family (desc);
1037 pango_font_description_free (desc);
1041 /* We make sure the filter is clear */
1042 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1044 /* We find the matching family/face */
1045 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1047 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1049 PangoFontFace *face;
1050 PangoFontDescription *tmp_desc;
1052 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1056 tmp_desc = pango_font_face_describe (face);
1057 if (pango_font_description_get_size_is_absolute (desc))
1058 pango_font_description_set_absolute_size (tmp_desc,
1059 pango_font_description_get_size (desc));
1061 pango_font_description_set_size (tmp_desc,
1062 pango_font_description_get_size (desc));
1065 if (pango_font_description_equal (desc, tmp_desc))
1068 gint size = pango_font_description_get_size (desc);
1072 if (pango_font_description_get_size_is_absolute (desc))
1073 size = size * PANGO_SCALE;
1074 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1075 size / PANGO_SCALE);
1078 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1083 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1087 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1093 gtk_tree_path_free (path);
1099 g_object_unref (face);
1100 pango_font_description_free (tmp_desc);
1106 pango_font_description_free (desc);
1107 g_object_notify (G_OBJECT (fontchooser), "font-name");
1113 * gtk_font_chooser_get_preview_text:
1114 * @fontchooser: a #GtkFontChooser
1116 * Gets the text displayed in the preview area.
1118 * Return value: (transfer none): the text displayed in the
1119 * preview area. This string is owned by the widget and
1120 * should not be modified or freed
1125 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1127 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1128 return (const gchar*)fontchooser->priv->preview_text;
1133 * gtk_font_chooser_set_preview_text:
1134 * @fontchooser: a #GtkFontChooser
1135 * @text: (transfer none): the text to display in the preview area
1137 * Sets the text displayed in the preview area.
1138 * The @text is used to show how the selected font looks.
1143 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1146 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1147 g_return_if_fail (text != NULL);
1149 g_free (fontchooser->priv->preview_text);
1150 fontchooser->priv->preview_text = g_strdup (text);
1152 populate_list (fontchooser,
1153 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1154 fontchooser->priv->model);
1156 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1158 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1162 * gtk_font_chooser_get_show_preview_entry:
1163 * @fontchooser: a #GtkFontChooser
1165 * Return value: %TRUE if the preview entry is shown or %FALSE if
1170 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1172 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1174 return fontchooser->priv->show_preview_entry;
1178 * gtk_font_chooser_set_show_preview_entry:
1179 * @fontchooser: a #GtkFontChooser
1180 * @show_preview_entry: whether to show the editable preview entry or not
1182 * Shows or hides the editable preview entry.
1186 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1187 gboolean show_preview_entry)
1189 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1191 if (show_preview_entry)
1192 gtk_widget_show (fontchooser->priv->preview_scrolled_window);
1194 gtk_widget_hide (fontchooser->priv->preview_scrolled_window);
1196 fontchooser->priv->show_preview_entry = show_preview_entry;
1197 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");