1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Massively updated for Pango by Owen Taylor, May 2000
5 * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
6 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
7 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include <glib/gprintf.h>
40 #include "gtkfontsel.h"
41 #include "gtkbutton.h"
42 #include "gtkcellrenderertext.h"
48 #include "gtkliststore.h"
52 #include "gtktreeselection.h"
53 #include "gtktreeview.h"
55 #include "gtkscrolledwindow.h"
57 #include "gtkaccessible.h"
58 #include "gtkbuildable.h"
59 #include "gtkprivate.h"
60 #include "gtkalignment.h"
63 #include "gtkspinbutton.h"
64 #include "gtkwidget.h"
68 * @Short_description: A widget for selecting fonts
69 * @Title: GtkFontSelection
70 * @See_also: #GtkFontSelectionDialog
72 * The #GtkFontSelection widget lists the available fonts, styles and sizes,
73 * allowing the user to select a font.
74 * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
77 * To set the font which is initially selected, use
78 * gtk_font_selection_set_font_name().
80 * To get the selected font use gtk_font_selection_get_font_name().
82 * To change the text which is shown in the preview area, use
83 * gtk_font_selection_set_preview_text().
87 struct _GtkFontSelectionPrivate
89 GtkWidget *search_entry;
90 GtkWidget *family_face_list;
91 GtkWidget *size_slider;
100 PangoFontFamily *family;
102 gboolean ignore_slider;
106 struct _GtkFontSelectionDialogPrivate
110 GtkWidget *ok_button;
111 GtkWidget *apply_button;
112 GtkWidget *cancel_button;
116 #define DEFAULT_FONT_NAME "Sans 10"
117 #define MAX_FONT_SIZE 999
119 /* This is the initial fixed height and the top padding of the preview entry */
120 #define PREVIEW_HEIGHT 84
121 #define PREVIEW_TOP_PADDING 6
123 /* These are the sizes of the font, style & size lists. */
124 #define FONT_LIST_HEIGHT 136
125 #define FONT_LIST_WIDTH 190
126 #define FONT_STYLE_LIST_WIDTH 170
127 #define FONT_SIZE_LIST_WIDTH 60
129 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\" foreground=\"%s\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
131 /* These are what we use as the standard font sizes, for the size list.
133 #define FONT_SIZES_LENGTH 14
134 static const gint font_sizes[] = {
135 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
149 PREVIEW_TITLE_COLUMN,
150 /*FIXME: Remove two strings after deprecation removal */
155 static void gtk_font_selection_set_property (GObject *object,
159 static void gtk_font_selection_get_property (GObject *object,
163 static void gtk_font_selection_finalize (GObject *object);
166 static void gtk_font_selection_screen_changed (GtkWidget *widget,
167 GdkScreen *previous_screen);
168 static void gtk_font_selection_style_updated (GtkWidget *widget);
171 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
172 PangoFontFamily *family);
173 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
174 PangoFontFace *face);
176 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
178 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
181 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
186 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
187 widget_class->screen_changed = gtk_font_selection_screen_changed;
188 widget_class->style_updated = gtk_font_selection_style_updated;
191 gobject_class->finalize = gtk_font_selection_finalize;
192 gobject_class->set_property = gtk_font_selection_set_property;
193 gobject_class->get_property = gtk_font_selection_get_property;
195 g_object_class_install_property (gobject_class,
197 g_param_spec_string ("font-name",
199 P_("The string that represents this font"),
201 GTK_PARAM_READWRITE));
202 g_object_class_install_property (gobject_class,
204 g_param_spec_string ("preview-text",
206 P_("The text to display in order to demonstrate the selected font"),
207 pango_language_get_sample_string (NULL),
208 GTK_PARAM_READWRITE));
210 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
214 gtk_font_selection_set_property (GObject *object,
219 GtkFontSelection *fontsel;
221 fontsel = GTK_FONT_SELECTION (object);
226 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
228 case PROP_PREVIEW_TEXT:
229 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gtk_font_selection_get_property (GObject *object,
243 GtkFontSelection *fontsel;
245 fontsel = GTK_FONT_SELECTION (object);
250 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
252 case PROP_PREVIEW_TEXT:
253 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 refilter_and_focus (GtkFontSelectionPrivate *priv)
265 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
266 GtkTreePath *path = gtk_tree_path_new ();
268 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
273 gtk_tree_view_get_cursor (treeview, &path, NULL);
275 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter), &iter, path))
277 gtk_tree_path_free (path);
281 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
282 gtk_tree_path_free (path);
286 deleted_text_cb (GtkEntryBuffer *buffer,
291 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
292 GtkWidget *entry = priv->search_entry;
294 if (gtk_entry_buffer_get_length (buffer) == 0)
296 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
297 GTK_ENTRY_ICON_SECONDARY,
301 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
305 inserted_text_cb (GtkEntryBuffer *buffer,
311 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
312 GtkWidget *entry = priv->search_entry;
314 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
316 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
317 GTK_ENTRY_ICON_SECONDARY,
321 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
325 icon_press_cb (GtkEntry *entry,
326 GtkEntryIconPosition pos,
330 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
334 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
336 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
338 /* If we set the silder value manually, we ignore this callback */
339 if (priv->ignore_slider)
341 priv->ignore_slider = FALSE;
345 gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
346 gtk_adjustment_get_value (adjustment));
350 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
352 PangoFontDescription *desc;
353 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
355 gdouble size = gtk_adjustment_get_value (adjustment);
357 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
359 /* We ignore the slider value change callback for both of this set_value call */
360 priv->ignore_slider = TRUE;
361 if (size < gtk_adjustment_get_lower (slider_adj))
362 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
363 else if (size > gtk_adjustment_get_upper (slider_adj))
364 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
366 gtk_adjustment_set_value (slider_adj, size);
368 priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
370 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
371 pango_font_description_set_size (desc, priv->size);
372 gtk_widget_override_font (priv->preview, desc);
374 gtk_widget_queue_draw (priv->preview);
378 set_range_marks (GtkFontSelectionPrivate *priv,
379 GtkWidget* size_slider,
389 sizes = (gint*)font_sizes;
390 length = FONT_SIZES_LENGTH;
393 gtk_scale_clear_marks (GTK_SCALE (size_slider));
395 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
397 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
398 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
400 value = gtk_adjustment_get_value (adj);
401 if (value > (gdouble) sizes[length-1])
403 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
404 priv->ignore_slider = TRUE;
406 else if (value < (gdouble) sizes[0])
408 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
409 priv->ignore_slider = TRUE;
412 for (i=0; i<length; i++)
413 gtk_scale_add_mark (GTK_SCALE (size_slider),
415 GTK_POS_BOTTOM, NULL);
420 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
422 PangoFontFamily *family;
424 PangoFontDescription *desc;
430 GtkTreePath *path = gtk_tree_path_new ();
432 GtkFontSelection *fontsel = (GtkFontSelection*)data;
434 gtk_tree_view_get_cursor (treeview, &path, NULL);
439 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->filter), &iter, path))
441 gtk_tree_path_free (path);
446 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->filter), &iter,
448 FAMILY_COLUMN, &family,
451 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
453 gtk_tree_path_free (path);
456 if (!face || !family)
458 g_object_unref (face);
459 g_object_unref (family);
463 desc = pango_font_face_describe (face);
464 pango_font_description_set_size (desc, fontsel->priv->size);
465 gtk_widget_override_font (fontsel->priv->preview, desc);
467 pango_font_face_list_sizes (face, &sizes, &n_sizes);
468 /* It seems not many fonts actually have a sane set of sizes */
469 for (i=0; i<n_sizes; i++)
470 sizes[i] = sizes[i] / PANGO_SCALE;
472 set_range_marks (fontsel->priv, fontsel->priv->size_slider, sizes, n_sizes);
474 gtk_font_selection_ref_family (fontsel, family);
475 gtk_font_selection_ref_face (fontsel, face);
478 g_object_unref ((gpointer)face);
479 pango_font_description_free(desc);
483 zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
485 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
487 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
489 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
490 gtk_adjustment_set_value (adj,
491 gtk_adjustment_get_value (adj) +
492 gtk_adjustment_get_step_increment (adj));
493 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
494 gtk_adjustment_set_value (adj,
495 gtk_adjustment_get_value (adj) -
496 gtk_adjustment_get_step_increment (adj));
501 gtk_font_selection_init (GtkFontSelection *fontsel)
503 GtkFontSelectionPrivate *priv;
504 PangoFontDescription *font_desc;
505 GtkWidget *scrolled_win;
506 GtkWidget *alignment;
507 GtkWidget *preview_and_size;
508 GtkWidget *size_controls;
510 GList *focus_chain = NULL;
514 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
515 GTK_TYPE_FONT_SELECTION,
516 GtkFontSelectionPrivate);
517 priv = fontsel->priv;
518 gtk_widget_push_composite_child ();
520 /* Creating fundamental widgets for the private struct */
521 priv->search_entry = gtk_entry_new ();
522 priv->family_face_list = gtk_tree_view_new ();
523 priv->preview = gtk_entry_new ();
524 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
525 (gdouble) font_sizes[0],
526 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
529 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
531 /** Bootstrapping widget layout **/
532 gtk_box_set_spacing (GTK_BOX (fontsel), 6);
533 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
535 /* Main font family/face view */
536 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
537 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
538 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
539 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
540 GTK_SHADOW_ETCHED_IN);
541 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
543 /* Alignment for the preview and size controls */
544 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
545 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
546 PREVIEW_TOP_PADDING, 0, 0, 0);
547 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
549 preview_and_size = gtk_vbox_new (TRUE, 0);
550 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
552 /* The preview entry needs a scrolled window to make sure we have a */
553 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
554 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
555 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
556 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
557 GTK_SHADOW_ETCHED_IN);
558 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
560 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
562 /* Setting the size requests for various widgets */
563 gtk_widget_set_size_request (GTK_WIDGET (fontsel), 462, 462);
564 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
565 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
567 /* Unset the frame on the preview entry */
568 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
570 /* Packing the slider and the spin in a hbox */
571 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
572 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
573 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
574 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
576 gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
578 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
579 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
581 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
583 /* Getting the default size */
584 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
585 priv->size = pango_font_description_get_size (font_desc);
589 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
590 (gdouble)(priv->size / PANGO_SCALE));
591 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
592 (gdouble)(priv->size / PANGO_SCALE));
594 gtk_widget_show_all (GTK_WIDGET (fontsel));
595 gtk_widget_hide (GTK_WIDGET (fontsel));
597 /* Treeview column and model bootstrapping */
598 gtk_font_selection_bootstrap_fontlist (fontsel);
600 /* Set default preview text */
601 gtk_entry_set_text (GTK_ENTRY (priv->preview),
602 pango_language_get_sample_string (NULL));
604 /* Set search icon and place holder text */
605 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
606 GTK_ENTRY_ICON_SECONDARY,
608 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
610 /** Callback connections **/
611 /* Connect to callback for the live search text entry */
612 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
613 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
614 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
615 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
616 g_signal_connect (G_OBJECT (priv->search_entry),
617 "icon-press", G_CALLBACK (icon_press_cb), priv);
619 /* Size controls callbacks */
620 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
621 "value-changed", G_CALLBACK (slider_change_cb), priv);
622 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
623 "value-changed", G_CALLBACK (spin_change_cb), priv);
624 priv->ignore_slider = FALSE;
626 /* Font selection callback */
627 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
628 G_CALLBACK (cursor_changed_cb), fontsel);
630 /* Zoom on preview scroll*/
631 g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
632 G_CALLBACK (zoom_preview_cb), priv);
634 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
635 G_CALLBACK (zoom_preview_cb), priv);
637 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
639 /* Set default focus */
640 gtk_widget_pop_composite_child();
644 * gtk_font_selection_new:
646 * Creates a new #GtkFontSelection.
648 * Return value: a new #GtkFontSelection
651 gtk_font_selection_new (void)
653 GtkFontSelection *fontsel;
655 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
657 return GTK_WIDGET (fontsel);
661 cmp_families (const void *a, const void *b)
663 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
664 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
666 return g_utf8_collate (a_name, b_name);
670 populate_list (GtkTreeView* treeview, GtkListStore* model)
672 GtkStyleContext *style_context;
677 GtkTreeIter match_row;
681 PangoFontFamily **families;
683 GString *tmp = g_string_new (NULL);
684 GString *family_and_face = g_string_new (NULL);
686 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
690 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
692 gtk_list_store_clear (model);
694 /* Get row header font color */
695 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
696 gtk_style_context_get_color (style_context,
697 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
700 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
701 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
702 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
703 color_string = pango_color_to_string (&p_color);
705 /* Iterate over families and faces */
706 for (i=0; i<n_families; i++)
709 PangoFontFace **faces;
712 const gchar *fam_name = pango_font_family_get_name (families[i]);
714 pango_font_family_list_faces (families[i], &faces, &n_faces);
716 for (j=0; j<n_faces; j++)
718 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
719 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
720 gchar *font_desc = pango_font_description_to_string (pango_desc);
722 /* foreground_color, family_name, face_name, desc, sample string */
723 g_string_printf (family_and_face, "%s %s",
727 g_string_printf (tmp, ROW_FORMAT_STRING,
729 family_and_face->str,
731 pango_language_get_sample_string (NULL));
733 gtk_list_store_append (model, &iter);
734 gtk_list_store_set (model, &iter,
735 FAMILY_COLUMN, families[i],
736 FACE_COLUMN, faces[j],
737 PREVIEW_TITLE_COLUMN, family_and_face->str,
738 PREVIEW_TEXT_COLUMN, tmp->str,
739 /** FIXME: FAMILY_NAME_COLUMN and FACE_NAME_COLUMN
740 are needed only until we remove the deprecated
742 FAMILY_NAME_COLUMN, fam_name,
743 FACE_NAME_COLUMN, face_name,
746 if ((i == 0 && j == 0) ||
747 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
752 pango_font_description_free(pango_desc);
759 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
763 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
764 gtk_tree_path_free(path);
768 g_string_free (family_and_face, TRUE);
769 g_string_free (tmp, TRUE);
770 g_free (color_string);
775 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
777 gboolean result = FALSE;
778 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
780 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
782 gchar *font_name_casefold;
783 gchar *search_text_casefold;
785 gtk_tree_model_get (model, iter,
786 FAMILY_NAME_COLUMN, &font_name,
789 /* Covering some corner cases to speed up the result */
790 if (font_name == NULL ||
791 strlen (search_text) > strlen (font_name))
796 if (strlen (search_text) == 0)
802 font_name_casefold = g_utf8_casefold (font_name, -1);
803 search_text_casefold = g_utf8_casefold (search_text, -1);
805 if (g_strrstr (font_name_casefold, search_text_casefold))
808 g_free (search_text_casefold);
809 g_free (font_name_casefold);
815 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
817 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
818 GtkCellRenderer *cell;
819 GtkTreeViewColumn *col;
821 fontsel->priv->model = gtk_list_store_new (6,
822 PANGO_TYPE_FONT_FAMILY,
823 PANGO_TYPE_FONT_FACE,
826 /*FIXME: Remove two strings after deprecation removal */
830 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
832 g_object_unref (fontsel->priv->model);
834 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
836 (gpointer)fontsel->priv,
839 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
840 g_object_unref (fontsel->priv->filter);
842 gtk_tree_view_set_rules_hint (treeview, TRUE);
843 gtk_tree_view_set_headers_visible (treeview, FALSE);
845 cell = gtk_cell_renderer_text_new ();
846 col = gtk_tree_view_column_new_with_attributes ("Family",
848 "markup", PREVIEW_TEXT_COLUMN,
852 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
854 gtk_tree_view_append_column (treeview, col);
856 populate_list (treeview, fontsel->priv->model);
861 gtk_font_selection_finalize (GObject *object)
863 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
865 gtk_font_selection_ref_family (fontsel, NULL);
866 gtk_font_selection_ref_face (fontsel, NULL);
868 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
873 gtk_font_selection_screen_changed (GtkWidget *widget,
874 GdkScreen *previous_screen)
880 gtk_font_selection_style_updated (GtkWidget *widget)
882 /*GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);*/
888 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
889 PangoFontFamily *family)
891 GtkFontSelectionPrivate *priv = fontsel->priv;
894 family = g_object_ref (family);
896 g_object_unref (priv->family);
897 priv->family = family;
901 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
904 GtkFontSelectionPrivate *priv = fontsel->priv;
907 face = g_object_ref (face);
909 g_object_unref (priv->face);
913 /*****************************************************************************
914 * These functions are the main public interface for getting/setting the font.
915 *****************************************************************************/
918 * gtk_font_selection_get_family_list:
919 * @fontsel: a #GtkFontSelection
921 * This returns the #GtkTreeView that lists font families, for
922 * example, 'Sans', 'Serif', etc.
924 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
929 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
931 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
937 * gtk_font_selection_get_face_list:
938 * @fontsel: a #GtkFontSelection
940 * This returns the #GtkTreeView which lists all styles available for
941 * the selected font. For example, 'Regular', 'Bold', etc.
943 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
948 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
950 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
956 * gtk_font_selection_get_size_entry:
957 * @fontsel: a #GtkFontSelection
959 * This returns the #GtkEntry used to allow the user to edit the font
960 * number manually instead of selecting it from the list of font sizes.
962 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
967 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
969 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
975 * gtk_font_selection_get_size_list:
976 * @fontsel: a #GtkFontSelection
978 * This returns the #GtkTreeeView used to list font sizes.
980 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
985 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
987 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
993 * gtk_font_selection_get_preview_entry:
994 * @fontsel: a #GtkFontSelection
996 * This returns the #GtkEntry used to display the font as a preview.
998 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1003 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1005 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1011 * gtk_font_selection_get_family:
1012 * @fontsel: a #GtkFontSelection
1014 * Gets the #PangoFontFamily representing the selected font family.
1016 * Return value: (transfer none): A #PangoFontFamily representing the
1017 * selected font family. Font families are a collection of font
1018 * faces. The returned object is owned by @fontsel and must not
1019 * be modified or freed.
1024 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1026 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1032 * gtk_font_selection_get_face:
1033 * @fontsel: a #GtkFontSelection
1035 * Gets the #PangoFontFace representing the selected font group
1036 * details (i.e. family, slant, weight, width, etc).
1038 * Return value: (transfer none): A #PangoFontFace representing the
1039 * selected font group details. The returned object is owned by
1040 * @fontsel and must not be modified or freed.
1045 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1047 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1053 * gtk_font_selection_get_size:
1054 * @fontsel: a #GtkFontSelection
1056 * The selected font size.
1058 * Return value: A n integer representing the selected font size,
1059 * or -1 if no font size is selected.
1064 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1066 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1068 return fontsel->priv->size;
1072 * gtk_font_selection_get_font_name:
1073 * @fontsel: a #GtkFontSelection
1075 * Gets the currently-selected font name.
1077 * Note that this can be a different string than what you set with
1078 * gtk_font_selection_set_font_name(), as the font selection widget may
1079 * normalize font names and thus return a string with a different structure.
1080 * For example, "Helvetica Italic Bold 12" could be normalized to
1081 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1082 * if you want to compare two font descriptions.
1084 * Return value: (transfer full) (allow-none): A string with the name of the
1085 * current font, or %NULL if no font is selected. You must free this
1086 * string with g_free().
1089 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1091 if (!fontsel->priv->family)
1094 return g_strdup (pango_font_family_get_name (fontsel->priv->family));
1097 /* This sets the current font, then selecting the appropriate list rows. */
1100 * gtk_font_selection_set_font_name:
1101 * @fontsel: a #GtkFontSelection
1102 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1104 * Sets the currently-selected font.
1106 * Note that the @fontsel needs to know the screen in which it will appear
1107 * for this to work; this can be guaranteed by simply making sure that the
1108 * @fontsel is inserted in a toplevel window before you call this function.
1110 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1111 * such font exists or if the @fontsel doesn't belong to a particular
1115 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1116 const gchar *fontname)
1119 PangoFontFamily *family = NULL;
1120 PangoFontFace *face = NULL;
1121 PangoFontDescription *new_desc;
1124 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1130 * gtk_font_selection_get_preview_text:
1131 * @fontsel: a #GtkFontSelection
1133 * Gets the text displayed in the preview area.
1135 * Return value: the text displayed in the preview area.
1136 * This string is owned by the widget and should not be
1139 G_CONST_RETURN gchar*
1140 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1147 * gtk_font_selection_set_preview_text:
1148 * @fontsel: a #GtkFontSelection
1149 * @text: the text to display in the preview area
1151 * Sets the text displayed in the preview area.
1152 * The @text is used to show how the selected font looks.
1155 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1159 GtkFontSelectionPrivate *priv;
1161 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1162 g_return_if_fail (text != NULL);
1164 priv = fontsel->priv;
1170 * SECTION:gtkfontseldlg
1171 * @Short_description: A dialog box for selecting fonts
1172 * @Title: GtkFontSelectionDialog
1173 * @See_also: #GtkFontSelection, #GtkDialog
1175 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1177 * To set the font which is initially selected, use
1178 * gtk_font_selection_dialog_set_font_name().
1180 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1182 * To change the text which is shown in the preview area, use
1183 * gtk_font_selection_dialog_set_preview_text().
1185 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1186 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1187 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1188 * exposes the embedded #GtkFontSelection as internal child with the
1189 * name "font_selection". It also exposes the buttons with the names
1190 * "ok_button", "cancel_button" and "apply_button".
1194 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1195 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1196 GtkBuilder *builder,
1197 const gchar *childname);
1199 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1201 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1202 gtk_font_selection_dialog_buildable_interface_init))
1204 static GtkBuildableIface *parent_buildable_iface;
1207 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1209 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1213 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1215 GtkFontSelectionDialogPrivate *priv;
1216 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1217 GtkWidget *action_area, *content_area;
1219 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1220 GTK_TYPE_FONT_SELECTION_DIALOG,
1221 GtkFontSelectionDialogPrivate);
1222 priv = fontseldiag->priv;
1224 content_area = gtk_dialog_get_content_area (dialog);
1225 action_area = gtk_dialog_get_action_area (dialog);
1227 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1228 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1229 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1230 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1232 gtk_widget_push_composite_child ();
1234 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1236 /* Create the content area */
1237 priv->fontsel = gtk_font_selection_new ();
1238 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1239 gtk_widget_show (priv->fontsel);
1240 gtk_box_pack_start (GTK_BOX (content_area),
1241 priv->fontsel, TRUE, TRUE, 0);
1243 /* Create the action area */
1244 priv->cancel_button = gtk_dialog_add_button (dialog,
1246 GTK_RESPONSE_CANCEL);
1248 priv->apply_button = gtk_dialog_add_button (dialog,
1250 GTK_RESPONSE_APPLY);
1251 gtk_widget_hide (priv->apply_button);
1253 priv->ok_button = gtk_dialog_add_button (dialog,
1256 gtk_widget_grab_default (priv->ok_button);
1258 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1261 GTK_RESPONSE_CANCEL,
1264 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1265 _("Font Selection"));
1267 gtk_widget_pop_composite_child ();
1271 * gtk_font_selection_dialog_new:
1272 * @title: the title of the dialog window
1274 * Creates a new #GtkFontSelectionDialog.
1276 * Return value: a new #GtkFontSelectionDialog
1279 gtk_font_selection_dialog_new (const gchar *title)
1281 GtkFontSelectionDialog *fontseldiag;
1283 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1286 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1288 return GTK_WIDGET (fontseldiag);
1292 * gtk_font_selection_dialog_get_font_selection:
1293 * @fsd: a #GtkFontSelectionDialog
1295 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1297 * Returns: (transfer none): the embedded #GtkFontSelection
1302 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1304 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1306 return fsd->priv->fontsel;
1311 * gtk_font_selection_dialog_get_ok_button:
1312 * @fsd: a #GtkFontSelectionDialog
1314 * Gets the 'OK' button.
1316 * Return value: (transfer none): the #GtkWidget used in the dialog
1317 * for the 'OK' button.
1322 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1324 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1326 return fsd->priv->ok_button;
1330 * gtk_font_selection_dialog_get_cancel_button:
1331 * @fsd: a #GtkFontSelectionDialog
1333 * Gets the 'Cancel' button.
1335 * Return value: (transfer none): the #GtkWidget used in the dialog
1336 * for the 'Cancel' button.
1341 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1343 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1345 return fsd->priv->cancel_button;
1349 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1351 parent_buildable_iface = g_type_interface_peek_parent (iface);
1352 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1356 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1357 GtkBuilder *builder,
1358 const gchar *childname)
1360 GtkFontSelectionDialogPrivate *priv;
1362 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1364 if (g_strcmp0 (childname, "ok_button") == 0)
1365 return G_OBJECT (priv->ok_button);
1366 else if (g_strcmp0 (childname, "cancel_button") == 0)
1367 return G_OBJECT (priv->cancel_button);
1368 else if (g_strcmp0 (childname, "apply_button") == 0)
1369 return G_OBJECT (priv->apply_button);
1370 else if (g_strcmp0 (childname, "font_selection") == 0)
1371 return G_OBJECT (priv->fontsel);
1373 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1377 * gtk_font_selection_dialog_get_font_name:
1378 * @fsd: a #GtkFontSelectionDialog
1380 * Gets the currently-selected font name.
1382 * Note that this can be a different string than what you set with
1383 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1384 * may normalize font names and thus return a string with a different
1385 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1386 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1387 * if you want to compare two font descriptions.
1389 * Return value: A string with the name of the current font, or %NULL if no
1390 * font is selected. You must free this string with g_free().
1393 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1395 GtkFontSelectionDialogPrivate *priv;
1397 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1401 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1405 * gtk_font_selection_dialog_set_font_name:
1406 * @fsd: a #GtkFontSelectionDialog
1407 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1409 * Sets the currently selected font.
1411 * Return value: %TRUE if the font selected in @fsd is now the
1412 * @fontname specified, %FALSE otherwise.
1415 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1416 const gchar *fontname)
1418 GtkFontSelectionDialogPrivate *priv;
1420 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1421 g_return_val_if_fail (fontname, FALSE);
1425 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1429 * gtk_font_selection_dialog_get_preview_text:
1430 * @fsd: a #GtkFontSelectionDialog
1432 * Gets the text displayed in the preview area.
1434 * Return value: the text displayed in the preview area.
1435 * This string is owned by the widget and should not be
1438 G_CONST_RETURN gchar*
1439 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1441 GtkFontSelectionDialogPrivate *priv;
1443 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1447 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1451 * gtk_font_selection_dialog_set_preview_text:
1452 * @fsd: a #GtkFontSelectionDialog
1453 * @text: the text to display in the preview area
1455 * Sets the text displayed in the preview area.
1458 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1461 GtkFontSelectionDialogPrivate *priv;
1463 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1464 g_return_if_fail (text != NULL);
1468 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);