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 "gtkfontchooserwidget.h"
29 #include "gtkfontchooser.h"
30 #include "gtkfontchooserutils.h"
31 #include "gtkcellrenderertext.h"
37 #include "gtkliststore.h"
39 #include "gtktextview.h"
40 #include "gtktreeselection.h"
41 #include "gtktreeview.h"
42 #include "gtkscrolledwindow.h"
44 #include "gtkaccessible.h"
45 #include "gtkbuildable.h"
46 #include "gtkprivate.h"
48 #include "gtkspinbutton.h"
49 #include "gtknotebook.h"
50 #include "gtkwidget.h"
54 * SECTION:gtkfontchooser
55 * @Short_description: A widget for selecting fonts
56 * @Title: GtkFontChooserWidget
57 * @See_also: #GtkFontChooserDialog
59 * The #GtkFontChooserWidget widget lists the available fonts,
60 * styles and sizes, allowing the user to select a font. It is
61 * used in the #GtkFontChooserDialog widget to provide a
62 * dialog box for selecting fonts.
64 * To set the font which is initially selected, use
65 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
67 * To get the selected font use gtk_font_chooser_get_font() or
68 * gtk_font_chooser_get_font_desc().
70 * To change the text which is shown in the preview area, use
71 * gtk_font_chooser_set_preview_text().
77 struct _GtkFontChooserWidgetPrivate
79 GtkWidget *search_entry;
80 GtkWidget *family_face_list;
81 GtkWidget *list_scrolled_window;
82 GtkWidget *empty_list;
83 GtkWidget *list_notebook;
85 GtkTreeModel *filter_model;
89 gboolean show_preview_entry;
92 GtkWidget *size_slider;
94 PangoFontDescription *font_desc;
97 PangoFontFamily *family;
99 gulong cursor_changed_handler;
101 GtkFontFilterFunc filter_func;
102 gpointer filter_data;
103 GDestroyNotify filter_data_destroy;
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 /* These are the sizes of the font, style & size lists. */
111 #define FONT_LIST_HEIGHT 136
112 #define FONT_LIST_WIDTH 190
113 #define FONT_STYLE_LIST_WIDTH 170
114 #define FONT_SIZE_LIST_WIDTH 60
116 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
118 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
120 /* These are what we use as the standard font sizes, for the size list.
122 static const gint font_sizes[] = {
123 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
133 static void gtk_font_chooser_widget_set_property (GObject *object,
137 static void gtk_font_chooser_widget_get_property (GObject *object,
141 static void gtk_font_chooser_widget_finalize (GObject *object);
142 static void gtk_font_chooser_widget_dispose (GObject *object);
144 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
145 GdkScreen *previous_screen);
146 static void gtk_font_chooser_widget_style_updated (GtkWidget *widget);
148 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
150 static void gtk_font_chooser_widget_select_font (GtkFontChooserWidget *fontchooser);
152 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
153 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
154 const gchar *fontname);
156 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
157 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
158 PangoFontDescription *font_desc);
161 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
162 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
165 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
166 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
167 gboolean show_preview_entry);
169 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
171 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
172 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
173 gtk_font_chooser_widget_iface_init))
176 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
181 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
182 widget_class->style_updated = gtk_font_chooser_widget_style_updated;
184 gobject_class->dispose = gtk_font_chooser_widget_dispose;
185 gobject_class->finalize = gtk_font_chooser_widget_finalize;
186 gobject_class->set_property = gtk_font_chooser_widget_set_property;
187 gobject_class->get_property = gtk_font_chooser_widget_get_property;
189 _gtk_font_chooser_install_properties (gobject_class);
191 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
195 gtk_font_chooser_widget_set_property (GObject *object,
200 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
204 case GTK_FONT_CHOOSER_PROP_FONT:
205 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
207 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
208 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
210 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
211 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
213 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
214 gtk_font_chooser_widget_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_widget_get_property (GObject *object,
228 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
232 case GTK_FONT_CHOOSER_PROP_FONT:
233 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
235 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
236 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
238 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
239 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
241 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
242 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 text_changed_cb (GtkEntry *entry,
253 GtkFontChooserWidget *fc)
255 GtkFontChooserWidgetPrivate *priv = fc->priv;
258 text = gtk_entry_get_text (entry);
260 if (text == NULL || text[0] == '\0')
264 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
265 g_object_set (G_OBJECT (priv->search_entry),
266 "secondary-icon-gicon", icon,
267 "secondary-icon-activatable", FALSE,
268 "secondary-icon-sensitive", FALSE,
270 g_object_unref (icon);
274 if (!gtk_entry_get_icon_activatable (GTK_ENTRY (priv->search_entry), GTK_ENTRY_ICON_SECONDARY))
278 icon = g_themed_icon_new_with_default_fallbacks ("edit-clear-symbolic");
279 g_object_set (G_OBJECT (priv->search_entry),
280 "secondary-icon-gicon", icon,
281 "secondary-icon-activatable", TRUE,
282 "secondary-icon-sensitive", TRUE,
284 g_object_unref (icon);
288 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
292 icon_press_cb (GtkEntry *entry,
293 GtkEntryIconPosition pos,
297 gtk_entry_set_text (entry, "");
301 slider_change_cb (GtkAdjustment *adjustment,
304 GtkFontChooserWidget *fc = (GtkFontChooserWidget*)user_data;
305 GtkFontChooserWidgetPrivate *priv = fc->priv;
306 GtkAdjustment *spin_adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON (priv->size_spin));
307 gdouble slider_value = gtk_adjustment_get_value (adjustment);
308 gdouble spin_value = gtk_adjustment_get_value (spin_adj);
310 if (slider_value != spin_value)
311 gtk_adjustment_set_value (spin_adj,
312 gtk_adjustment_get_value (adjustment));
316 spin_change_cb (GtkAdjustment *adjustment,
319 PangoFontDescription *desc;
320 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
321 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
322 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
323 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 (pango_font_description_get_size_is_absolute (priv->font_desc))
332 pango_font_description_set_absolute_size (priv->font_desc, size);
334 pango_font_description_set_size (priv->font_desc, priv->size);
336 /* If the new value is lower than the lower bound of the slider, we set
337 * the slider adjustment to the lower bound value if it is not already set
339 if (size < gtk_adjustment_get_lower (slider_adj) &&
340 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_lower (slider_adj))
341 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
343 /* If the new value is upper than the upper bound of the slider, we set
344 * the slider adjustment to the upper bound value if it is not already set
346 else if (size > gtk_adjustment_get_upper (slider_adj) &&
347 gtk_adjustment_get_value (slider_adj) != gtk_adjustment_get_upper (slider_adj))
348 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
350 /* If the new value is not already set on the slider we set it */
351 else if (size != gtk_adjustment_get_value (slider_adj))
352 gtk_adjustment_set_value (slider_adj, size);
354 gtk_widget_queue_draw (priv->preview);
356 g_object_notify (G_OBJECT (fontchooser), "font");
357 g_object_notify (G_OBJECT (fontchooser), "font-desc");
361 set_range_marks (GtkFontChooserWidgetPrivate *priv,
362 GtkWidget *size_slider,
372 sizes = (gint*)font_sizes;
373 length = G_N_ELEMENTS (font_sizes);
376 gtk_scale_clear_marks (GTK_SCALE (size_slider));
378 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
380 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
381 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
383 value = gtk_adjustment_get_value (adj);
384 if (value > (gdouble) sizes[length-1])
385 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
386 else if (value < (gdouble) sizes[0])
387 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
389 for (i = 0; i < length; i++)
390 gtk_scale_add_mark (GTK_SCALE (size_slider),
392 GTK_POS_BOTTOM, NULL);
396 row_activated_cb (GtkTreeView *view,
398 GtkTreeViewColumn *column,
401 GtkFontChooserWidget *fontchooser = user_data;
404 fontname = gtk_font_chooser_widget_get_font (fontchooser);
405 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
410 cursor_changed_cb (GtkTreeView *treeview,
413 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
414 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
416 PangoFontFamily *family;
418 PangoFontDescription *desc;
424 GtkTreePath *path = gtk_tree_path_new ();
426 gtk_tree_view_get_cursor (treeview, &path, NULL);
431 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter_model), &iter, path))
433 gtk_tree_path_free (path);
437 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
439 FAMILY_COLUMN, &family,
442 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
444 gtk_tree_path_free (path);
447 desc = pango_font_face_describe (face);
448 pango_font_description_set_size (desc, priv->size);
449 gtk_widget_override_font (priv->preview, desc);
451 pango_font_face_list_sizes (face, &sizes, &n_sizes);
452 /* It seems not many fonts actually have a sane set of sizes */
453 for (i = 0; i < n_sizes; i++)
454 sizes[i] = sizes[i] / PANGO_SCALE;
456 set_range_marks (priv, priv->size_slider, sizes, n_sizes);
459 g_object_unref (priv->family);
460 priv->family = family;
463 g_object_unref (priv->face);
467 pango_font_description_free (priv->font_desc);
468 priv->font_desc = desc;
470 g_object_notify (G_OBJECT (fontchooser), "font");
471 g_object_notify (G_OBJECT (fontchooser), "font-desc");
475 zoom_preview_cb (GtkWidget *scrolled_window,
476 GdkEventScroll *event,
479 GtkFontChooserWidget *fc = (GtkFontChooserWidget*)user_data;
480 GtkFontChooserWidgetPrivate *priv = fc->priv;
482 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
484 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
485 gtk_adjustment_set_value (adj,
486 gtk_adjustment_get_value (adj) +
487 gtk_adjustment_get_step_increment (adj));
488 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
489 gtk_adjustment_set_value (adj,
490 gtk_adjustment_get_value (adj) -
491 gtk_adjustment_get_step_increment (adj));
496 row_inserted_cb (GtkTreeModel *model,
501 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
502 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
504 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
508 row_deleted_cb (GtkTreeModel *model,
512 GtkFontChooserWidget *fontchooser = (GtkFontChooserWidget*)user_data;
513 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
515 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
516 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
520 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
523 GtkFontChooserWidgetPrivate *priv;
524 const PangoFontDescription *font_desc;
525 GtkWidget *scrolled_win;
528 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
529 GTK_TYPE_FONT_CHOOSER_WIDGET,
530 GtkFontChooserWidgetPrivate);
532 priv = fontchooser->priv;
534 /* Default preview string */
535 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
536 priv->show_preview_entry = TRUE;
538 /* Getting the default size */
539 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)));
540 priv->size = pango_font_description_get_size (font_desc);
543 priv->font_desc = NULL;
545 gtk_widget_push_composite_child ();
547 /* Creating fundamental widgets for the private struct */
548 priv->search_entry = gtk_entry_new ();
549 priv->family_face_list = gtk_tree_view_new ();
550 priv->preview = gtk_entry_new ();
551 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
552 (gdouble) font_sizes[0],
553 (gdouble) font_sizes[G_N_ELEMENTS (font_sizes) - 1],
556 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
558 /** Bootstrapping widget layout **/
559 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
561 /* Main font family/face view */
562 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
563 scrolled_win = priv->list_scrolled_window;
564 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
565 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
566 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
567 GTK_SHADOW_ETCHED_IN);
568 gtk_widget_set_size_request (scrolled_win, 400, 300);
569 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
571 /* Text to display when list is empty */
572 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
573 gtk_widget_set_margin_top (priv->empty_list, 12);
574 gtk_widget_set_margin_left (priv->empty_list, 12);
575 gtk_widget_set_margin_right (priv->empty_list, 12);
576 gtk_widget_set_margin_bottom (priv->empty_list, 12);
577 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
578 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
580 priv->list_notebook = gtk_notebook_new ();
581 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
582 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
583 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
586 grid = gtk_grid_new ();
588 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
589 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
591 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
592 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
593 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
595 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
596 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
598 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
599 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
600 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
602 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
603 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
605 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
607 /* Setting the adjustment values for the size slider */
608 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
609 (gdouble)(priv->size / PANGO_SCALE));
610 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
611 (gdouble)(priv->size / PANGO_SCALE));
613 gtk_widget_show_all (GTK_WIDGET (fontchooser));
614 gtk_widget_hide (GTK_WIDGET (fontchooser));
616 /* Treeview column and model bootstrapping */
617 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
619 /* Set default preview text */
620 gtk_entry_set_text (GTK_ENTRY (priv->preview),
621 pango_language_get_sample_string (NULL));
623 /* Set search icon and place holder text */
624 icon = g_themed_icon_new_with_default_fallbacks ("edit-find-symbolic");
625 g_object_set (G_OBJECT (priv->search_entry),
626 "secondary-icon-gicon", icon,
627 "secondary-icon-activatable", FALSE,
628 "secondary-icon-sensitive", FALSE,
630 g_object_unref (icon);
632 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
634 /** Callback connections **/
635 g_signal_connect (priv->search_entry, "notify::text",
636 G_CALLBACK (text_changed_cb), fontchooser);
637 g_signal_connect (priv->search_entry,
638 "icon-press", G_CALLBACK (icon_press_cb), NULL);
640 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
641 "value-changed", G_CALLBACK (slider_change_cb), fontchooser);
642 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
643 "value-changed", G_CALLBACK (spin_change_cb), fontchooser);
645 priv->cursor_changed_handler =
646 g_signal_connect (priv->family_face_list, "cursor-changed",
647 G_CALLBACK (cursor_changed_cb), fontchooser);
649 g_signal_connect (priv->family_face_list, "row-activated",
650 G_CALLBACK (row_activated_cb), fontchooser);
652 /* Zoom on preview scroll */
653 g_signal_connect (priv->preview, "scroll-event",
654 G_CALLBACK (zoom_preview_cb), fontchooser);
656 g_signal_connect (priv->size_slider, "scroll-event",
657 G_CALLBACK (zoom_preview_cb), fontchooser);
659 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, G_N_ELEMENTS (font_sizes));
661 /* Font list empty hides the scrolledwindow */
662 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
663 G_CALLBACK (row_deleted_cb), fontchooser);
664 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
665 G_CALLBACK (row_inserted_cb), fontchooser);
667 /* Set default focus */
668 gtk_widget_pop_composite_child ();
670 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
674 * gtk_font_chooser_widget_new:
676 * Creates a new #GtkFontChooserWidget.
678 * Return value: a new #GtkFontChooserWidget
683 gtk_font_chooser_widget_new (void)
685 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
689 cmp_families (const void *a,
692 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
693 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
695 return g_utf8_collate (a_name, b_name);
699 populate_list (GtkFontChooserWidget *fontchooser,
700 GtkTreeView *treeview,
703 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
704 GtkStyleContext *style_context;
705 PangoFontDescription *default_font;
706 PangoFontDescription *selected_font;
709 GtkTreeIter match_row;
713 PangoFontFamily **families;
715 gchar *family_and_face;
717 if (!gtk_widget_has_screen (GTK_WIDGET (fontchooser)))
720 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
724 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
726 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
727 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
731 selected_font = pango_font_face_describe (priv->face);
733 selected_font = NULL;
735 gtk_list_store_clear (model);
739 /* Iterate over families and faces */
740 for (i = 0; i < n_families; i++)
743 PangoFontFace **faces;
745 const gchar *fam_name = pango_font_family_get_name (families[i]);
747 pango_font_family_list_faces (families[i], &faces, &n_faces);
749 for (j = 0; j < n_faces; j++)
751 PangoFontDescription *pango_desc;
752 const gchar *face_name;
755 pango_desc = pango_font_face_describe (faces[j]);
756 face_name = pango_font_face_get_face_name (faces[j]);
757 font_desc = pango_font_description_to_string (pango_desc);
759 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
760 tmp = g_markup_printf_escaped (ROW_FORMAT_STRING,
763 fontchooser->priv->preview_text);
765 gtk_list_store_insert_with_values (model, &iter, -1,
766 FAMILY_COLUMN, families[i],
767 FACE_COLUMN, faces[j],
768 PREVIEW_TITLE_COLUMN, family_and_face,
769 PREVIEW_TEXT_COLUMN, tmp,
772 /* Select the current font,
773 * the default font/face from the theme,
777 selected_font != NULL &&
778 pango_font_description_equal (selected_font, pango_desc))
784 strcmp (fam_name, pango_font_description_get_family (default_font)) == 0)
795 pango_font_description_free (pango_desc);
796 g_free (family_and_face);
804 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
807 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
808 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.5, 0.5);
809 gtk_tree_path_free (path);
813 pango_font_description_free (selected_font);
819 visible_func (GtkTreeModel *model,
823 GtkFontChooserWidgetPrivate *priv = user_data;
824 gboolean result = TRUE;
825 const gchar *search_text;
827 gchar *font_name, *font_name_casefold, *term_casefold;
830 if (priv->filter_func != NULL)
832 PangoFontFamily *family;
835 gtk_tree_model_get (model, iter,
836 FAMILY_COLUMN, &family,
840 result = priv->filter_func (family, face, priv->filter_data);
842 g_object_unref (family);
843 g_object_unref (face);
849 /* If there's no filter string we show the item */
850 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
851 if (strlen (search_text) == 0)
854 gtk_tree_model_get (model, iter,
855 PREVIEW_TITLE_COLUMN, &font_name,
858 if (font_name == NULL)
861 split_terms = g_strsplit (search_text, " ", 0);
862 font_name_casefold = g_utf8_casefold (font_name, -1);
864 for (i = 0; split_terms[i] && result; i++)
866 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
868 if (!strstr (font_name_casefold, term_casefold))
871 g_free (term_casefold);
874 g_free (font_name_casefold);
876 g_strfreev (split_terms);
882 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
884 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
885 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
886 GtkCellRenderer *cell;
887 GtkTreeViewColumn *col;
889 priv->model = gtk_list_store_new (4,
890 PANGO_TYPE_FONT_FAMILY,
891 PANGO_TYPE_FONT_FACE,
895 priv->filter_model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->model), NULL);
896 g_object_unref (priv->model);
898 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
899 visible_func, (gpointer)priv, NULL);
901 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (priv->filter_model));
902 g_object_unref (priv->filter_model);
904 gtk_tree_view_set_rules_hint (treeview, TRUE);
905 gtk_tree_view_set_headers_visible (treeview, FALSE);
907 cell = gtk_cell_renderer_text_new ();
908 col = gtk_tree_view_column_new_with_attributes (_("Font Family"),
910 "markup", PREVIEW_TEXT_COLUMN,
913 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
915 gtk_tree_view_append_column (treeview, col);
917 populate_list (fontchooser, treeview, priv->model);
921 gtk_font_chooser_widget_dispose (GObject *object)
923 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
924 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
926 if (priv->cursor_changed_handler != 0)
928 g_signal_handler_disconnect (priv->family_face_list,
929 priv->cursor_changed_handler);
930 priv->cursor_changed_handler = 0;
933 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->dispose (object);
937 gtk_font_chooser_widget_finalize (GObject *object)
939 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
940 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
943 pango_font_description_free (priv->font_desc);
946 g_object_unref (priv->family);
949 g_object_unref (priv->face);
951 if (priv->filter_data_destroy)
952 priv->filter_data_destroy (priv->filter_data);
954 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
958 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
959 GdkScreen *previous_screen)
961 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
962 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
964 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
965 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
967 populate_list (fontchooser,
968 GTK_TREE_VIEW (priv->family_face_list),
971 gtk_font_chooser_widget_select_font (fontchooser);
975 gtk_font_chooser_widget_style_updated (GtkWidget *widget)
977 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
979 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->style_updated (widget);
981 populate_list (fontchooser,
982 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
983 fontchooser->priv->model);
986 static PangoFontFamily *
987 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
989 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
991 return fontchooser->priv->family;
994 static PangoFontFace *
995 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
997 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
999 return fontchooser->priv->face;
1003 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1005 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1007 return fontchooser->priv->size;
1011 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1013 return pango_font_description_to_string (fontchooser->priv->font_desc);
1016 static PangoFontDescription *
1017 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1019 return fontchooser->priv->font_desc;
1023 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1024 const gchar *fontname)
1026 PangoFontDescription *font_desc;
1028 font_desc = pango_font_description_from_string (fontname);
1029 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1033 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1034 PangoFontDescription *font_desc)
1036 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1038 if (font_desc && priv->font_desc &&
1039 pango_font_description_equal (font_desc, priv->font_desc))
1041 pango_font_description_free (font_desc);
1045 if (priv->font_desc)
1046 pango_font_description_free (priv->font_desc);
1048 priv->font_desc = font_desc; /* adopted */
1050 priv->font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1052 gtk_font_chooser_widget_select_font (fontchooser);
1056 gtk_font_chooser_widget_select_font (GtkFontChooserWidget *fontchooser)
1058 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1059 const PangoFontDescription *desc;
1060 const gchar *family_name;
1062 gboolean font_size_is_absolute;
1065 gboolean found = FALSE;
1067 desc = priv->font_desc;
1068 g_assert (desc != NULL);
1070 font_size = pango_font_description_get_size (desc);
1071 font_size_is_absolute = pango_font_description_get_size_is_absolute (desc);
1072 family_name = pango_font_description_get_family (desc);
1074 /* We make sure the filter is clear */
1075 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1080 /* We find the matching family/face */
1081 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter_model), &iter);
1083 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter_model), &iter))
1085 PangoFontFace *face;
1086 PangoFontDescription *tmp_desc;
1088 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter_model), &iter,
1092 tmp_desc = pango_font_face_describe (face);
1093 if (font_size_is_absolute)
1094 pango_font_description_set_absolute_size (tmp_desc, font_size);
1096 pango_font_description_set_size (tmp_desc, font_size);
1098 if (pango_font_description_equal (desc, tmp_desc))
1104 if (font_size_is_absolute)
1105 font_size *= PANGO_SCALE;
1106 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1107 font_size / PANGO_SCALE);
1110 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model), &iter);
1114 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1118 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1124 gtk_tree_path_free (path);
1130 g_object_unref (face);
1131 pango_font_description_free (tmp_desc);
1140 gtk_tree_selection_unselect_all
1141 (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list)));
1146 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1148 return fontchooser->priv->preview_text;
1152 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1155 g_free (fontchooser->priv->preview_text);
1156 fontchooser->priv->preview_text = g_strdup (text);
1158 populate_list (fontchooser,
1159 GTK_TREE_VIEW (fontchooser->priv->family_face_list),
1160 fontchooser->priv->model);
1162 gtk_entry_set_text (GTK_ENTRY (fontchooser->priv->preview), text);
1164 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1168 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1170 return fontchooser->priv->show_preview_entry;
1174 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1175 gboolean show_preview_entry)
1177 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1179 if (priv->show_preview_entry != show_preview_entry)
1181 fontchooser->priv->show_preview_entry = show_preview_entry;
1183 if (show_preview_entry)
1184 gtk_widget_show (fontchooser->priv->preview);
1186 gtk_widget_hide (fontchooser->priv->preview);
1188 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1193 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1194 GtkFontFilterFunc filter,
1196 GDestroyNotify destroy)
1198 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1199 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1201 if (priv->filter_data_destroy)
1202 priv->filter_data_destroy (priv->filter_data);
1204 priv->filter_func = filter;
1205 priv->filter_data = data;
1206 priv->filter_data_destroy = destroy;
1208 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter_model));
1212 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1214 iface->get_font_family = gtk_font_chooser_widget_get_family;
1215 iface->get_font_face = gtk_font_chooser_widget_get_face;
1216 iface->get_font_size = gtk_font_chooser_widget_get_size;
1217 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;