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;
86 GtkWidget *preview_scrolled_window;
88 gboolean show_preview_entry;
91 GtkWidget *size_slider;
95 PangoFontFamily *family;
98 #define DEFAULT_FONT_NAME "Sans 10"
99 #define MAX_FONT_SIZE 999
101 /* This is the initial fixed height and the top padding of the preview entry */
102 #define PREVIEW_HEIGHT 72
103 #define PREVIEW_TOP_PADDING 6
105 /* These are the sizes of the font, style & size lists. */
106 #define FONT_LIST_HEIGHT 136
107 #define FONT_LIST_WIDTH 190
108 #define FONT_STYLE_LIST_WIDTH 170
109 #define FONT_SIZE_LIST_WIDTH 60
111 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
113 #define NO_FONT_MATCHED_SEARCH "No fonts matched your serach entry.You can revise your serach and try again."
115 /* These are what we use as the standard font sizes, for the size list.
117 static const gint font_sizes[] = {
118 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
125 PROP_SHOW_PREVIEW_ENTRY
136 static void gtk_font_chooser_set_property (GObject *object,
140 static void gtk_font_chooser_get_property (GObject *object,
144 static void gtk_font_chooser_finalize (GObject *object);
146 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
147 GdkScreen *previous_screen);
148 static void gtk_font_chooser_style_updated (GtkWidget *widget);
150 static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
151 PangoFontFamily *family);
152 static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
153 PangoFontFace *face);
155 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
157 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
160 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
163 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
165 widget_class->screen_changed = gtk_font_chooser_screen_changed;
166 widget_class->style_updated = gtk_font_chooser_style_updated;
168 gobject_class->finalize = gtk_font_chooser_finalize;
169 gobject_class->set_property = gtk_font_chooser_set_property;
170 gobject_class->get_property = gtk_font_chooser_get_property;
172 g_object_class_install_property (gobject_class,
174 g_param_spec_string ("font-name",
176 P_("The string that represents this font"),
178 GTK_PARAM_READWRITE));
179 g_object_class_install_property (gobject_class,
181 g_param_spec_string ("preview-text",
183 P_("The text to display in order to demonstrate the selected font"),
184 pango_language_get_sample_string (NULL),
185 GTK_PARAM_READWRITE));
187 g_object_class_install_property (gobject_class,
188 PROP_SHOW_PREVIEW_ENTRY,
189 g_param_spec_boolean ("show-preview-entry",
190 P_("Show preview text entry"),
191 P_("Whether the preview text entry is shown or not"),
193 GTK_PARAM_READWRITE));
195 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
199 gtk_font_chooser_set_property (GObject *object,
204 GtkFontChooser *fontchooser;
206 fontchooser = GTK_FONT_CHOOSER (object);
211 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
213 case PROP_PREVIEW_TEXT:
214 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
216 case PROP_SHOW_PREVIEW_ENTRY:
217 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gtk_font_chooser_get_property (GObject *object,
231 GtkFontChooser *fontchooser;
233 fontchooser = GTK_FONT_CHOOSER (object);
238 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
240 case PROP_PREVIEW_TEXT:
241 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
243 case PROP_SHOW_PREVIEW_ENTRY:
244 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 deleted_text_cb (GtkEntryBuffer *buffer,
257 GtkFontChooser *fc = (GtkFontChooser*)user_data;
258 GtkFontChooserPrivate *priv = fc->priv;
259 GtkWidget *entry = priv->search_entry;
261 if (gtk_entry_buffer_get_length (buffer) == 0)
263 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
264 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
265 GTK_ENTRY_ICON_SECONDARY,
267 g_object_unref (icon);
270 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
274 inserted_text_cb (GtkEntryBuffer *buffer,
280 GtkFontChooser *fc = (GtkFontChooser*)user_data;
281 GtkFontChooserPrivate *priv = fc->priv;
282 GtkWidget *entry = priv->search_entry;
284 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
285 "edit-clear-symbolic") != 0 ||
286 g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
287 GTK_STOCK_CLEAR) != 0)
289 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
290 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
291 GTK_ENTRY_ICON_SECONDARY,
293 g_object_unref (icon);
296 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
300 icon_press_cb (GtkEntry *entry,
301 GtkEntryIconPosition pos,
305 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
309 slider_change_cb (GtkAdjustment *adjustment,
312 GtkFontChooser *fc = (GtkFontChooser*)user_data;
313 GtkFontChooserPrivate *priv = fc->priv;
314 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
315 gdouble slider_value = gtk_adjustment_get_value (adjustment);
316 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
318 if (slider_value != spin_value)
319 gtk_adjustment_set_value (spin_adj,
320 gtk_adjustment_get_value (adjustment));
324 spin_change_cb (GtkAdjustment *adjustment,
327 PangoFontDescription *desc;
328 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
329 GtkFontChooserPrivate *priv = fontchooser->priv;
330 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
332 gdouble size = gtk_adjustment_get_value (adjustment);
333 priv->size = ((gint)size) * PANGO_SCALE;
335 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
336 pango_font_description_set_size (desc, priv->size);
337 gtk_widget_override_font (priv->preview, desc);
339 g_object_notify (G_OBJECT (fontchooser), "font-name");
341 /* If the new value is lower than the lower bound of the slider, we set
342 * the slider adjustment to the lower bound value if it is not already set
344 if (size < gtk_adjustment_get_lower (slider_adj) &&
345 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
346 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
348 /* If the new value is upper than the upper bound of the slider, we set
349 * the slider adjustment to the upper bound value if it is not already set
351 else if (size > gtk_adjustment_get_upper (slider_adj) &&
352 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
353 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
355 /* If the new value is not already set on the slider we set it */
356 else if (size != gtk_adjustment_get_value (slider_adj))
357 gtk_adjustment_set_value (slider_adj, size);
359 gtk_widget_queue_draw (priv->preview);
363 set_range_marks (GtkFontChooserPrivate *priv,
364 GtkWidget *size_slider,
374 sizes = (gint*)font_sizes;
375 length = G_N_ELEMENTS (font_sizes);
378 gtk_scale_clear_marks (GTK_SCALE (size_slider));
380 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
382 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
383 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
385 value = gtk_adjustment_get_value (adj);
386 if (value > (gdouble) sizes[length-1])
387 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
388 else if (value < (gdouble) sizes[0])
389 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
391 for (i = 0; i < length; i++)
392 gtk_scale_add_mark (GTK_SCALE (size_slider),
394 GTK_POS_BOTTOM, NULL);
398 cursor_changed_cb (GtkTreeView *treeview,
401 PangoFontFamily *family;
403 PangoFontDescription *desc;
409 GtkTreePath *path = gtk_tree_path_new ();
411 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
413 gtk_tree_view_get_cursor (treeview, &path, NULL);
418 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
420 gtk_tree_path_free (path);
425 gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
427 FAMILY_COLUMN, &family,
430 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
432 gtk_tree_path_free (path);
435 if (!face || !family)
437 g_object_unref (face);
438 g_object_unref (family);
442 desc = pango_font_face_describe (face);
443 pango_font_description_set_size (desc, fontchooser->priv->size);
444 gtk_widget_override_font (fontchooser->priv->preview, desc);
446 pango_font_face_list_sizes (face, &sizes, &n_sizes);
447 /* It seems not many fonts actually have a sane set of sizes */
448 for (i = 0; i < n_sizes; i++)
449 sizes[i] = sizes[i] / PANGO_SCALE;
451 set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
453 gtk_font_chooser_ref_family (fontchooser, family);
454 gtk_font_chooser_ref_face (fontchooser, face);
457 g_object_unref ((gpointer)family);
458 g_object_unref ((gpointer)face);
459 pango_font_description_free(desc);
461 g_object_notify (G_OBJECT (fontchooser), "font-name");
465 zoom_preview_cb (GtkWidget *scrolled_window,
466 GdkEventScroll *event,
469 GtkFontChooser *fc = (GtkFontChooser*)user_data;
470 GtkFontChooserPrivate *priv = fc->priv;
472 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
474 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
475 gtk_adjustment_set_value (adj,
476 gtk_adjustment_get_value (adj) +
477 gtk_adjustment_get_step_increment (adj));
478 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
479 gtk_adjustment_set_value (adj,
480 gtk_adjustment_get_value (adj) -
481 gtk_adjustment_get_step_increment (adj));
487 row_inserted_cb (GtkTreeModel *model,
492 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
493 GtkFontChooserPrivate *priv = fontchooser->priv;
495 if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
498 g_object_ref (priv->empty_list);
499 gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
501 gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
502 priv->family_face_list);
507 row_deleted_cb (GtkTreeModel *model,
511 GtkFontChooser *fontchooser = (GtkFontChooser*)user_data;
512 GtkFontChooserPrivate *priv = fontchooser->priv;
514 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
516 if (gtk_bin_get_child (GTK_BIN (priv->list_scrolled_window)) ==
517 priv->family_face_list)
519 g_object_ref (priv->family_face_list);
520 gtk_container_remove (GTK_CONTAINER (priv->list_scrolled_window),
521 priv->family_face_list);
522 gtk_container_add (GTK_CONTAINER (priv->list_scrolled_window),
531 gtk_font_chooser_init (GtkFontChooser *fontchooser)
534 GtkFontChooserPrivate *priv;
535 PangoFontDescription *font_desc;
536 GtkWidget *scrolled_win;
540 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
541 GTK_TYPE_FONT_CHOOSER,
542 GtkFontChooserPrivate);
544 priv = fontchooser->priv;
546 /* Default preview string */
547 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
548 priv->show_preview_entry = TRUE;
550 /* Getting the default size */
551 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
552 priv->size = pango_font_description_get_size (font_desc);
556 gtk_widget_push_composite_child ();
558 /* Creating fundamental widgets for the private struct */
559 priv->search_entry = gtk_entry_new ();
560 priv->family_face_list = gtk_tree_view_new ();
561 priv->preview = gtk_entry_new ();
562 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
563 (gdouble) font_sizes[0],
564 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
567 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
569 /** Bootstrapping widget layout **/
570 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
572 /* Main font family/face view */
573 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
574 scrolled_win = priv->list_scrolled_window;
575 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
576 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
577 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
578 GTK_SHADOW_ETCHED_IN);
579 gtk_container_add (GTK_CONTAINER (scrolled_win),
580 priv->family_face_list);
582 /* Text to display when list is empty */
583 priv->empty_list = gtk_text_view_new ();
584 gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->empty_list)),
585 _(NO_FONT_MATCHED_SEARCH),
589 grid = gtk_grid_new ();
590 sub_grid = gtk_grid_new ();
592 gtk_widget_set_margin_bottom (priv->search_entry, 6);
593 gtk_widget_set_margin_bottom (scrolled_win, 6);
594 gtk_widget_set_margin_bottom (priv->preview, 6);
595 gtk_widget_set_margin_right (priv->size_slider, 6);
597 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
598 gtk_grid_attach (GTK_GRID (grid), scrolled_win, 0, 1, 3, 1);
599 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 3, 1);
600 gtk_grid_attach (GTK_GRID (grid), sub_grid, 0, 4, 3, 1);
602 gtk_widget_set_hexpand (GTK_WIDGET (sub_grid), TRUE);
603 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider, 0, 3, 2, 1);
604 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin, 2, 3, 1, 1);
606 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
607 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
608 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
610 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
611 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
613 gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
614 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
616 /* Setting the adjustment values for the size slider */
617 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
618 (gdouble)(priv->size / PANGO_SCALE));
619 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
620 (gdouble)(priv->size / PANGO_SCALE));
622 gtk_widget_show_all (GTK_WIDGET (fontchooser));
623 gtk_widget_hide (GTK_WIDGET (fontchooser));
625 /* Treeview column and model bootstrapping */
626 gtk_font_chooser_bootstrap_fontlist (fontchooser);
628 /* Set default preview text */
629 gtk_entry_set_text (GTK_ENTRY (priv->preview),
630 pango_language_get_sample_string (NULL));
632 /* Set search icon and place holder text */
633 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
634 gtk_entry_set_icon_from_gicon (GTK_ENTRY (priv->search_entry),
635 GTK_ENTRY_ICON_SECONDARY,
637 g_object_unref (icon);
639 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
641 /** Callback connections **/
642 /* Connect to callback for the live search text entry */
643 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
644 "deleted-text", G_CALLBACK (deleted_text_cb), fontchooser);
645 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
646 "inserted-text", G_CALLBACK (inserted_text_cb), fontchooser);
647 g_signal_connect (G_OBJECT (priv->search_entry),
648 "icon-press", G_CALLBACK (icon_press_cb), NULL);
650 /* Size controls callbacks */
651 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
652 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
653 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
654 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
656 /* Font selection callback */
657 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
658 G_CALLBACK (cursor_changed_cb), fontchooser);
660 /* Zoom on preview scroll*/
661 g_signal_connect (G_OBJECT (priv->preview), "scroll-event",
662 G_CALLBACK (zoom_preview_cb), fontchooser);
664 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
665 G_CALLBACK (zoom_preview_cb), fontchooser);
667 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
669 /* Font list empty hides the scrolledwindow */
671 g_signal_connect (G_OBJECT (priv->filter), "row-deleted",
672 G_CALLBACK (row_deleted_cb), fontchooser);
673 g_signal_connect (G_OBJECT (priv->filter), "row-inserted",
674 G_CALLBACK (row_inserted_cb), fontchooser);
677 /* Set default focus */
678 gtk_widget_pop_composite_child();
682 * gtk_font_chooser_new:
684 * Creates a new #GtkFontChooser.
686 * Return value: a new #GtkFontChooser
691 gtk_font_chooser_new (void)
693 GtkFontChooser *fontchooser;
695 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
697 return GTK_WIDGET (fontchooser);
701 cmp_families (const void *a,
704 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
705 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
707 return g_utf8_collate (a_name, b_name);
711 populate_list (GtkFontChooser *fontchooser,
712 GtkTreeView *treeview,
715 GtkStyleContext *style_context;
716 PangoFontDescription *default_font;
718 GtkTreeIter match_row;
722 PangoFontFamily **families;
724 GString *tmp = g_string_new (NULL);
725 GString *family_and_face = g_string_new (NULL);
727 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
731 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
733 gtk_list_store_clear (model);
735 /* Get row header font color */
736 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
739 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
742 /* Iterate over families and faces */
743 for (i = 0; i < n_families; i++)
746 PangoFontFace **faces;
749 const gchar *fam_name = pango_font_family_get_name (families[i]);
751 pango_font_family_list_faces (families[i], &faces, &n_faces);
753 for (j = 0; j < n_faces; j++)
755 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
756 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
757 gchar *font_desc = pango_font_description_to_string (pango_desc);
759 /* foreground_color, family_name, face_name, desc, sample string */
760 g_string_printf (family_and_face, "%s %s",
764 g_string_printf (tmp, ROW_FORMAT_STRING,
765 family_and_face->str,
767 fontchooser->priv->preview_text);
769 gtk_list_store_append (model, &iter);
770 gtk_list_store_set (model, &iter,
771 FAMILY_COLUMN, families[i],
772 FACE_COLUMN, faces[j],
773 PREVIEW_TITLE_COLUMN, family_and_face->str,
774 PREVIEW_TEXT_COLUMN, tmp->str,
777 /* Select the first font or the default font/face from the style context */
778 if ((i == 0 && j == 0) ||
779 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
782 pango_font_description_free(pango_desc);
789 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
793 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
794 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
795 gtk_tree_path_free(path);
798 g_string_free (family_and_face, TRUE);
799 g_string_free (tmp, TRUE);
804 visible_func (GtkTreeModel *model,
808 gboolean result = TRUE;
809 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
811 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
817 /* If there's no filter string we show the item */
818 if (strlen (search_text) == 0)
821 gtk_tree_model_get (model, iter,
822 PREVIEW_TITLE_COLUMN, &font_name,
825 if (font_name == NULL)
828 split_terms = g_strsplit (search_text, " ", 0);
829 term = split_terms[0];
831 while (term && result)
833 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
834 gchar* term_casefold = g_utf8_casefold (term, -1);
836 if (g_strrstr (font_name_casefold, term_casefold))
837 result = result && TRUE;
842 term = split_terms[n_terms];
844 g_free (term_casefold);
845 g_free (font_name_casefold);
849 g_strfreev (split_terms);
855 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser* fontchooser)
857 GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
858 GtkCellRenderer *cell;
859 GtkTreeViewColumn *col;
861 fontchooser->priv->model = gtk_list_store_new (4,
862 PANGO_TYPE_FONT_FAMILY,
863 PANGO_TYPE_FONT_FACE,
867 fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
869 g_object_unref (fontchooser->priv->model);
871 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
873 (gpointer)fontchooser->priv,
876 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
877 g_object_unref (fontchooser->priv->filter);
879 gtk_tree_view_set_rules_hint (treeview, TRUE);
880 gtk_tree_view_set_headers_visible (treeview, FALSE);
882 cell = gtk_cell_renderer_text_new ();
883 col = gtk_tree_view_column_new_with_attributes ("Family",
885 "markup", PREVIEW_TEXT_COLUMN,
888 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
890 gtk_tree_view_append_column (treeview, col);
892 populate_list (fontchooser, treeview, fontchooser->priv->model);
897 gtk_font_chooser_finalize (GObject *object)
899 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
901 gtk_font_chooser_ref_family (fontchooser, NULL);
902 gtk_font_chooser_ref_face (fontchooser, NULL);
904 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
909 gtk_font_chooser_screen_changed (GtkWidget *widget,
910 GdkScreen *previous_screen)
912 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
914 populate_list (fontchooser,
915 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
916 fontchooser->priv->model);
921 gtk_font_chooser_style_updated (GtkWidget *widget)
923 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
925 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
927 populate_list (fontchooser,
928 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
929 fontchooser->priv->model);
934 gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
935 PangoFontFamily *family)
937 GtkFontChooserPrivate *priv = fontchooser->priv;
940 family = g_object_ref (family);
942 g_object_unref (priv->family);
943 priv->family = family;
947 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
950 GtkFontChooserPrivate *priv = fontchooser->priv;
953 face = g_object_ref (face);
955 g_object_unref (priv->face);
961 * These functions are the main public interface for getting/setting the font.
965 * gtk_font_chooser_get_family:
966 * @fontchooser: a #GtkFontChooser
968 * Gets the #PangoFontFamily representing the selected font family.
970 * Return value: (transfer none): A #PangoFontFamily representing the
971 * selected font family. Font families are a collection of font
972 * faces. The returned object is owned by @fontchooser and must not
973 * be modified or freed.
978 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
980 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
982 return fontchooser->priv->family;
986 * gtk_font_chooser_get_face:
987 * @fontchooser: a #GtkFontChooser
989 * Gets the #PangoFontFace representing the selected font group
990 * details (i.e. family, slant, weight, width, etc).
992 * Return value: (transfer none): A #PangoFontFace representing the
993 * selected font group details. The returned object is owned by
994 * @fontchooser and must not be modified or freed.
999 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
1001 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1003 return fontchooser->priv->face;
1007 * gtk_font_chooser_get_size:
1008 * @fontchooser: a #GtkFontChooser
1010 * The selected font size.
1012 * Return value: A n integer representing the selected font size,
1013 * or -1 if no font size is selected.
1018 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
1020 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
1022 return fontchooser->priv->size;
1026 * gtk_font_chooser_get_font_name:
1027 * @fontchooser: a #GtkFontChooser
1029 * Gets the currently-selected font name.
1031 * Note that this can be a different string than what you set with
1032 * gtk_font_chooser_set_font_name(), as the font chooser widget may
1033 * normalize font names and thus return a string with a different structure.
1034 * For example, "Helvetica Italic Bold 12" could be normalized to
1035 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1036 * if you want to compare two font descriptions.
1038 * Return value: (transfer full) (allow-none): A string with the name of the
1039 * current font, or %NULL if no font is selected. You must free this
1040 * string with g_free().
1045 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1048 gchar *font_desc_name;
1049 PangoFontDescription *desc;
1051 if (!fontchooser->priv->face)
1056 desc = pango_font_face_describe (fontchooser->priv->face);
1057 font_desc_name = pango_font_description_to_string (desc);
1058 pango_font_description_free (desc);
1060 font_name = g_strdup_printf ("%s %d", font_desc_name, fontchooser->priv->size / PANGO_SCALE);
1061 g_free (font_desc_name);
1065 /* This sets the current font, then selecting the appropriate list rows. */
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 it will appear
1075 * for this to work; this can be guaranteed by simply making sure that the
1076 * @fontchooser is inserted in a toplevel window before you call this function.
1078 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1079 * such font exists or if the @fontchooser doesn't belong to a particular
1085 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1086 const gchar *fontname)
1088 GtkFontChooserPrivate *priv = fontchooser->priv;
1092 PangoFontDescription *desc;
1093 gboolean found = FALSE;
1095 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1096 g_return_val_if_fail (fontname != NULL, FALSE);
1098 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1101 desc = pango_font_description_from_string (fontname);
1102 family_name = (gchar*)pango_font_description_get_family (desc);
1106 pango_font_description_free (desc);
1110 /* We make sure the filter is clear */
1111 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1113 /* We find the matching family/face */
1114 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1116 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1118 PangoFontFace *face;
1119 PangoFontDescription *tmp_desc;
1121 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1125 tmp_desc = pango_font_face_describe (face);
1126 if (pango_font_description_get_size_is_absolute (desc))
1127 pango_font_description_set_absolute_size (tmp_desc,
1128 pango_font_description_get_size (desc));
1130 pango_font_description_set_size (tmp_desc,
1131 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);
1197 return (const gchar*)fontchooser->priv->preview_text;
1202 * gtk_font_chooser_set_preview_text:
1203 * @fontchooser: a #GtkFontChooser
1204 * @text: (transfer none): the text to display in the preview area
1206 * Sets the text displayed in the preview area.
1207 * The @text is used to show how the selected font looks.
1212 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1215 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1216 g_return_if_fail (text != NULL);
1218 g_free (fontchooser->priv->preview_text);
1219 fontchooser->priv->preview_text = g_strdup (text);
1221 populate_list (fontchooser,
1222 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1223 fontchooser->priv->model);
1225 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1227 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1231 * gtk_font_chooser_get_show_preview_entry:
1232 * @fontchooser: a #GtkFontChooser
1234 * Return value: %TRUE if the preview entry is shown or %FALSE if
1239 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1241 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1243 return fontchooser->priv->show_preview_entry;
1247 * gtk_font_chooser_set_show_preview_entry:
1248 * @fontchooser: a #GtkFontChooser
1249 * @show_preview_entry: whether to show the editable preview entry or not
1251 * Shows or hides the editable preview entry.
1255 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1256 gboolean show_preview_entry)
1258 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1260 if (show_preview_entry)
1261 gtk_widget_show (fontchooser->priv->preview_scrolled_window);
1263 gtk_widget_hide (fontchooser->priv->preview_scrolled_window);
1265 fontchooser->priv->show_preview_entry = show_preview_entry;
1266 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");