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, see <http://www.gnu.org/licenses/>.
21 #include <glib/gprintf.h>
26 #include "gtkfontchooserwidget.h"
28 #include "gtkadjustment.h"
29 #include "gtkbuildable.h"
31 #include "gtkcellrenderertext.h"
33 #include "gtksearchentry.h"
35 #include "gtkfontchooser.h"
36 #include "gtkfontchooserutils.h"
39 #include "gtkliststore.h"
40 #include "gtknotebook.h"
41 #include "gtkprivate.h"
43 #include "gtkscrolledwindow.h"
44 #include "gtkspinbutton.h"
45 #include "gtktextview.h"
46 #include "gtktreeselection.h"
47 #include "gtktreeview.h"
48 #include "gtkwidget.h"
51 * SECTION:gtkfontchooserwidget
52 * @Short_description: A widget for selecting fonts
53 * @Title: GtkFontChooserWidget
54 * @See_also: #GtkFontChooserDialog
56 * The #GtkFontChooserWidget widget lists the available fonts,
57 * styles and sizes, allowing the user to select a font. It is
58 * used in the #GtkFontChooserDialog widget to provide a
59 * dialog box for selecting fonts.
61 * To set the font which is initially selected, use
62 * gtk_font_chooser_set_font() or gtk_font_chooser_set_font_desc().
64 * To get the selected font use gtk_font_chooser_get_font() or
65 * gtk_font_chooser_get_font_desc().
67 * To change the text which is shown in the preview area, use
68 * gtk_font_chooser_set_preview_text().
74 struct _GtkFontChooserWidgetPrivate
76 GtkWidget *search_entry;
77 GtkWidget *family_face_list;
78 GtkCellRenderer *family_face_cell;
79 GtkWidget *list_scrolled_window;
80 GtkWidget *empty_list;
81 GtkWidget *list_notebook;
83 GtkTreeModel *filter_model;
87 gboolean show_preview_entry;
90 GtkWidget *size_slider;
92 PangoFontDescription *font_desc;
93 GtkTreeIter font_iter; /* invalid if font not available or pointer into model
94 (not filter_model) to the row containing font */
95 GtkFontFilterFunc filter_func;
97 GDestroyNotify filter_data_destroy;
100 /* This is the initial fixed height and the top padding of the preview entry */
101 #define PREVIEW_HEIGHT 72
102 #define PREVIEW_TOP_PADDING 6
104 /* These are the sizes of the font, style & size lists. */
105 #define FONT_LIST_HEIGHT 136
106 #define FONT_LIST_WIDTH 190
107 #define FONT_STYLE_LIST_WIDTH 170
108 #define FONT_SIZE_LIST_WIDTH 60
110 #define NO_FONT_MATCHED_SEARCH N_("No fonts matched your search. You can revise your search and try again.")
119 static void gtk_font_chooser_widget_set_property (GObject *object,
123 static void gtk_font_chooser_widget_get_property (GObject *object,
127 static void gtk_font_chooser_widget_finalize (GObject *object);
129 static void gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
130 GdkScreen *previous_screen);
132 static void gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser);
134 static gboolean gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
135 const PangoFontDescription *font_desc,
137 static void gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser);
139 static gchar *gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser);
140 static void gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
141 const gchar *fontname);
143 static PangoFontDescription *gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser);
144 static void gtk_font_chooser_widget_merge_font_desc(GtkFontChooserWidget *fontchooser,
145 PangoFontDescription *font_desc,
147 static void gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
148 PangoFontDescription *font_desc);
151 static const gchar *gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser);
152 static void gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
155 static gboolean gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser);
156 static void gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
157 gboolean show_preview_entry);
159 static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
161 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserWidget, gtk_font_chooser_widget, GTK_TYPE_BOX,
162 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
163 gtk_font_chooser_widget_iface_init))
166 gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
171 widget_class->screen_changed = gtk_font_chooser_widget_screen_changed;
173 gobject_class->finalize = gtk_font_chooser_widget_finalize;
174 gobject_class->set_property = gtk_font_chooser_widget_set_property;
175 gobject_class->get_property = gtk_font_chooser_widget_get_property;
177 _gtk_font_chooser_install_properties (gobject_class);
179 g_type_class_add_private (klass, sizeof (GtkFontChooserWidgetPrivate));
183 gtk_font_chooser_widget_set_property (GObject *object,
188 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
192 case GTK_FONT_CHOOSER_PROP_FONT:
193 gtk_font_chooser_widget_set_font (fontchooser, g_value_get_string (value));
195 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
196 gtk_font_chooser_widget_take_font_desc (fontchooser, g_value_dup_boxed (value));
198 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
199 gtk_font_chooser_widget_set_preview_text (fontchooser, g_value_get_string (value));
201 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
202 gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211 gtk_font_chooser_widget_get_property (GObject *object,
216 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
220 case GTK_FONT_CHOOSER_PROP_FONT:
221 g_value_take_string (value, gtk_font_chooser_widget_get_font (fontchooser));
223 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
224 g_value_set_boxed (value, gtk_font_chooser_widget_get_font_desc (fontchooser));
226 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
227 g_value_set_string (value, gtk_font_chooser_widget_get_preview_text (fontchooser));
229 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
230 g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gtk_font_chooser_widget_refilter_font_list (GtkFontChooserWidget *fontchooser)
241 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (fontchooser->priv->filter_model));
242 gtk_font_chooser_widget_ensure_selection (fontchooser);
246 text_changed_cb (GtkEntry *entry,
248 GtkFontChooserWidget *fc)
250 gtk_font_chooser_widget_refilter_font_list (fc);
254 size_change_cb (GtkAdjustment *adjustment,
257 GtkFontChooserWidget *fontchooser = user_data;
258 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
259 PangoFontDescription *font_desc;
260 gdouble size = gtk_adjustment_get_value (adjustment);
262 font_desc = pango_font_description_new ();
263 if (pango_font_description_get_size_is_absolute (priv->font_desc))
264 pango_font_description_set_absolute_size (font_desc, size * PANGO_SCALE);
266 pango_font_description_set_size (font_desc, size * PANGO_SCALE);
268 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
272 gtk_font_chooser_widget_update_marks (GtkFontChooserWidget *fontchooser)
274 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
280 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
284 gtk_tree_model_get (priv->model, &priv->font_iter,
288 pango_font_face_list_sizes (face, &font_sizes, &n_sizes);
290 /* It seems not many fonts actually have a sane set of sizes */
291 for (i = 0; i < n_sizes; i++)
292 font_sizes[i] = font_sizes[i] / PANGO_SCALE;
294 g_object_unref (face);
304 static const gint fallback_sizes[] = {
305 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
308 sizes = fallback_sizes;
309 n_sizes = G_N_ELEMENTS (fallback_sizes);
316 gtk_scale_clear_marks (GTK_SCALE (priv->size_slider));
318 adj = gtk_range_get_adjustment(GTK_RANGE (priv->size_slider));
320 /* ensure clamping doesn't callback into font resizing code */
321 g_signal_handlers_block_by_func (adj, size_change_cb, fontchooser);
322 gtk_adjustment_configure (adj,
323 gtk_adjustment_get_value (adj),
326 gtk_adjustment_get_step_increment (adj),
327 gtk_adjustment_get_page_increment (adj),
328 gtk_adjustment_get_page_size (adj));
329 g_signal_handlers_unblock_by_func (adj, size_change_cb, fontchooser);
331 for (i = 0; i < n_sizes; i++)
333 gtk_scale_add_mark (GTK_SCALE (priv->size_slider),
335 GTK_POS_BOTTOM, NULL);
342 row_activated_cb (GtkTreeView *view,
344 GtkTreeViewColumn *column,
347 GtkFontChooserWidget *fontchooser = user_data;
350 fontname = gtk_font_chooser_widget_get_font (fontchooser);
351 _gtk_font_chooser_font_activated (GTK_FONT_CHOOSER (fontchooser), fontname);
355 static PangoFontDescription *
356 tree_model_get_font_description (GtkTreeModel *model,
359 PangoFontDescription *desc;
361 GtkTreeIter child_iter;
363 gtk_tree_model_get (model, iter,
364 FONT_DESC_COLUMN, &desc,
369 gtk_tree_model_get (model, iter,
372 desc = pango_font_face_describe (face);
373 g_object_unref (face);
375 if (GTK_IS_TREE_MODEL_FILTER (model))
377 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model),
381 model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model));
384 gtk_list_store_set (GTK_LIST_STORE (model), iter,
385 FONT_DESC_COLUMN, desc,
392 cursor_changed_cb (GtkTreeView *treeview,
395 GtkFontChooserWidget *fontchooser = user_data;
396 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
397 PangoFontDescription *desc;
398 GtkTreeIter filter_iter, iter;
399 GtkTreePath *path = NULL;
401 gtk_tree_view_get_cursor (treeview, &path, NULL);
406 if (!gtk_tree_model_get_iter (priv->filter_model, &filter_iter, path))
408 gtk_tree_path_free (path);
412 gtk_tree_path_free (path);
414 gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
417 desc = tree_model_get_font_description (priv->model, &iter);
419 gtk_font_chooser_widget_merge_font_desc (fontchooser, desc, &iter);
423 zoom_preview_cb (GtkWidget *scrolled_window,
424 GdkEventScroll *event,
427 GtkFontChooserWidget *fc = user_data;
428 GtkFontChooserWidgetPrivate *priv = fc->priv;
429 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
431 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
432 gtk_adjustment_set_value (adj,
433 gtk_adjustment_get_value (adj) +
434 gtk_adjustment_get_step_increment (adj));
435 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
436 gtk_adjustment_set_value (adj,
437 gtk_adjustment_get_value (adj) -
438 gtk_adjustment_get_step_increment (adj));
443 row_inserted_cb (GtkTreeModel *model,
448 GtkFontChooserWidget *fontchooser = user_data;
449 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
451 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 0);
455 row_deleted_cb (GtkTreeModel *model,
459 GtkFontChooserWidget *fontchooser = user_data;
460 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
462 if (gtk_tree_model_iter_n_children (model, NULL) == 0)
463 gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->list_notebook), 1);
467 gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
469 GtkFontChooserWidgetPrivate *priv;
470 GtkWidget *scrolled_win;
473 fontchooser->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooser,
474 GTK_TYPE_FONT_CHOOSER_WIDGET,
475 GtkFontChooserWidgetPrivate);
477 priv = fontchooser->priv;
479 /* Default preview string */
480 priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
481 priv->show_preview_entry = TRUE;
482 priv->font_desc = pango_font_description_new ();
484 gtk_widget_push_composite_child ();
486 /* Creating fundamental widgets for the private struct */
487 priv->search_entry = gtk_search_entry_new ();
488 priv->family_face_list = gtk_tree_view_new ();
489 gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->family_face_list), FALSE);
490 priv->preview = gtk_entry_new ();
491 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
493 (gdouble)(G_MAXINT / PANGO_SCALE),
496 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
498 /** Bootstrapping widget layout **/
499 gtk_box_set_spacing (GTK_BOX (fontchooser), 6);
501 /* Main font family/face view */
502 priv->list_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
503 scrolled_win = priv->list_scrolled_window;
504 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
505 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
506 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
507 GTK_SHADOW_ETCHED_IN);
508 gtk_widget_set_size_request (scrolled_win, 400, 300);
509 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
511 /* Text to display when list is empty */
512 priv->empty_list = gtk_label_new (_(NO_FONT_MATCHED_SEARCH));
513 gtk_widget_set_margin_top (priv->empty_list, 12);
514 gtk_widget_set_margin_left (priv->empty_list, 12);
515 gtk_widget_set_margin_right (priv->empty_list, 12);
516 gtk_widget_set_margin_bottom (priv->empty_list, 12);
517 gtk_widget_set_halign (priv->empty_list, GTK_ALIGN_CENTER);
518 gtk_widget_set_valign (priv->empty_list, GTK_ALIGN_START);
520 priv->list_notebook = gtk_notebook_new ();
521 gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->list_notebook), FALSE);
522 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), scrolled_win, NULL);
523 gtk_notebook_append_page (GTK_NOTEBOOK (priv->list_notebook), priv->empty_list, NULL);
526 grid = gtk_grid_new ();
528 gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
529 gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
531 gtk_grid_attach (GTK_GRID (grid), priv->search_entry, 0, 0, 2, 1);
532 gtk_grid_attach (GTK_GRID (grid), priv->list_notebook, 0, 1, 2, 1);
533 gtk_grid_attach (GTK_GRID (grid), priv->preview, 0, 2, 2, 1);
535 gtk_grid_attach (GTK_GRID (grid), priv->size_slider, 0, 3, 1, 1);
536 gtk_grid_attach (GTK_GRID (grid), priv->size_spin, 1, 3, 1, 1);
538 gtk_widget_set_hexpand (GTK_WIDGET (scrolled_win), TRUE);
539 gtk_widget_set_vexpand (GTK_WIDGET (scrolled_win), TRUE);
540 gtk_widget_set_hexpand (GTK_WIDGET (priv->search_entry), TRUE);
542 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_slider), TRUE);
543 gtk_widget_set_hexpand (GTK_WIDGET (priv->size_spin), FALSE);
545 gtk_box_pack_start (GTK_BOX (fontchooser), grid, TRUE, TRUE, 0);
547 gtk_widget_show_all (GTK_WIDGET (fontchooser));
548 gtk_widget_hide (GTK_WIDGET (fontchooser));
550 /* Treeview column and model bootstrapping */
551 gtk_font_chooser_widget_bootstrap_fontlist (fontchooser);
553 /* Set default preview text */
554 gtk_entry_set_text (GTK_ENTRY (priv->preview),
555 pango_language_get_sample_string (NULL));
557 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
559 /* Callback connections */
560 g_signal_connect (priv->search_entry, "notify::text",
561 G_CALLBACK (text_changed_cb), fontchooser);
563 g_signal_connect (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
564 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
565 g_signal_connect (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
566 "value-changed", G_CALLBACK (size_change_cb), fontchooser);
568 g_signal_connect (priv->family_face_list, "cursor-changed",
569 G_CALLBACK (cursor_changed_cb), fontchooser);
570 g_signal_connect (priv->family_face_list, "row-activated",
571 G_CALLBACK (row_activated_cb), fontchooser);
573 /* Zoom on preview scroll */
574 g_signal_connect (priv->preview, "scroll-event",
575 G_CALLBACK (zoom_preview_cb), fontchooser);
577 g_signal_connect (priv->size_slider, "scroll-event",
578 G_CALLBACK (zoom_preview_cb), fontchooser);
580 /* Font list empty hides the scrolledwindow */
581 g_signal_connect (G_OBJECT (priv->filter_model), "row-deleted",
582 G_CALLBACK (row_deleted_cb), fontchooser);
583 g_signal_connect (G_OBJECT (priv->filter_model), "row-inserted",
584 G_CALLBACK (row_inserted_cb), fontchooser);
586 /* Set default focus */
587 gtk_widget_pop_composite_child ();
589 gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
593 * gtk_font_chooser_widget_new:
595 * Creates a new #GtkFontChooserWidget.
597 * Return value: a new #GtkFontChooserWidget
602 gtk_font_chooser_widget_new (void)
604 return g_object_new (GTK_TYPE_FONT_CHOOSER_WIDGET, NULL);
608 cmp_families (const void *a,
611 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
612 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
614 return g_utf8_collate (a_name, b_name);
618 gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser)
620 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
621 GtkListStore *list_store;
623 PangoFontFamily **families;
624 gchar *family_and_face;
626 list_store = GTK_LIST_STORE (priv->model);
628 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
632 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
634 gtk_list_store_clear (list_store);
636 /* Iterate over families and faces */
637 for (i = 0; i < n_families; i++)
640 PangoFontFace **faces;
642 const gchar *fam_name = pango_font_family_get_name (families[i]);
644 pango_font_family_list_faces (families[i], &faces, &n_faces);
646 for (j = 0; j < n_faces; j++)
648 const gchar *face_name;
650 face_name = pango_font_face_get_face_name (faces[j]);
652 family_and_face = g_strconcat (fam_name, " ", face_name, NULL);
654 gtk_list_store_insert_with_values (list_store, &iter, -1,
655 FAMILY_COLUMN, families[i],
656 FACE_COLUMN, faces[j],
657 PREVIEW_TITLE_COLUMN, family_and_face,
660 g_free (family_and_face);
668 /* now make sure the font list looks right */
669 if (!gtk_font_chooser_widget_find_font (fontchooser,
672 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
674 gtk_font_chooser_widget_ensure_selection (fontchooser);
678 visible_func (GtkTreeModel *model,
682 GtkFontChooserWidgetPrivate *priv = user_data;
683 gboolean result = TRUE;
684 const gchar *search_text;
686 gchar *font_name, *font_name_casefold;
689 if (priv->filter_func != NULL)
691 PangoFontFamily *family;
694 gtk_tree_model_get (model, iter,
695 FAMILY_COLUMN, &family,
699 result = priv->filter_func (family, face, priv->filter_data);
701 g_object_unref (family);
702 g_object_unref (face);
708 /* If there's no filter string we show the item */
709 search_text = gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
710 if (strlen (search_text) == 0)
713 gtk_tree_model_get (model, iter,
714 PREVIEW_TITLE_COLUMN, &font_name,
717 if (font_name == NULL)
720 split_terms = g_strsplit (search_text, " ", 0);
721 font_name_casefold = g_utf8_casefold (font_name, -1);
723 for (i = 0; split_terms[i] && result; i++)
725 gchar* term_casefold = g_utf8_casefold (split_terms[i], -1);
727 if (!strstr (font_name_casefold, term_casefold))
730 g_free (term_casefold);
733 g_free (font_name_casefold);
735 g_strfreev (split_terms);
742 gtk_font_chooser_widget_get_preview_text_height (GtkFontChooserWidget *fontchooser)
744 GtkWidget *treeview = fontchooser->priv->family_face_list;
745 double dpi, font_size;
747 dpi = gdk_screen_get_resolution (gtk_widget_get_screen (treeview));
748 gtk_style_context_get (gtk_widget_get_style_context (treeview),
749 gtk_widget_get_state_flags (treeview),
750 "font-size", &font_size,
753 return (dpi < 0.0 ? 96.0 : dpi) / 72.0 * PANGO_SCALE_X_LARGE * font_size * PANGO_SCALE;
756 static PangoAttrList *
757 gtk_font_chooser_widget_get_preview_attributes (GtkFontChooserWidget *fontchooser,
758 const PangoFontDescription *font_desc,
759 gsize first_line_len)
761 PangoAttribute *attribute;
762 PangoAttrList *attrs;
764 attrs = pango_attr_list_new ();
766 attribute = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
767 attribute->end_index = first_line_len;
768 pango_attr_list_insert (attrs, attribute);
770 attribute = pango_attr_scale_new (PANGO_SCALE_SMALL);
771 attribute->end_index = first_line_len;
772 pango_attr_list_insert (attrs, attribute);
776 attribute = pango_attr_font_desc_new (font_desc);
777 attribute->start_index = first_line_len;
778 pango_attr_list_insert (attrs, attribute);
781 attribute = pango_attr_fallback_new (FALSE);
782 attribute->start_index = first_line_len;
783 pango_attr_list_insert (attrs, attribute);
785 attribute = pango_attr_size_new_absolute (gtk_font_chooser_widget_get_preview_text_height (fontchooser));
786 attribute->start_index = first_line_len;
787 pango_attr_list_insert (attrs, attribute);
793 gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
794 GtkCellRenderer *cell,
795 GtkTreeModel *tree_model,
799 GtkFontChooserWidget *fontchooser = user_data;
800 PangoFontDescription *font_desc;
801 PangoAttrList *attrs;
802 char *to_string, *text;
803 gsize first_line_len;
805 font_desc = tree_model_get_font_description (tree_model, iter);
807 to_string = pango_font_description_to_string (font_desc);
809 text = g_strconcat (to_string, "\n", fontchooser->priv->preview_text, NULL);
810 first_line_len = strlen (to_string) + 1;
812 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
821 pango_font_description_free (font_desc);
822 pango_attr_list_unref (attrs);
828 gtk_font_chooser_widget_set_cell_size (GtkFontChooserWidget *fontchooser)
830 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
831 PangoAttrList *attrs;
834 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, -1, -1);
836 attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
840 g_object_set (priv->family_face_cell,
845 pango_attr_list_unref (attrs);
847 gtk_cell_renderer_get_preferred_size (priv->family_face_cell,
848 priv->family_face_list,
851 gtk_cell_renderer_set_fixed_size (priv->family_face_cell, size.width, size.height);
855 gtk_font_chooser_widget_bootstrap_fontlist (GtkFontChooserWidget *fontchooser)
857 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
858 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
859 GtkTreeViewColumn *col;
861 g_signal_connect_data (priv->family_face_list,
863 G_CALLBACK (gtk_font_chooser_widget_set_cell_size),
866 G_CONNECT_AFTER | G_CONNECT_SWAPPED);
868 priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
869 PANGO_TYPE_FONT_FAMILY,
870 PANGO_TYPE_FONT_FACE,
871 PANGO_TYPE_FONT_DESCRIPTION,
874 priv->filter_model = gtk_tree_model_filter_new (priv->model, NULL);
875 g_object_unref (priv->model);
877 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->filter_model),
878 visible_func, (gpointer)priv, NULL);
880 gtk_tree_view_set_model (treeview, priv->filter_model);
881 g_object_unref (priv->filter_model);
883 gtk_tree_view_set_rules_hint (treeview, TRUE);
884 gtk_tree_view_set_headers_visible (treeview, FALSE);
885 gtk_tree_view_set_fixed_height_mode (treeview, TRUE);
887 priv->family_face_cell = gtk_cell_renderer_text_new ();
888 g_object_set (priv->family_face_cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
890 col = gtk_tree_view_column_new ();
891 gtk_tree_view_column_set_title (col, _("Font Family"));
892 gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_FIXED);
893 gtk_tree_view_column_pack_start (col, priv->family_face_cell, TRUE);
894 gtk_tree_view_column_set_cell_data_func (col,
895 priv->family_face_cell,
896 gtk_font_chooser_widget_cell_data_func,
900 gtk_tree_view_append_column (treeview, col);
902 gtk_font_chooser_widget_load_fonts (fontchooser);
904 gtk_font_chooser_widget_set_cell_size (fontchooser);
908 gtk_font_chooser_widget_finalize (GObject *object)
910 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (object);
911 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
914 pango_font_description_free (priv->font_desc);
916 if (priv->filter_data_destroy)
917 priv->filter_data_destroy (priv->filter_data);
919 G_OBJECT_CLASS (gtk_font_chooser_widget_parent_class)->finalize (object);
923 my_pango_font_family_equal (const char *familya,
926 return g_ascii_strcasecmp (familya, familyb) == 0;
930 gtk_font_chooser_widget_find_font (GtkFontChooserWidget *fontchooser,
931 const PangoFontDescription *font_desc,
935 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
936 PangoFontDescription *desc;
937 PangoFontFamily *family;
940 if (pango_font_description_get_family (font_desc) == NULL)
943 for (valid = gtk_tree_model_get_iter_first (priv->model, iter);
945 valid = gtk_tree_model_iter_next (priv->model, iter))
947 gtk_tree_model_get (priv->model, iter,
948 FAMILY_COLUMN, &family,
951 if (!my_pango_font_family_equal (pango_font_description_get_family (font_desc),
952 pango_font_family_get_name (family)))
955 desc = tree_model_get_font_description (priv->model, iter);
957 pango_font_description_merge_static (desc, font_desc, FALSE);
958 if (pango_font_description_equal (desc, font_desc))
961 pango_font_description_free (desc);
968 gtk_font_chooser_widget_screen_changed (GtkWidget *widget,
969 GdkScreen *previous_screen)
971 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (widget);
973 if (GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed)
974 GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->screen_changed (widget, previous_screen);
976 if (previous_screen == NULL)
977 previous_screen = gdk_screen_get_default ();
979 if (previous_screen == gtk_widget_get_screen (widget))
982 gtk_font_chooser_widget_load_fonts (fontchooser);
985 static PangoFontFamily *
986 gtk_font_chooser_widget_get_family (GtkFontChooser *chooser)
988 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
989 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
990 PangoFontFamily *family;
992 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
995 gtk_tree_model_get (priv->model, &priv->font_iter,
996 FAMILY_COLUMN, &family,
998 g_object_unref (family);
1003 static PangoFontFace *
1004 gtk_font_chooser_widget_get_face (GtkFontChooser *chooser)
1006 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1007 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1008 PangoFontFace *face;
1010 if (!gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter))
1013 gtk_tree_model_get (priv->model, &priv->font_iter,
1016 g_object_unref (face);
1022 gtk_font_chooser_widget_get_size (GtkFontChooser *chooser)
1024 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1026 return pango_font_description_get_size (fontchooser->priv->font_desc);
1030 gtk_font_chooser_widget_get_font (GtkFontChooserWidget *fontchooser)
1032 return pango_font_description_to_string (fontchooser->priv->font_desc);
1035 static PangoFontDescription *
1036 gtk_font_chooser_widget_get_font_desc (GtkFontChooserWidget *fontchooser)
1038 return fontchooser->priv->font_desc;
1042 gtk_font_chooser_widget_set_font (GtkFontChooserWidget *fontchooser,
1043 const gchar *fontname)
1045 PangoFontDescription *font_desc;
1047 font_desc = pango_font_description_from_string (fontname);
1048 gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
1052 gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
1054 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1055 GtkTreeSelection *selection;
1056 GtkTreeIter filter_iter;
1058 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->family_face_list));
1060 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (priv->model), &priv->font_iter) &&
1061 gtk_tree_model_filter_convert_child_iter_to_iter (GTK_TREE_MODEL_FILTER (priv->filter_model),
1065 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter_model),
1068 gtk_tree_selection_select_iter (selection, &filter_iter);
1069 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1070 path, NULL, FALSE, 0.0, 0.0);
1071 gtk_tree_path_free (path);
1075 gtk_tree_selection_unselect_all (selection);
1080 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser,
1081 PangoFontDescription *font_desc,
1084 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1087 g_assert (font_desc != NULL);
1088 /* iter may be NULL if the font doesn't exist on the list */
1090 mask = pango_font_description_get_set_fields (font_desc);
1092 /* sucky test, because we can't restrict the comparison to
1093 * only the parts that actually do get merged */
1094 if (pango_font_description_equal (font_desc, priv->font_desc))
1096 pango_font_description_free (font_desc);
1100 pango_font_description_merge (priv->font_desc, font_desc, TRUE);
1102 if (mask & PANGO_FONT_MASK_SIZE)
1104 double font_size = (double) pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
1105 /* XXX: This clamps, which can cause it to reloop into here, do we need
1106 * to block its signal handler? */
1107 gtk_range_set_value (GTK_RANGE (priv->size_slider), font_size);
1108 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin), font_size);
1110 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1111 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1113 if (&priv->font_iter != iter)
1116 memset (&priv->font_iter, 0, sizeof (GtkTreeIter));
1118 memcpy (&priv->font_iter, iter, sizeof (GtkTreeIter));
1120 gtk_font_chooser_widget_ensure_selection (fontchooser);
1123 gtk_font_chooser_widget_update_marks (fontchooser);
1126 gtk_widget_override_font (priv->preview, priv->font_desc);
1128 pango_font_description_free (font_desc); /* adopted */
1130 g_object_notify (G_OBJECT (fontchooser), "font");
1131 g_object_notify (G_OBJECT (fontchooser), "font-desc");
1135 gtk_font_chooser_widget_take_font_desc (GtkFontChooserWidget *fontchooser,
1136 PangoFontDescription *font_desc)
1138 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1141 if (font_desc == NULL)
1142 font_desc = pango_font_description_from_string (GTK_FONT_CHOOSER_DEFAULT_FONT_NAME);
1144 mask = pango_font_description_get_set_fields (font_desc);
1145 if (mask & (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT |
1146 PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH))
1150 if (gtk_font_chooser_widget_find_font (fontchooser,
1154 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1160 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1167 gtk_font_chooser_widget_merge_font_desc (fontchooser,
1175 gtk_font_chooser_widget_get_preview_text (GtkFontChooserWidget *fontchooser)
1177 return fontchooser->priv->preview_text;
1181 gtk_font_chooser_widget_set_preview_text (GtkFontChooserWidget *fontchooser,
1184 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1186 g_free (priv->preview_text);
1187 priv->preview_text = g_strdup (text);
1189 gtk_entry_set_text (GTK_ENTRY (priv->preview), text);
1191 g_object_notify (G_OBJECT (fontchooser), "preview-text");
1193 /* XXX: There's no API to tell the treeview that a column has changed,
1195 gtk_widget_queue_draw (priv->family_face_list);
1199 gtk_font_chooser_widget_get_show_preview_entry (GtkFontChooserWidget *fontchooser)
1201 return fontchooser->priv->show_preview_entry;
1205 gtk_font_chooser_widget_set_show_preview_entry (GtkFontChooserWidget *fontchooser,
1206 gboolean show_preview_entry)
1208 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1210 if (priv->show_preview_entry != show_preview_entry)
1212 fontchooser->priv->show_preview_entry = show_preview_entry;
1214 if (show_preview_entry)
1215 gtk_widget_show (fontchooser->priv->preview);
1217 gtk_widget_hide (fontchooser->priv->preview);
1219 g_object_notify (G_OBJECT (fontchooser), "show-preview-entry");
1224 gtk_font_chooser_widget_set_filter_func (GtkFontChooser *chooser,
1225 GtkFontFilterFunc filter,
1227 GDestroyNotify destroy)
1229 GtkFontChooserWidget *fontchooser = GTK_FONT_CHOOSER_WIDGET (chooser);
1230 GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
1232 if (priv->filter_data_destroy)
1233 priv->filter_data_destroy (priv->filter_data);
1235 priv->filter_func = filter;
1236 priv->filter_data = data;
1237 priv->filter_data_destroy = destroy;
1239 gtk_font_chooser_widget_refilter_font_list (fontchooser);
1243 gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
1245 iface->get_font_family = gtk_font_chooser_widget_get_family;
1246 iface->get_font_face = gtk_font_chooser_widget_get_face;
1247 iface->get_font_size = gtk_font_chooser_widget_get_size;
1248 iface->set_filter_func = gtk_font_chooser_widget_set_filter_func;