1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * Massively updated to rework the user interface by Alberto Ruiz, 2011
6 * Massively updated for Pango by Owen Taylor, May 2000
7 * GtkFontChooser widget for Gtk+, by Damon Chaplin, May 1998.
8 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
9 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
30 #include <glib/gprintf.h>
35 #include "gtkfontchooser.h"
36 #include "gtkcellrenderertext.h"
42 #include "gtkliststore.h"
44 #include "gtktreeselection.h"
45 #include "gtktreeview.h"
47 #include "gtkscrolledwindow.h"
49 #include "gtkaccessible.h"
50 #include "gtkbuildable.h"
51 #include "gtkprivate.h"
52 #include "gtkalignment.h"
55 #include "gtkspinbutton.h"
56 #include "gtkwidget.h"
60 * SECTION:gtkfontchooser
61 * @Short_description: A widget for selecting fonts
62 * @Title: GtkFontChooser
63 * @See_also: #GtkFontChooserDialog
65 * The #GtkFontChooser widget lists the available fonts, styles and sizes,
66 * allowing the user to select a font.
67 * It is used in the #GtkFontChooserDialog widget to provide a dialog box for
70 * To set the font which is initially selected, use
71 * gtk_font_chooser_set_font_name().
73 * To get the selected font use gtk_font_chooser_get_font_name().
75 * To change the text which is shown in the preview area, use
76 * gtk_font_chooser_set_preview_text().
82 struct _GtkFontChooserPrivate
84 GtkWidget *search_entry;
85 GtkWidget *family_face_list;
90 GtkWidget *preview_scrolled_window;
92 gboolean show_preview_entry;
95 GtkWidget *size_slider;
96 gboolean ignore_slider;
100 PangoFontFamily *family;
103 #define DEFAULT_FONT_NAME "Sans 10"
104 #define MAX_FONT_SIZE 999
106 /* This is the initial fixed height and the top padding of the preview entry */
107 #define PREVIEW_HEIGHT 72
108 #define PREVIEW_TOP_PADDING 6
110 /* Widget default geometry */
111 #define FONT_CHOOSER_WIDTH 540
112 #define FONT_CHOOSER_HEIGHT 408
114 /* These are the sizes of the font, style & size lists. */
115 #define FONT_LIST_HEIGHT 136
116 #define FONT_LIST_WIDTH 190
117 #define FONT_STYLE_LIST_WIDTH 170
118 #define FONT_SIZE_LIST_WIDTH 60
120 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\" foreground=\"%s\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
122 /* These are what we use as the standard font sizes, for the size list.
124 #define FONT_SIZES_LENGTH 14
125 static const gint font_sizes[] = {
126 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
133 PROP_SHOW_PREVIEW_ENTRY
144 static void gtk_font_chooser_set_property (GObject *object,
148 static void gtk_font_chooser_get_property (GObject *object,
152 static void gtk_font_chooser_finalize (GObject *object);
154 static void gtk_font_chooser_screen_changed (GtkWidget *widget,
155 GdkScreen *previous_screen);
156 static void gtk_font_chooser_style_updated (GtkWidget *widget);
158 static void gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
159 PangoFontFamily *family);
160 static void gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
161 PangoFontFace *face);
163 static void gtk_font_chooser_bootstrap_fontlist (GtkFontChooser *fontchooser);
165 G_DEFINE_TYPE (GtkFontChooser, gtk_font_chooser, GTK_TYPE_BOX)
168 gtk_font_chooser_class_init (GtkFontChooserClass *klass)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
171 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
173 widget_class->screen_changed = gtk_font_chooser_screen_changed;
174 widget_class->style_updated = gtk_font_chooser_style_updated;
176 gobject_class->finalize = gtk_font_chooser_finalize;
177 gobject_class->set_property = gtk_font_chooser_set_property;
178 gobject_class->get_property = gtk_font_chooser_get_property;
180 g_object_class_install_property (gobject_class,
182 g_param_spec_string ("font-name",
184 P_("The string that represents this font"),
186 GTK_PARAM_READWRITE));
187 g_object_class_install_property (gobject_class,
189 g_param_spec_string ("preview-text",
191 P_("The text to display in order to demonstrate the selected font"),
192 pango_language_get_sample_string (NULL),
193 GTK_PARAM_READWRITE));
195 g_object_class_install_property (gobject_class,
196 PROP_SHOW_PREVIEW_ENTRY,
197 g_param_spec_boolean ("show-preview-entry",
198 P_("Show preview text entry"),
199 P_("Whether the preview text entry is shown or not"),
201 GTK_PARAM_READWRITE));
203 g_type_class_add_private (klass, sizeof (GtkFontChooserPrivate));
207 gtk_font_chooser_set_property (GObject *object,
212 GtkFontChooser *fontchooser;
214 fontchooser = GTK_FONT_CHOOSER (object);
219 gtk_font_chooser_set_font_name (fontchooser, g_value_get_string (value));
221 case PROP_PREVIEW_TEXT:
222 gtk_font_chooser_set_preview_text (fontchooser, g_value_get_string (value));
224 case PROP_SHOW_PREVIEW_ENTRY:
225 gtk_font_chooser_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gtk_font_chooser_get_property (GObject *object,
238 GtkFontChooser *fontchooser;
240 fontchooser = GTK_FONT_CHOOSER (object);
245 g_value_take_string (value, gtk_font_chooser_get_font_name (fontchooser));
247 case PROP_PREVIEW_TEXT:
248 g_value_set_string (value, gtk_font_chooser_get_preview_text (fontchooser));
250 case PROP_SHOW_PREVIEW_ENTRY:
251 g_value_set_boolean (value, gtk_font_chooser_get_show_preview_entry (fontchooser));
253 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 refilter_and_focus (GtkFontChooserPrivate *priv)
262 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
263 GtkTreePath *path = gtk_tree_path_new ();
265 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
270 gtk_tree_view_get_cursor (treeview, &path, NULL);
272 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter), &iter, path))
274 gtk_tree_path_free (path);
278 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
279 gtk_tree_path_free (path);
283 deleted_text_cb (GtkEntryBuffer *buffer,
288 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
289 GtkWidget *entry = priv->search_entry;
291 if (gtk_entry_buffer_get_length (buffer) == 0)
293 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
294 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
295 GTK_ENTRY_ICON_SECONDARY,
297 g_object_unref (icon);
300 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
304 inserted_text_cb (GtkEntryBuffer *buffer,
310 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)user_data;
311 GtkWidget *entry = priv->search_entry;
313 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
314 "edit-clear-symbolic") ||
315 g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
318 GIcon *icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
319 gtk_entry_set_icon_from_gicon (GTK_ENTRY (entry),
320 GTK_ENTRY_ICON_SECONDARY,
322 g_object_unref (icon);
325 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
329 icon_press_cb (GtkEntry *entry,
330 GtkEntryIconPosition pos,
334 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
338 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
340 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
342 priv->ignore_slider = TRUE;
344 gtk_adjustment_set_value (gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin)),
345 gtk_adjustment_get_value (adjustment));
349 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
351 PangoFontDescription *desc;
352 GtkFontChooser *fontchooser = (GtkFontChooser*)data;
353 GtkFontChooserPrivate *priv = fontchooser->priv;
355 gdouble size = gtk_adjustment_get_value (adjustment);
356 priv->size = ((gint)size) * PANGO_SCALE;
358 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
359 pango_font_description_set_size (desc, priv->size);
360 gtk_widget_override_font (priv->preview, desc);
362 g_object_notify (G_OBJECT (fontchooser), "font-name");
364 if (priv->ignore_slider)
366 priv->ignore_slider = FALSE;
370 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
372 if (size < gtk_adjustment_get_lower (slider_adj))
373 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
374 else if (size > gtk_adjustment_get_upper (slider_adj))
375 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
377 gtk_adjustment_set_value (slider_adj, size);
379 gtk_widget_queue_draw (priv->preview);
383 set_range_marks (GtkFontChooserPrivate *priv,
384 GtkWidget* size_slider,
394 sizes = (gint*)font_sizes;
395 length = FONT_SIZES_LENGTH;
398 gtk_scale_clear_marks (GTK_SCALE (size_slider));
400 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
402 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
403 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
405 value = gtk_adjustment_get_value (adj);
406 if (value > (gdouble) sizes[length-1])
408 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
409 priv->ignore_slider = TRUE;
411 else if (value < (gdouble) sizes[0])
413 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
414 priv->ignore_slider = TRUE;
417 for (i=0; i<length; i++)
418 gtk_scale_add_mark (GTK_SCALE (size_slider),
420 GTK_POS_BOTTOM, NULL);
424 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
426 PangoFontFamily *family;
428 PangoFontDescription *desc;
434 GtkTreePath *path = gtk_tree_path_new ();
436 GtkFontChooser *fontchooser = (GtkFontChooser*)data;
438 gtk_tree_view_get_cursor (treeview, &path, NULL);
443 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontchooser->priv->filter), &iter, path))
445 gtk_tree_path_free (path);
450 gtk_tree_model_get (GTK_TREE_MODEL (fontchooser->priv->filter), &iter,
452 FAMILY_COLUMN, &family,
455 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
457 gtk_tree_path_free (path);
460 if (!face || !family)
462 g_object_unref (face);
463 g_object_unref (family);
467 desc = pango_font_face_describe (face);
468 pango_font_description_set_size (desc, fontchooser->priv->size);
469 gtk_widget_override_font (fontchooser->priv->preview, desc);
471 pango_font_face_list_sizes (face, &sizes, &n_sizes);
472 /* It seems not many fonts actually have a sane set of sizes */
473 for (i=0; i<n_sizes; i++)
474 sizes[i] = sizes[i] / PANGO_SCALE;
476 set_range_marks (fontchooser->priv, fontchooser->priv->size_slider, sizes, n_sizes);
478 gtk_font_chooser_ref_family (fontchooser, family);
479 gtk_font_chooser_ref_face (fontchooser, face);
482 g_object_unref ((gpointer)family);
483 g_object_unref ((gpointer)face);
484 pango_font_description_free(desc);
486 g_object_notify (G_OBJECT (fontchooser), "font-name");
490 zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
492 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*)data;
494 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
496 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
497 gtk_adjustment_set_value (adj,
498 gtk_adjustment_get_value (adj) +
499 gtk_adjustment_get_step_increment (adj));
500 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
501 gtk_adjustment_set_value (adj,
502 gtk_adjustment_get_value (adj) -
503 gtk_adjustment_get_step_increment (adj));
508 gtk_font_chooser_init (GtkFontChooser *fontchooser)
511 GtkFontChooserPrivate *priv;
512 PangoFontDescription *font_desc;
513 GtkWidget *scrolled_win;
517 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
518 GTK_TYPE_FONT_CHOOSER,
519 GtkFontChooserPrivate);
521 priv = fontchooser->priv;
523 /* Default preview string */
524 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
525 priv->show_preview_entry = TRUE;
527 /* Getting the default size */
528 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
529 priv->size = pango_font_description_get_size (font_desc);
533 gtk_widget_push_composite_child ();
535 /* Creating fundamental widgets for the private struct */
536 priv->search_entry = gtk_entry_new ();
537 priv->family_face_list = gtk_tree_view_new ();
538 priv->preview = gtk_entry_new ();
539 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
540 (gdouble) font_sizes[0],
541 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
544 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
546 /** Bootstrapping widget layout **/
547 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
549 /* Main font family/face view */
550 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
551 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
552 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
553 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
554 GTK_SHADOW_ETCHED_IN);
555 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
558 grid = gtk_grid_new ();
559 sub_grid = gtk_grid_new ();
561 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 3, 1);
562 gtk_grid_attach (GTK_GRID (grid), scrolled_win, 0, 1, 3, 1);
563 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 3, 1);
564 gtk_grid_attach (GTK_GRID (grid), sub_grid, 0, 3, 3, 1);
566 gtk_widget_set_hexpand (GTK_WIDGET (sub_grid), TRUE);
567 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_slider, 0, 3, 2, 1);
568 gtk_grid_attach (GTK_GRID (sub_grid), priv->size_spin, 2, 3, 1, 1);
570 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
571 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
572 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
574 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
575 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
577 gtk_grid_set_column_homogeneous (GTK_GRID (sub_grid), FALSE);
578 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
580 /* Setting the adjustment values for the size slider */
581 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
582 (gdouble)(priv->size / PANGO_SCALE));
583 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
584 (gdouble)(priv->size / PANGO_SCALE));
586 gtk_widget_show_all (GTK_WIDGET (fontchooser));
587 gtk_widget_hide (GTK_WIDGET (fontchooser));
589 /* Treeview column and model bootstrapping */
590 gtk_font_chooser_bootstrap_fontlist (fontchooser);
592 /* Set default preview text */
593 gtk_entry_set_text (GTK_ENTRY (priv->preview),
594 pango_language_get_sample_string (NULL));
596 /* Set search icon and place holder text */
597 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
598 gtk_entry_set_icon_from_gicon (GTK_ENTRY (priv->search_entry),
599 GTK_ENTRY_ICON_SECONDARY,
601 g_object_unref (icon);
603 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
605 /** Callback connections **/
606 /* Connect to callback for the live search text entry */
607 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
608 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
609 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
610 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
611 g_signal_connect (G_OBJECT (priv->search_entry),
612 "icon-press", G_CALLBACK (icon_press_cb), priv);
614 /* Size controls callbacks */
615 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
616 "value-changed", G_CALLBACK (slider_change_cb), priv);
617 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
618 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
619 priv->ignore_slider = FALSE;
621 /* Font selection callback */
622 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
623 G_CALLBACK (cursor_changed_cb), fontchooser);
625 /* Zoom on preview scroll*/
626 g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
627 G_CALLBACK (zoom_preview_cb), priv);
629 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
630 G_CALLBACK (zoom_preview_cb), priv);
632 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
634 /* Set default focus */
635 gtk_widget_pop_composite_child();
639 * gtk_font_chooser_new:
641 * Creates a new #GtkFontChooser.
643 * Return value: a new #GtkFontChooser
648 gtk_font_chooser_new (void)
650 GtkFontChooser *fontchooser;
652 fontchooser = g_object_new (GTK_TYPE_FONT_CHOOSER, NULL);
654 return GTK_WIDGET (fontchooser);
658 cmp_families (const void *a, const void *b)
660 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
661 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
663 return g_utf8_collate (a_name, b_name);
667 populate_list (GtkFontChooser *fontchooser, GtkTreeView* treeview, GtkListStore* model)
669 GtkStyleContext *style_context;
673 PangoFontDescription *default_font;
675 GtkTreeIter match_row;
679 PangoFontFamily **families;
681 GString *tmp = g_string_new (NULL);
682 GString *family_and_face = g_string_new (NULL);
684 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
688 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
690 gtk_list_store_clear (model);
692 /* Get row header font color */
693 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
694 gtk_style_context_get_color (style_context,
695 GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_INSENSITIVE,
698 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
699 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
700 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
701 color_string = pango_color_to_string (&p_color);
704 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
707 /* Iterate over families and faces */
708 for (i=0; i<n_families; i++)
711 PangoFontFace **faces;
714 const gchar *fam_name = pango_font_family_get_name (families[i]);
716 pango_font_family_list_faces (families[i], &faces, &n_faces);
718 for (j=0; j<n_faces; j++)
720 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
721 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
722 gchar *font_desc = pango_font_description_to_string (pango_desc);
724 /* foreground_color, family_name, face_name, desc, sample string */
725 g_string_printf (family_and_face, "%s %s",
729 g_string_printf (tmp, ROW_FORMAT_STRING,
731 family_and_face->str,
733 fontchooser->priv->preview_text);
735 gtk_list_store_append (model, &iter);
736 gtk_list_store_set (model, &iter,
737 FAMILY_COLUMN, families[i],
738 FACE_COLUMN, faces[j],
739 PREVIEW_TITLE_COLUMN, family_and_face->str,
740 PREVIEW_TEXT_COLUMN, tmp->str,
743 /* Select the first font or the default font/face from the style context */
744 if ((i == 0 && j == 0) ||
745 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
748 pango_font_description_free(pango_desc);
755 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
759 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
760 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
761 gtk_tree_path_free(path);
764 g_string_free (family_and_face, TRUE);
765 g_string_free (tmp, TRUE);
766 g_free (color_string);
771 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
773 gboolean result = TRUE;
774 GtkFontChooserPrivate *priv = (GtkFontChooserPrivate*) data;
776 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
782 /* If there's no filter string we show the item */
783 if (strlen (search_text) == 0)
786 gtk_tree_model_get (model, iter,
787 PREVIEW_TITLE_COLUMN, &font_name,
790 if (font_name == NULL)
793 split_terms = g_strsplit (search_text, " ", 0);
794 term = split_terms[0];
796 while (term && result)
798 gchar* font_name_casefold = g_utf8_casefold (font_name, -1);
799 gchar* term_casefold = g_utf8_casefold (term, -1);
801 if (g_strrstr (font_name_casefold, term_casefold))
802 result = result && TRUE;
807 term = split_terms[n_terms];
809 g_free (term_casefold);
810 g_free (font_name_casefold);
814 g_strfreev (split_terms);
820 gtk_font_chooser_bootstrap_fontlist (GtkFontChooser* fontchooser)
822 GtkTreeView *treeview = GTK_TREE_VIEW (fontchooser->priv->family_face_list);
823 GtkCellRenderer *cell;
824 GtkTreeViewColumn *col;
826 fontchooser->priv->model = gtk_list_store_new (4,
827 PANGO_TYPE_FONT_FAMILY,
828 PANGO_TYPE_FONT_FACE,
832 fontchooser->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontchooser->priv->model),
834 g_object_unref (fontchooser->priv->model);
836 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter),
838 (gpointer)fontchooser->priv,
841 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontchooser->priv->filter));
842 g_object_unref (fontchooser->priv->filter);
844 gtk_tree_view_set_rules_hint (treeview, TRUE);
845 gtk_tree_view_set_headers_visible (treeview, FALSE);
847 cell = gtk_cell_renderer_text_new ();
848 col = gtk_tree_view_column_new_with_attributes ("Family",
850 "markup", PREVIEW_TEXT_COLUMN,
853 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
855 gtk_tree_view_append_column (treeview, col);
857 populate_list (fontchooser, treeview, fontchooser->priv->model);
862 gtk_font_chooser_finalize (GObject *object)
864 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (object);
866 gtk_font_chooser_ref_family (fontchooser, NULL);
867 gtk_font_chooser_ref_face (fontchooser, NULL);
869 G_OBJECT_CLASS (gtk_font_chooser_parent_class)->finalize (object);
874 gtk_font_chooser_screen_changed (GtkWidget *widget,
875 GdkScreen *previous_screen)
877 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
879 populate_list (fontchooser,
880 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
881 fontchooser->priv->model);
886 gtk_font_chooser_style_updated (GtkWidget *widget)
888 GtkFontChooser *fontchooser = GTK_FONT_CHOOSER (widget);
890 GTK_WIDGET_CLASS (gtk_font_chooser_parent_class)->style_updated (widget);
892 populate_list (fontchooser,
893 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
894 fontchooser->priv->model);
899 gtk_font_chooser_ref_family (GtkFontChooser *fontchooser,
900 PangoFontFamily *family)
902 GtkFontChooserPrivate *priv = fontchooser->priv;
905 family = g_object_ref (family);
907 g_object_unref (priv->family);
908 priv->family = family;
912 gtk_font_chooser_ref_face (GtkFontChooser *fontchooser,
915 GtkFontChooserPrivate *priv = fontchooser->priv;
918 face = g_object_ref (face);
920 g_object_unref (priv->face);
925 /*****************************************************************************
926 * These functions are the main public interface for getting/setting the font.
927 *****************************************************************************/
930 * gtk_font_chooser_get_family:
931 * @fontchooser: a #GtkFontChooser
933 * Gets the #PangoFontFamily representing the selected font family.
935 * Return value: (transfer none): A #PangoFontFamily representing the
936 * selected font family. Font families are a collection of font
937 * faces. The returned object is owned by @fontchooser and must not
938 * be modified or freed.
943 gtk_font_chooser_get_family (GtkFontChooser *fontchooser)
945 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
947 return fontchooser->priv->family;
951 * gtk_font_chooser_get_face:
952 * @fontchooser: a #GtkFontChooser
954 * Gets the #PangoFontFace representing the selected font group
955 * details (i.e. family, slant, weight, width, etc).
957 * Return value: (transfer none): A #PangoFontFace representing the
958 * selected font group details. The returned object is owned by
959 * @fontchooser and must not be modified or freed.
964 gtk_font_chooser_get_face (GtkFontChooser *fontchooser)
966 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
968 return fontchooser->priv->face;
972 * gtk_font_chooser_get_size:
973 * @fontchooser: a #GtkFontChooser
975 * The selected font size.
977 * Return value: A n integer representing the selected font size,
978 * or -1 if no font size is selected.
983 gtk_font_chooser_get_size (GtkFontChooser *fontchooser)
985 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
987 return fontchooser->priv->size;
991 * gtk_font_chooser_get_font_name:
992 * @fontchooser: a #GtkFontChooser
994 * Gets the currently-selected font name.
996 * Note that this can be a different string than what you set with
997 * gtk_font_chooser_set_font_name(), as the font chooser widget may
998 * normalize font names and thus return a string with a different structure.
999 * For example, "Helvetica Italic Bold 12" could be normalized to
1000 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1001 * if you want to compare two font descriptions.
1003 * Return value: (transfer full) (allow-none): A string with the name of the
1004 * current font, or %NULL if no font is selected. You must free this
1005 * string with g_free().
1010 gtk_font_chooser_get_font_name (GtkFontChooser *fontchooser)
1013 PangoFontDescription *desc;
1015 if (!fontchooser->priv->face)
1018 desc = pango_font_face_describe (fontchooser->priv->face);
1019 font_name = pango_font_description_to_string (desc);
1020 pango_font_description_free (desc);
1024 /* This sets the current font, then selecting the appropriate list rows. */
1027 * gtk_font_chooser_set_font_name:
1028 * @fontchooser: a #GtkFontChooser
1029 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1031 * Sets the currently-selected font.
1033 * Note that the @fontchooser needs to know the screen in which it will appear
1034 * for this to work; this can be guaranteed by simply making sure that the
1035 * @fontchooser is inserted in a toplevel window before you call this function.
1037 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1038 * such font exists or if the @fontchooser doesn't belong to a particular
1044 gtk_font_chooser_set_font_name (GtkFontChooser *fontchooser,
1045 const gchar *fontname)
1047 GtkFontChooserPrivate *priv = fontchooser->priv;
1051 PangoFontDescription *desc;
1052 gboolean found = FALSE;
1054 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1055 g_return_val_if_fail (fontname != NULL, FALSE);
1057 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
1060 desc = pango_font_description_from_string (fontname);
1061 family_name = (gchar*)pango_font_description_get_family (desc);
1065 pango_font_description_free (desc);
1069 /* We make sure the filter is clear */
1070 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1072 /* We find the matching family/face */
1073 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1075 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1077 PangoFontFace *face;
1078 PangoFontDescription *tmp_desc;
1080 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1084 tmp_desc = pango_font_face_describe (face);
1085 if (pango_font_description_get_size_is_absolute (desc))
1086 pango_font_description_set_absolute_size (tmp_desc,
1087 pango_font_description_get_size (desc));
1089 pango_font_description_set_size (tmp_desc,
1090 pango_font_description_get_size (desc));
1093 if (pango_font_description_equal (desc, tmp_desc))
1096 gint size = pango_font_description_get_size (desc);
1100 if (pango_font_description_get_size_is_absolute (desc))
1101 size = size * PANGO_SCALE;
1102 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1103 size / PANGO_SCALE);
1106 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1111 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1115 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1121 gtk_tree_path_free (path);
1127 g_object_unref (face);
1128 pango_font_description_free (tmp_desc);
1134 pango_font_description_free (desc);
1135 g_object_notify (G_OBJECT (fontchooser), "font-name");
1141 * gtk_font_chooser_get_preview_text:
1142 * @fontchooser: a #GtkFontChooser
1144 * Gets the text displayed in the preview area.
1146 * Return value: (transfer none): the text displayed in the
1147 * preview area. This string is owned by the widget and
1148 * should not be modified or freed
1153 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
1155 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
1156 return (const gchar*)fontchooser->priv->preview_text;
1161 * gtk_font_chooser_set_preview_text:
1162 * @fontchooser: a #GtkFontChooser
1163 * @text: (transfer none): the text to display in the preview area
1165 * Sets the text displayed in the preview area.
1166 * The @text is used to show how the selected font looks.
1171 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
1174 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1175 g_return_if_fail (text != NULL);
1177 g_free (fontchooser->priv->preview_text);
1178 fontchooser->priv->preview_text = g_strdup (text);
1180 populate_list (fontchooser,
1181 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1182 fontchooser->priv->model);
1184 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1186 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1190 * gtk_font_chooser_get_show_preview_entry:
1191 * @fontchooser: a #GtkFontChooser
1193 * Return value: %TRUE if the preview entry is shown or %FALSE if
1198 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
1200 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
1202 return fontchooser->priv->show_preview_entry;
1206 * gtk_font_chooser_set_show_preview_entry:
1207 * @fontchooser: a #GtkFontChooser
1208 * @show_preview_entry: whether to show the editable preview entry or not
1210 * Shows or hides the editable preview entry.
1214 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
1215 gboolean show_preview_entry)
1217 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
1219 if (show_preview_entry)
1220 gtk_widget_show (fontchooser->priv->preview_scrolled_window);
1222 gtk_widget_hide (fontchooser->priv->preview_scrolled_window);
1224 fontchooser->priv->show_preview_entry = show_preview_entry;
1225 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");