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;
104 #ifndef GTK_DISABLE_DEPRECATED
105 GtkWidget *size_list;
106 GtkWidget *font_list;
107 GtkWidget *face_list;
109 GtkListStore *_size_model;
110 GtkListStore *_font_model;
111 GtkListStore *_face_model;
113 gboolean ignore_size;
114 gboolean ignore_face;
115 gboolean ignore_font;
120 struct _GtkFontSelectionDialogPrivate
124 GtkWidget *ok_button;
125 GtkWidget *apply_button;
126 GtkWidget *cancel_button;
130 #define DEFAULT_FONT_NAME "Sans 10"
131 #define MAX_FONT_SIZE 999
133 /* This is the initial fixed height and the top padding of the preview entry */
134 #define PREVIEW_HEIGHT 72
135 #define PREVIEW_TOP_PADDING 6
137 /* Widget default geometry */
138 #define FONTSEL_WIDTH 540
139 #define FONTSEL_HEIGHT 408
141 /* These are the sizes of the font, style & size lists. */
142 #define FONT_LIST_HEIGHT 136
143 #define FONT_LIST_WIDTH 190
144 #define FONT_STYLE_LIST_WIDTH 170
145 #define FONT_SIZE_LIST_WIDTH 60
147 #define ROW_FORMAT_STRING "<span weight=\"bold\" size=\"small\" foreground=\"%s\">%s</span>\n<span size=\"x-large\" font_desc=\"%s\">%s</span>"
149 /* These are what we use as the standard font sizes, for the size list.
151 #define FONT_SIZES_LENGTH 14
152 static const gint font_sizes[] = {
153 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
170 static void gtk_font_selection_set_property (GObject *object,
174 static void gtk_font_selection_get_property (GObject *object,
178 static void gtk_font_selection_finalize (GObject *object);
181 static void gtk_font_selection_screen_changed (GtkWidget *widget,
182 GdkScreen *previous_screen);
183 static void gtk_font_selection_style_updated (GtkWidget *widget);
186 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
187 PangoFontFamily *family);
188 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
189 PangoFontFace *face);
191 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
193 #ifndef GTK_DISABLE_DEPRECATED
194 static void update_font_list_selection (GtkFontSelection *fontsel);
195 static void update_size_list_selection (GtkFontSelection *fontsel);
196 static void update_face_model (GtkFontSelection *fontsel,
200 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
203 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
209 widget_class->screen_changed = gtk_font_selection_screen_changed;
210 widget_class->style_updated = gtk_font_selection_style_updated;
213 gobject_class->finalize = gtk_font_selection_finalize;
214 gobject_class->set_property = gtk_font_selection_set_property;
215 gobject_class->get_property = gtk_font_selection_get_property;
217 g_object_class_install_property (gobject_class,
219 g_param_spec_string ("font-name",
221 P_("The string that represents this font"),
223 GTK_PARAM_READWRITE));
224 g_object_class_install_property (gobject_class,
226 g_param_spec_string ("preview-text",
228 P_("The text to display in order to demonstrate the selected font"),
229 pango_language_get_sample_string (NULL),
230 GTK_PARAM_READWRITE));
232 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
236 gtk_font_selection_set_property (GObject *object,
241 GtkFontSelection *fontsel;
243 fontsel = GTK_FONT_SELECTION (object);
248 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
250 case PROP_PREVIEW_TEXT:
251 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 gtk_font_selection_get_property (GObject *object,
265 GtkFontSelection *fontsel;
267 fontsel = GTK_FONT_SELECTION (object);
272 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
274 case PROP_PREVIEW_TEXT:
275 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 refilter_and_focus (GtkFontSelectionPrivate *priv)
287 GtkTreeView *treeview = GTK_TREE_VIEW (priv->family_face_list);
288 GtkTreePath *path = gtk_tree_path_new ();
290 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
295 gtk_tree_view_get_cursor (treeview, &path, NULL);
297 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->filter), &iter, path))
299 gtk_tree_path_free (path);
303 gtk_tree_view_scroll_to_cell (treeview, path, NULL, TRUE, 0.5, 0.5);
304 gtk_tree_path_free (path);
308 deleted_text_cb (GtkEntryBuffer *buffer,
313 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
314 GtkWidget *entry = priv->search_entry;
316 if (gtk_entry_buffer_get_length (buffer) == 0)
318 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
319 GTK_ENTRY_ICON_SECONDARY,
323 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
327 inserted_text_cb (GtkEntryBuffer *buffer,
333 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
334 GtkWidget *entry = priv->search_entry;
336 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
338 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
339 GTK_ENTRY_ICON_SECONDARY,
343 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
347 icon_press_cb (GtkEntry *entry,
348 GtkEntryIconPosition pos,
352 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
356 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
358 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
360 /* If we set the silder value manually, we ignore this callback */
361 if (priv->ignore_slider)
363 priv->ignore_slider = FALSE;
367 gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
368 gtk_adjustment_get_value (adjustment));
372 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
374 PangoFontDescription *desc;
375 GtkFontSelection *fontsel = (GtkFontSelection*)data;
376 GtkFontSelectionPrivate *priv = fontsel->priv;
378 gdouble size = gtk_adjustment_get_value (adjustment);
380 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
382 /* We ignore the slider value change callback for both of this set_value call */
383 priv->ignore_slider = TRUE;
384 if (size < gtk_adjustment_get_lower (slider_adj))
385 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
386 else if (size > gtk_adjustment_get_upper (slider_adj))
387 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
389 gtk_adjustment_set_value (slider_adj, size);
391 priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
393 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
394 pango_font_description_set_size (desc, priv->size);
395 gtk_widget_override_font (priv->preview, desc);
398 #ifndef GTK_DISABLE_DEPRECATED
399 priv->ignore_size = TRUE;
400 update_size_list_selection (fontsel);
401 #endif /* GTK_DISABLE_DEPRECATED */
403 gtk_widget_queue_draw (priv->preview);
405 g_object_notify (G_OBJECT (fontsel), "font-name");
409 set_range_marks (GtkFontSelectionPrivate *priv,
410 GtkWidget* size_slider,
420 sizes = (gint*)font_sizes;
421 length = FONT_SIZES_LENGTH;
424 gtk_scale_clear_marks (GTK_SCALE (size_slider));
426 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
428 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
429 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
431 value = gtk_adjustment_get_value (adj);
432 if (value > (gdouble) sizes[length-1])
434 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
435 priv->ignore_slider = TRUE;
437 else if (value < (gdouble) sizes[0])
439 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
440 priv->ignore_slider = TRUE;
443 #ifndef GTK_DISABLE_DEPRECATED
444 if (!priv->_size_model)
446 for (i=0; i<length; i++)
447 gtk_scale_add_mark (GTK_SCALE (size_slider),
449 GTK_POS_BOTTOM, NULL);
453 GString *size_str = g_string_new (NULL);
454 gtk_list_store_clear (priv->_size_model);
456 for (i=0; i<length; i++)
460 g_string_printf (size_str, "%d", sizes[i]);
462 gtk_scale_add_mark (GTK_SCALE (size_slider),
464 GTK_POS_BOTTOM, NULL);
466 gtk_list_store_append (priv->_size_model, &iter);
467 gtk_list_store_set (priv->_size_model, &iter,
472 g_string_free (size_str, TRUE);
475 for (i=0; i<length; i++)
476 gtk_scale_add_mark (GTK_SCALE (size_slider),
478 GTK_POS_BOTTOM, NULL);
483 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
485 PangoFontFamily *family;
487 PangoFontDescription *desc;
493 GtkTreePath *path = gtk_tree_path_new ();
495 GtkFontSelection *fontsel = (GtkFontSelection*)data;
497 gtk_tree_view_get_cursor (treeview, &path, NULL);
502 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->filter), &iter, path))
504 gtk_tree_path_free (path);
509 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->filter), &iter,
511 FAMILY_COLUMN, &family,
514 gtk_tree_view_scroll_to_cell (treeview, path, NULL, TRUE, 0.5, 0.5);
516 gtk_tree_path_free (path);
519 if (!face || !family)
521 g_object_unref (face);
522 g_object_unref (family);
526 desc = pango_font_face_describe (face);
527 pango_font_description_set_size (desc, fontsel->priv->size);
528 gtk_widget_override_font (fontsel->priv->preview, desc);
530 pango_font_face_list_sizes (face, &sizes, &n_sizes);
531 /* It seems not many fonts actually have a sane set of sizes */
532 for (i=0; i<n_sizes; i++)
533 sizes[i] = sizes[i] / PANGO_SCALE;
535 set_range_marks (fontsel->priv, fontsel->priv->size_slider, sizes, n_sizes);
537 gtk_font_selection_ref_family (fontsel, family);
538 gtk_font_selection_ref_face (fontsel, face);
540 #ifndef GTK_DISABLE_DEPRECATED
541 if (fontsel->priv->_font_model)
542 update_font_list_selection (fontsel);
546 g_object_unref ((gpointer)family);
547 g_object_unref ((gpointer)face);
548 pango_font_description_free(desc);
550 g_object_notify (G_OBJECT (fontsel), "font-name");
554 zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
556 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
558 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
560 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
561 gtk_adjustment_set_value (adj,
562 gtk_adjustment_get_value (adj) +
563 gtk_adjustment_get_step_increment (adj));
564 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
565 gtk_adjustment_set_value (adj,
566 gtk_adjustment_get_value (adj) -
567 gtk_adjustment_get_step_increment (adj));
572 gtk_font_selection_init (GtkFontSelection *fontsel)
574 GtkFontSelectionPrivate *priv;
575 PangoFontDescription *font_desc;
576 GtkWidget *scrolled_win;
577 GtkWidget *preview_and_size;
578 GtkWidget *size_controls;
580 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
581 GTK_TYPE_FONT_SELECTION,
582 GtkFontSelectionPrivate);
584 priv = fontsel->priv;
586 #ifndef GTK_DISABLE_DEPRECATED
587 priv->size_list = NULL;
588 priv->font_list = NULL;
589 priv->face_list = NULL;
591 priv->_size_model = NULL;
592 priv->_font_model = NULL;
593 priv->_face_model = NULL;
595 priv->ignore_size = FALSE;
596 priv->ignore_face = FALSE;
597 priv->ignore_font = FALSE;
598 #endif /* GTK_DISABLE_DEPRECATED */
600 /* Getting the default size */
601 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
602 priv->size = pango_font_description_get_size (font_desc);
606 gtk_widget_push_composite_child ();
608 /* Creating fundamental widgets for the private struct */
609 priv->search_entry = gtk_entry_new ();
610 priv->family_face_list = gtk_tree_view_new ();
611 priv->preview = gtk_entry_new ();
612 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
613 (gdouble) font_sizes[0],
614 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
617 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
619 /** Bootstrapping widget layout **/
620 gtk_box_set_spacing (GTK_BOX (fontsel), 6);
621 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
623 /* Main font family/face view */
624 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
625 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
626 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
627 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
628 GTK_SHADOW_ETCHED_IN);
629 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
631 /* Alignment for the preview and size controls */
632 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
634 preview_and_size = gtk_vbox_new (TRUE, 0);
635 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
636 gtk_box_set_spacing (GTK_BOX (preview_and_size), 6);
638 /* The preview entry needs a scrolled window to make sure we have a */
639 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
640 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
641 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
642 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
643 GTK_SHADOW_ETCHED_IN);
644 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
646 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
648 /* Setting the size requests for various widgets */
649 gtk_widget_set_size_request (GTK_WIDGET (fontsel), FONTSEL_WIDTH, FONTSEL_HEIGHT);
650 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
651 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
653 /* Unset the frame on the preview entry */
654 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
656 /* Packing the slider and the spin in a hbox */
657 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
658 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
659 gtk_box_set_spacing (GTK_BOX (size_controls), 6);
660 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
661 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
663 gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
665 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
667 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(preview_and_size), FALSE, TRUE, 0);
669 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
670 (gdouble)(priv->size / PANGO_SCALE));
671 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
672 (gdouble)(priv->size / PANGO_SCALE));
674 gtk_widget_show_all (GTK_WIDGET (fontsel));
675 gtk_widget_hide (GTK_WIDGET (fontsel));
677 /* Treeview column and model bootstrapping */
678 gtk_font_selection_bootstrap_fontlist (fontsel);
680 /* Set default preview text */
681 gtk_entry_set_text (GTK_ENTRY (priv->preview),
682 pango_language_get_sample_string (NULL));
684 /* Set search icon and place holder text */
685 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
686 GTK_ENTRY_ICON_SECONDARY,
688 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
690 /** Callback connections **/
691 /* Connect to callback for the live search text entry */
692 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
693 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
694 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
695 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
696 g_signal_connect (G_OBJECT (priv->search_entry),
697 "icon-press", G_CALLBACK (icon_press_cb), priv);
699 /* Size controls callbacks */
700 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
701 "value-changed", G_CALLBACK (slider_change_cb), priv);
702 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
703 "value-changed", G_CALLBACK (spin_change_cb), fontsel);
704 priv->ignore_slider = FALSE;
706 /* Font selection callback */
707 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
708 G_CALLBACK (cursor_changed_cb), fontsel);
710 /* Zoom on preview scroll*/
711 g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
712 G_CALLBACK (zoom_preview_cb), priv);
714 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
715 G_CALLBACK (zoom_preview_cb), priv);
717 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
719 /* Set default focus */
720 gtk_widget_pop_composite_child();
724 * gtk_font_selection_new:
726 * Creates a new #GtkFontSelection.
728 * Return value: a new #GtkFontSelection
731 gtk_font_selection_new (void)
733 GtkFontSelection *fontsel;
735 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
737 return GTK_WIDGET (fontsel);
741 cmp_families (const void *a, const void *b)
743 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
744 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
746 return g_utf8_collate (a_name, b_name);
750 populate_list (GtkTreeView* treeview, GtkListStore* model)
752 GtkStyleContext *style_context;
756 PangoFontDescription *default_font;
758 GtkTreeIter match_row;
762 PangoFontFamily **families;
764 GString *tmp = g_string_new (NULL);
765 GString *family_and_face = g_string_new (NULL);
767 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
771 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
773 gtk_list_store_clear (model);
775 /* Get row header font color */
776 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
777 gtk_style_context_get_color (style_context,
778 GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_INSENSITIVE,
781 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
782 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
783 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
784 color_string = pango_color_to_string (&p_color);
787 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
790 /* Iterate over families and faces */
791 for (i=0; i<n_families; i++)
794 PangoFontFace **faces;
797 const gchar *fam_name = pango_font_family_get_name (families[i]);
799 pango_font_family_list_faces (families[i], &faces, &n_faces);
801 for (j=0; j<n_faces; j++)
803 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
804 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
805 gchar *font_desc = pango_font_description_to_string (pango_desc);
807 /* foreground_color, family_name, face_name, desc, sample string */
808 g_string_printf (family_and_face, "%s %s",
812 g_string_printf (tmp, ROW_FORMAT_STRING,
814 family_and_face->str,
816 pango_language_get_sample_string (NULL));
818 gtk_list_store_append (model, &iter);
819 gtk_list_store_set (model, &iter,
820 FAMILY_COLUMN, families[i],
821 FACE_COLUMN, faces[j],
822 PREVIEW_TITLE_COLUMN, family_and_face->str,
823 PREVIEW_TEXT_COLUMN, tmp->str,
826 /* Select the first font or the default font/face from the style context */
827 if ((i == 0 && j == 0) ||
828 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
831 pango_font_description_free(pango_desc);
838 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
842 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
843 gtk_tree_view_scroll_to_cell (treeview, path, NULL, TRUE, 0.5, 0.5);
844 gtk_tree_path_free(path);
847 g_string_free (family_and_face, TRUE);
848 g_string_free (tmp, TRUE);
849 g_free (color_string);
854 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
856 gboolean result = FALSE;
857 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
859 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
861 gchar *font_name_casefold;
862 gchar *search_text_casefold;
864 gtk_tree_model_get (model, iter,
865 PREVIEW_TITLE_COLUMN, &font_name,
868 /* Covering some corner cases to speed up the result */
869 if (font_name == NULL ||
870 strlen (search_text) > strlen (font_name))
875 if (strlen (search_text) == 0)
881 font_name_casefold = g_utf8_casefold (font_name, -1);
882 search_text_casefold = g_utf8_casefold (search_text, -1);
884 if (g_strrstr (font_name_casefold, search_text_casefold))
887 g_free (search_text_casefold);
888 g_free (font_name_casefold);
894 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
896 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
897 GtkCellRenderer *cell;
898 GtkTreeViewColumn *col;
900 fontsel->priv->model = gtk_list_store_new (4,
901 PANGO_TYPE_FONT_FAMILY,
902 PANGO_TYPE_FONT_FACE,
906 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
908 g_object_unref (fontsel->priv->model);
910 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
912 (gpointer)fontsel->priv,
915 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
916 g_object_unref (fontsel->priv->filter);
918 gtk_tree_view_set_rules_hint (treeview, TRUE);
919 gtk_tree_view_set_headers_visible (treeview, FALSE);
921 cell = gtk_cell_renderer_text_new ();
922 col = gtk_tree_view_column_new_with_attributes ("Family",
924 "markup", PREVIEW_TEXT_COLUMN,
927 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
929 gtk_tree_view_append_column (treeview, col);
931 populate_list (treeview, fontsel->priv->model);
936 gtk_font_selection_finalize (GObject *object)
938 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
940 gtk_font_selection_ref_family (fontsel, NULL);
941 gtk_font_selection_ref_face (fontsel, NULL);
943 #ifndef GTK_DISABLE_DEPRECATED
944 if (fontsel->priv->size_list)
946 g_object_unref (fontsel->priv->size_list);
947 g_object_unref (fontsel->priv->font_list);
948 g_object_unref (fontsel->priv->face_list);
952 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
957 gtk_font_selection_screen_changed (GtkWidget *widget,
958 GdkScreen *previous_screen)
964 gtk_font_selection_style_updated (GtkWidget *widget)
966 /*GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);*/
972 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
973 PangoFontFamily *family)
975 GtkFontSelectionPrivate *priv = fontsel->priv;
978 family = g_object_ref (family);
980 g_object_unref (priv->family);
981 priv->family = family;
985 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
988 GtkFontSelectionPrivate *priv = fontsel->priv;
991 face = g_object_ref (face);
993 g_object_unref (priv->face);
997 #ifndef GTK_DISABLE_DEPRECATED
999 populate_font_model (GtkFontSelection *fontsel)
1002 PangoFontFamily **families;
1003 GtkFontSelectionPrivate *priv = fontsel->priv;
1006 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
1010 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
1012 gtk_list_store_clear (priv->_font_model);
1014 for (i=0; i<n_families; i++)
1018 gtk_list_store_append (priv->_font_model, &iter);
1019 gtk_list_store_set (priv->_font_model, &iter,
1021 1, pango_font_family_get_name (families[i]),
1024 !strcmp (pango_font_family_get_name (families[i]),
1025 pango_font_family_get_name (priv->family)))
1027 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model),
1031 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1032 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv),
1036 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv),
1037 path, NULL, TRUE, 0.5, 0.5);
1038 gtk_tree_path_free (path);
1047 update_font_list_selection (GtkFontSelection *fontsel)
1051 GtkFontSelectionPrivate *priv = fontsel->priv;
1054 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_font_model), &iter);
1059 gtk_tree_model_get (GTK_TREE_MODEL (priv->_font_model), &iter,
1062 if (strcmp (family_name, pango_font_family_get_name (priv->family)))
1064 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_font_model), &iter);
1065 g_free (family_name);
1068 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_font_model), &iter);
1072 g_free (family_name);
1076 tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1078 priv->ignore_font = TRUE;
1079 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1080 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv), path, NULL, TRUE, 0.5, 0.5);
1082 /* Free resources */
1083 gtk_tree_path_free (path);
1084 g_free (family_name);
1088 update_face_model (fontsel, FALSE);
1092 update_face_model (GtkFontSelection *fontsel, gboolean first)
1094 GtkFontSelectionPrivate *priv = fontsel->priv;
1095 PangoFontFace **faces;
1098 pango_font_family_list_faces (priv->family, &faces, &n_faces);
1100 gtk_list_store_clear (priv->_face_model);
1102 for (i=0; i<n_faces; i++)
1106 gtk_list_store_append (priv->_face_model, &iter);
1107 gtk_list_store_set (priv->_face_model, &iter,
1109 1, pango_font_face_get_face_name (faces[i]),
1112 if ((!first && faces[i] == priv->face) ||
1118 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model), &iter);
1122 tv = gtk_bin_get_child (GTK_BIN (priv->face_list));
1125 priv->ignore_face = TRUE;
1127 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1129 gtk_tree_path_free (path);
1132 gtk_font_selection_ref_face (fontsel, faces[i]);
1136 update_size_list_selection (fontsel);
1138 g_object_notify (G_OBJECT (fontsel), "font-name");
1142 update_size_list_selection (GtkFontSelection *fontsel)
1146 GtkFontSelectionPrivate *priv = fontsel->priv;
1147 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->size_list));
1149 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (tv)));
1151 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_size_model), &iter);
1155 gtk_tree_model_get (GTK_TREE_MODEL (priv->_size_model), &iter,
1159 if (size * PANGO_SCALE == priv->size)
1164 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_size_model), &iter);
1169 priv->ignore_size = TRUE;
1170 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1172 gtk_tree_path_free (path);
1175 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_size_model), &iter);
1181 select_family_and_face (GtkFontSelection *fontsel)
1185 GtkFontSelectionPrivate *priv = fontsel->priv;
1186 PangoFontFace *face;
1187 PangoFontFamily *family;
1189 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1191 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1194 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1196 FAMILY_COLUMN, &family,
1199 if (face == priv->face && family == priv->family)
1203 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter), &iter);
1206 g_object_unref (face);
1207 g_object_unref (family);
1211 priv->ignore_size = TRUE;
1212 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list), path, NULL, FALSE);
1214 gtk_tree_path_free (path);
1215 g_object_unref (face);
1216 g_object_unref (family);
1220 g_object_unref (face);
1221 g_object_unref (family);
1222 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter);
1228 family_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1233 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1234 PangoFontFamily *font;
1236 if (fontsel->priv->ignore_font)
1238 fontsel->priv->ignore_font = FALSE;
1242 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->font_list));
1243 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1248 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_font_model),
1252 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_font_model), &iter,
1256 gtk_font_selection_ref_family (fontsel, font);
1257 update_face_model (fontsel, TRUE);
1259 fontsel->priv->ignore_font = TRUE;
1260 select_family_and_face (fontsel);
1262 gtk_tree_path_free (path);
1263 g_object_unref (font);
1267 face_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1272 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1273 PangoFontFace *face;
1275 if (fontsel->priv->ignore_face)
1277 fontsel->priv->ignore_face = FALSE;
1281 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->face_list));
1282 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1287 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_face_model),
1291 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_face_model), &iter,
1295 gtk_font_selection_ref_face (fontsel, face);
1297 fontsel->priv->ignore_face = TRUE;
1298 select_family_and_face (fontsel);
1300 gtk_tree_path_free (path);
1301 g_object_unref (face);
1305 size_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1310 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1313 if (fontsel->priv->ignore_size)
1315 fontsel->priv->ignore_size = FALSE;
1319 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->size_list));
1320 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1325 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_size_model),
1329 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_size_model), &iter,
1333 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fontsel->priv->size_spin), value);
1334 gtk_tree_path_free (path);
1338 initialize_deprecated_widgets (GtkFontSelection *fontsel)
1340 GtkTreeViewColumn *col;
1341 GtkFontSelectionPrivate *priv = fontsel->priv;
1343 GtkWidget *size_list;
1344 GtkWidget *font_list;
1345 GtkWidget *face_list;
1347 priv->_size_model = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING);
1348 priv->_font_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FAMILY, G_TYPE_STRING);
1349 priv->_face_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FACE, G_TYPE_STRING);
1351 size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_size_model));
1352 font_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_font_model));
1353 face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_face_model));
1355 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (size_list), FALSE);
1356 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (font_list), FALSE);
1357 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (face_list), FALSE);
1359 g_object_unref (priv->_size_model);
1360 g_object_unref (priv->_font_model);
1361 g_object_unref (priv->_face_model);
1363 col = gtk_tree_view_column_new_with_attributes ("Size",
1364 gtk_cell_renderer_text_new (),
1367 gtk_tree_view_append_column (GTK_TREE_VIEW (size_list), col);
1369 col = gtk_tree_view_column_new_with_attributes ("Family",
1370 gtk_cell_renderer_text_new (),
1373 gtk_tree_view_append_column (GTK_TREE_VIEW (font_list), col);
1375 col = gtk_tree_view_column_new_with_attributes ("Face",
1376 gtk_cell_renderer_text_new (),
1379 gtk_tree_view_append_column (GTK_TREE_VIEW (face_list), col);
1382 priv->font_list = gtk_scrolled_window_new (NULL, NULL);
1383 priv->face_list = gtk_scrolled_window_new (NULL, NULL);
1384 priv->size_list = gtk_scrolled_window_new (NULL, NULL);
1386 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->font_list),
1387 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1388 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->face_list),
1389 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1390 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->size_list),
1391 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1393 gtk_container_add (GTK_CONTAINER (priv->font_list), font_list);
1394 gtk_container_add (GTK_CONTAINER (priv->face_list), face_list);
1395 gtk_container_add (GTK_CONTAINER (priv->size_list), size_list);
1397 g_signal_connect (G_OBJECT (font_list), "cursor-changed",
1398 G_CALLBACK (family_list_cursor_changed_cb), fontsel);
1400 g_signal_connect (G_OBJECT (face_list), "cursor-changed",
1401 G_CALLBACK (face_list_cursor_changed_cb), fontsel);
1403 g_signal_connect (G_OBJECT (size_list), "cursor-changed",
1404 G_CALLBACK (size_list_cursor_changed_cb), fontsel);
1406 populate_font_model (fontsel);
1407 cursor_changed_cb (GTK_TREE_VIEW (priv->family_face_list), fontsel);
1410 #endif /* GTK_DISABLE_DEPRECATED */
1412 /*****************************************************************************
1413 * These functions are the main public interface for getting/setting the font.
1414 *****************************************************************************/
1417 * gtk_font_selection_get_family:
1418 * @fontsel: a #GtkFontSelection
1420 * Gets the #PangoFontFamily representing the selected font family.
1422 * Return value: (transfer none): A #PangoFontFamily representing the
1423 * selected font family. Font families are a collection of font
1424 * faces. The returned object is owned by @fontsel and must not
1425 * be modified or freed.
1430 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1432 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1434 return fontsel->priv->family;
1438 * gtk_font_selection_get_face:
1439 * @fontsel: a #GtkFontSelection
1441 * Gets the #PangoFontFace representing the selected font group
1442 * details (i.e. family, slant, weight, width, etc).
1444 * Return value: (transfer none): A #PangoFontFace representing the
1445 * selected font group details. The returned object is owned by
1446 * @fontsel and must not be modified or freed.
1451 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1453 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1455 return fontsel->priv->face;
1459 * gtk_font_selection_get_size:
1460 * @fontsel: a #GtkFontSelection
1462 * The selected font size.
1464 * Return value: A n integer representing the selected font size,
1465 * or -1 if no font size is selected.
1470 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1472 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1474 return fontsel->priv->size;
1478 * gtk_font_selection_get_font_name:
1479 * @fontsel: a #GtkFontSelection
1481 * Gets the currently-selected font name.
1483 * Note that this can be a different string than what you set with
1484 * gtk_font_selection_set_font_name(), as the font selection widget may
1485 * normalize font names and thus return a string with a different structure.
1486 * For example, "Helvetica Italic Bold 12" could be normalized to
1487 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1488 * if you want to compare two font descriptions.
1490 * Return value: (transfer full) (allow-none): A string with the name of the
1491 * current font, or %NULL if no font is selected. You must free this
1492 * string with g_free().
1495 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1498 PangoFontDescription *desc;
1500 if (!fontsel->priv->face)
1503 desc = pango_font_face_describe (fontsel->priv->face);
1504 font_name = pango_font_description_to_string (desc);
1505 pango_font_description_free (desc);
1509 /* This sets the current font, then selecting the appropriate list rows. */
1512 * gtk_font_selection_set_font_name:
1513 * @fontsel: a #GtkFontSelection
1514 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1516 * Sets the currently-selected font.
1518 * Note that the @fontsel needs to know the screen in which it will appear
1519 * for this to work; this can be guaranteed by simply making sure that the
1520 * @fontsel is inserted in a toplevel window before you call this function.
1522 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1523 * such font exists or if the @fontsel doesn't belong to a particular
1527 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1528 const gchar *fontname)
1530 GtkFontSelectionPrivate *priv = fontsel->priv;
1534 PangoFontDescription *desc;
1535 gboolean found = FALSE;
1537 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1539 if (!gtk_widget_has_screen (GTK_WIDGET (fontsel)))
1542 desc = pango_font_description_from_string (fontname);
1543 family_name = (gchar*)pango_font_description_get_family (desc);
1547 pango_font_description_free (desc);
1551 /* We make sure the filter is clear */
1552 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1554 /* We find the matching family/face */
1555 for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1557 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter))
1559 PangoFontFace *face;
1560 PangoFontDescription *tmp_desc;
1562 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1566 tmp_desc = pango_font_face_describe (face);
1567 if (pango_font_description_get_size_is_absolute (desc))
1568 pango_font_description_set_absolute_size (tmp_desc,
1569 pango_font_description_get_size (desc));
1571 pango_font_description_set_size (tmp_desc,
1572 pango_font_description_get_size (desc));
1575 if (pango_font_description_equal (desc, tmp_desc))
1578 gint size = pango_font_description_get_size (desc);
1582 if (pango_font_description_get_size_is_absolute (desc))
1583 size = size * PANGO_SCALE;
1584 gtk_spin_button_set_value (GTK_SPIN_BUTTON (priv->size_spin),
1585 size / PANGO_SCALE);
1588 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter),
1593 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list),
1597 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->family_face_list),
1603 gtk_tree_path_free (path);
1609 g_object_unref (face);
1610 pango_font_description_free (tmp_desc);
1616 pango_font_description_free (desc);
1617 g_object_notify (G_OBJECT (fontsel), "font-name");
1623 * gtk_font_selection_get_preview_text:
1624 * @fontsel: a #GtkFontSelection
1626 * Gets the text displayed in the preview area.
1628 * Return value: the text displayed in the preview area.
1629 * This string is owned by the widget and should not be
1632 G_CONST_RETURN gchar*
1633 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1640 * gtk_font_selection_set_preview_text:
1641 * @fontsel: a #GtkFontSelection
1642 * @text: the text to display in the preview area
1644 * Sets the text displayed in the preview area.
1645 * The @text is used to show how the selected font looks.
1648 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1652 GtkFontSelectionPrivate *priv;
1654 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1655 g_return_if_fail (text != NULL);
1657 priv = fontsel->priv;
1661 #ifndef GTK_DISABLE_DEPRECATED
1664 * gtk_font_selection_get_family_list:
1665 * @fontsel: a #GtkFontSelection
1667 * This returns the #GtkTreeView that lists font families, for
1668 * example, 'Sans', 'Serif', etc.
1670 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1675 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1677 GtkFontSelectionPrivate *priv = fontsel->priv;
1678 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1679 if (!priv->font_list)
1680 initialize_deprecated_widgets (fontsel);
1682 return priv->font_list;
1686 * gtk_font_selection_get_face_list:
1687 * @fontsel: a #GtkFontSelection
1689 * This returns the #GtkTreeView which lists all styles available for
1690 * the selected font. For example, 'Regular', 'Bold', etc.
1692 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1697 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1699 GtkFontSelectionPrivate *priv = fontsel->priv;
1700 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1701 if (!priv->face_list)
1702 initialize_deprecated_widgets (fontsel);
1704 return priv->face_list;
1708 * gtk_font_selection_get_size_entry:
1709 * @fontsel: a #GtkFontSelection
1711 * This returns the #GtkEntry used to allow the user to edit the font
1712 * number manually instead of selecting it from the list of font sizes.
1714 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1719 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1721 GtkFontSelectionPrivate *priv = fontsel->priv;
1722 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1724 return priv->size_spin;
1728 * gtk_font_selection_get_size_list:
1729 * @fontsel: a #GtkFontSelection
1731 * This returns the #GtkTreeeView used to list font sizes.
1733 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1738 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1740 GtkFontSelectionPrivate *priv = fontsel->priv;
1741 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1742 if (!priv->size_list)
1743 initialize_deprecated_widgets (fontsel);
1745 return priv->size_list;
1749 * gtk_font_selection_get_preview_entry:
1750 * @fontsel: a #GtkFontSelection
1752 * This returns the #GtkEntry used to display the font as a preview.
1754 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1759 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1761 GtkFontSelectionPrivate *priv = fontsel->priv;
1762 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1764 return priv->preview;
1767 #endif /* GTK_DISABLE_DEPRECATED */
1770 * SECTION:gtkfontseldlg
1771 * @Short_description: A dialog box for selecting fonts
1772 * @Title: GtkFontSelectionDialog
1773 * @See_also: #GtkFontSelection, #GtkDialog
1775 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1777 * To set the font which is initially selected, use
1778 * gtk_font_selection_dialog_set_font_name().
1780 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1782 * To change the text which is shown in the preview area, use
1783 * gtk_font_selection_dialog_set_preview_text().
1785 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1786 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1787 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1788 * exposes the embedded #GtkFontSelection as internal child with the
1789 * name "font_selection". It also exposes the buttons with the names
1790 * "ok_button", "cancel_button" and "apply_button".
1794 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1795 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1796 GtkBuilder *builder,
1797 const gchar *childname);
1799 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1801 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1802 gtk_font_selection_dialog_buildable_interface_init))
1804 static GtkBuildableIface *parent_buildable_iface;
1807 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1809 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1813 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1815 GtkFontSelectionDialogPrivate *priv;
1816 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1817 GtkWidget *action_area, *content_area;
1819 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1820 GTK_TYPE_FONT_SELECTION_DIALOG,
1821 GtkFontSelectionDialogPrivate);
1822 priv = fontseldiag->priv;
1824 content_area = gtk_dialog_get_content_area (dialog);
1825 action_area = gtk_dialog_get_action_area (dialog);
1827 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1828 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1829 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1830 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1832 gtk_widget_push_composite_child ();
1834 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1836 /* Create the content area */
1837 priv->fontsel = gtk_font_selection_new ();
1838 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1839 gtk_widget_show (priv->fontsel);
1840 gtk_box_pack_start (GTK_BOX (content_area),
1841 priv->fontsel, TRUE, TRUE, 0);
1843 /* Create the action area */
1844 priv->cancel_button = gtk_dialog_add_button (dialog,
1846 GTK_RESPONSE_CANCEL);
1848 priv->apply_button = gtk_dialog_add_button (dialog,
1850 GTK_RESPONSE_APPLY);
1851 gtk_widget_hide (priv->apply_button);
1853 priv->ok_button = gtk_dialog_add_button (dialog,
1856 gtk_widget_grab_default (priv->ok_button);
1858 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1861 GTK_RESPONSE_CANCEL,
1864 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1865 _("Font Selection"));
1867 gtk_widget_pop_composite_child ();
1871 * gtk_font_selection_dialog_new:
1872 * @title: the title of the dialog window
1874 * Creates a new #GtkFontSelectionDialog.
1876 * Return value: a new #GtkFontSelectionDialog
1879 gtk_font_selection_dialog_new (const gchar *title)
1881 GtkFontSelectionDialog *fontseldiag;
1883 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1886 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1888 return GTK_WIDGET (fontseldiag);
1892 * gtk_font_selection_dialog_get_font_selection:
1893 * @fsd: a #GtkFontSelectionDialog
1895 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1897 * Returns: (transfer none): the embedded #GtkFontSelection
1902 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1904 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1906 return fsd->priv->fontsel;
1911 * gtk_font_selection_dialog_get_ok_button:
1912 * @fsd: a #GtkFontSelectionDialog
1914 * Gets the 'OK' button.
1916 * Return value: (transfer none): the #GtkWidget used in the dialog
1917 * for the 'OK' button.
1922 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1924 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1926 return fsd->priv->ok_button;
1930 * gtk_font_selection_dialog_get_cancel_button:
1931 * @fsd: a #GtkFontSelectionDialog
1933 * Gets the 'Cancel' button.
1935 * Return value: (transfer none): the #GtkWidget used in the dialog
1936 * for the 'Cancel' button.
1941 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1943 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1945 return fsd->priv->cancel_button;
1949 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1951 parent_buildable_iface = g_type_interface_peek_parent (iface);
1952 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1956 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1957 GtkBuilder *builder,
1958 const gchar *childname)
1960 GtkFontSelectionDialogPrivate *priv;
1962 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1964 if (g_strcmp0 (childname, "ok_button") == 0)
1965 return G_OBJECT (priv->ok_button);
1966 else if (g_strcmp0 (childname, "cancel_button") == 0)
1967 return G_OBJECT (priv->cancel_button);
1968 else if (g_strcmp0 (childname, "apply_button") == 0)
1969 return G_OBJECT (priv->apply_button);
1970 else if (g_strcmp0 (childname, "font_selection") == 0)
1971 return G_OBJECT (priv->fontsel);
1973 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1977 * gtk_font_selection_dialog_get_font_name:
1978 * @fsd: a #GtkFontSelectionDialog
1980 * Gets the currently-selected font name.
1982 * Note that this can be a different string than what you set with
1983 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1984 * may normalize font names and thus return a string with a different
1985 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1986 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1987 * if you want to compare two font descriptions.
1989 * Return value: A string with the name of the current font, or %NULL if no
1990 * font is selected. You must free this string with g_free().
1993 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1995 GtkFontSelectionDialogPrivate *priv;
1997 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
2001 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
2005 * gtk_font_selection_dialog_set_font_name:
2006 * @fsd: a #GtkFontSelectionDialog
2007 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
2009 * Sets the currently selected font.
2011 * Return value: %TRUE if the font selected in @fsd is now the
2012 * @fontname specified, %FALSE otherwise.
2015 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
2016 const gchar *fontname)
2018 GtkFontSelectionDialogPrivate *priv;
2020 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
2021 g_return_val_if_fail (fontname, FALSE);
2025 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
2029 * gtk_font_selection_dialog_get_preview_text:
2030 * @fsd: a #GtkFontSelectionDialog
2032 * Gets the text displayed in the preview area.
2034 * Return value: the text displayed in the preview area.
2035 * This string is owned by the widget and should not be
2038 G_CONST_RETURN gchar*
2039 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
2041 GtkFontSelectionDialogPrivate *priv;
2043 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
2047 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
2051 * gtk_font_selection_dialog_set_preview_text:
2052 * @fsd: a #GtkFontSelectionDialog
2053 * @text: the text to display in the preview area
2055 * Sets the text displayed in the preview area.
2058 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
2061 GtkFontSelectionDialogPrivate *priv;
2063 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
2064 g_return_if_fail (text != NULL);
2068 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);