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, FALSE, 0.0, 0.0);
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);
407 set_range_marks (GtkFontSelectionPrivate *priv,
408 GtkWidget* size_slider,
418 sizes = (gint*)font_sizes;
419 length = FONT_SIZES_LENGTH;
422 gtk_scale_clear_marks (GTK_SCALE (size_slider));
424 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
426 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
427 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
429 value = gtk_adjustment_get_value (adj);
430 if (value > (gdouble) sizes[length-1])
432 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
433 priv->ignore_slider = TRUE;
435 else if (value < (gdouble) sizes[0])
437 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
438 priv->ignore_slider = TRUE;
441 #ifndef GTK_DISABLE_DEPRECATED
442 if (!priv->_size_model)
444 for (i=0; i<length; i++)
445 gtk_scale_add_mark (GTK_SCALE (size_slider),
447 GTK_POS_BOTTOM, NULL);
451 GString *size_str = g_string_new (NULL);
452 gtk_list_store_clear (priv->_size_model);
454 for (i=0; i<length; i++)
458 g_string_printf (size_str, "%d", sizes[i]);
460 gtk_scale_add_mark (GTK_SCALE (size_slider),
462 GTK_POS_BOTTOM, NULL);
464 gtk_list_store_append (priv->_size_model, &iter);
465 gtk_list_store_set (priv->_size_model, &iter,
470 g_string_free (size_str, TRUE);
473 for (i=0; i<length; i++)
474 gtk_scale_add_mark (GTK_SCALE (size_slider),
476 GTK_POS_BOTTOM, NULL);
481 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
483 PangoFontFamily *family;
485 PangoFontDescription *desc;
492 GtkTreePath *path = gtk_tree_path_new ();
494 GtkFontSelection *fontsel = (GtkFontSelection*)data;
496 gtk_tree_view_get_cursor (treeview, &path, NULL);
501 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->filter), &iter, path))
503 gtk_tree_path_free (path);
508 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->filter), &iter,
510 FAMILY_COLUMN, &family,
513 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
515 gtk_tree_path_free (path);
518 if (!face || !family)
520 g_object_unref (face);
521 g_object_unref (family);
525 desc = pango_font_face_describe (face);
526 pango_font_description_set_size (desc, fontsel->priv->size);
527 gtk_widget_override_font (fontsel->priv->preview, desc);
529 pango_font_face_list_sizes (face, &sizes, &n_sizes);
530 /* It seems not many fonts actually have a sane set of sizes */
531 for (i=0; i<n_sizes; i++)
532 sizes[i] = sizes[i] / PANGO_SCALE;
534 set_range_marks (fontsel->priv, fontsel->priv->size_slider, sizes, n_sizes);
536 gtk_font_selection_ref_family (fontsel, family);
537 gtk_font_selection_ref_face (fontsel, face);
539 #ifndef GTK_DISABLE_DEPRECATED
540 if (fontsel->priv->_font_model)
541 update_font_list_selection (fontsel);
544 font_str = pango_font_description_to_string (desc);
545 g_object_set (fontsel, "font-name", font_str, NULL);
548 g_object_unref ((gpointer)family);
549 g_object_unref ((gpointer)face);
550 pango_font_description_free(desc);
555 zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
557 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
559 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
561 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
562 gtk_adjustment_set_value (adj,
563 gtk_adjustment_get_value (adj) +
564 gtk_adjustment_get_step_increment (adj));
565 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
566 gtk_adjustment_set_value (adj,
567 gtk_adjustment_get_value (adj) -
568 gtk_adjustment_get_step_increment (adj));
573 gtk_font_selection_init (GtkFontSelection *fontsel)
575 GtkFontSelectionPrivate *priv;
576 PangoFontDescription *font_desc;
577 GtkWidget *scrolled_win;
578 GtkWidget *preview_and_size;
579 GtkWidget *size_controls;
581 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
582 GTK_TYPE_FONT_SELECTION,
583 GtkFontSelectionPrivate);
585 priv = fontsel->priv;
587 #ifndef GTK_DISABLE_DEPRECATED
588 priv->size_list = NULL;
589 priv->font_list = NULL;
590 priv->face_list = NULL;
592 priv->_size_model = NULL;
593 priv->_font_model = NULL;
594 priv->_face_model = NULL;
596 priv->ignore_size = FALSE;
597 priv->ignore_face = FALSE;
598 priv->ignore_font = FALSE;
599 #endif /* GTK_DISABLE_DEPRECATED */
601 /* Getting the default size */
602 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
603 priv->size = pango_font_description_get_size (font_desc);
607 gtk_widget_push_composite_child ();
609 /* Creating fundamental widgets for the private struct */
610 priv->search_entry = gtk_entry_new ();
611 priv->family_face_list = gtk_tree_view_new ();
612 priv->preview = gtk_entry_new ();
613 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
614 (gdouble) font_sizes[0],
615 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
618 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
620 /** Bootstrapping widget layout **/
621 gtk_box_set_spacing (GTK_BOX (fontsel), 6);
622 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
624 /* Main font family/face view */
625 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
626 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
627 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
628 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
629 GTK_SHADOW_ETCHED_IN);
630 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
632 /* Alignment for the preview and size controls */
633 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
635 preview_and_size = gtk_vbox_new (TRUE, 0);
636 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
637 gtk_box_set_spacing (GTK_BOX (preview_and_size), 6);
639 /* The preview entry needs a scrolled window to make sure we have a */
640 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
641 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
642 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
643 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
644 GTK_SHADOW_ETCHED_IN);
645 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
647 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
649 /* Setting the size requests for various widgets */
650 gtk_widget_set_size_request (GTK_WIDGET (fontsel), FONTSEL_WIDTH, FONTSEL_HEIGHT);
651 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
652 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
654 /* Unset the frame on the preview entry */
655 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
657 /* Packing the slider and the spin in a hbox */
658 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
659 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
660 gtk_box_set_spacing (GTK_BOX (size_controls), 6);
661 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
662 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
664 gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
666 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
668 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(preview_and_size), FALSE, TRUE, 0);
670 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
671 (gdouble)(priv->size / PANGO_SCALE));
672 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
673 (gdouble)(priv->size / PANGO_SCALE));
675 gtk_widget_show_all (GTK_WIDGET (fontsel));
676 gtk_widget_hide (GTK_WIDGET (fontsel));
678 /* Treeview column and model bootstrapping */
679 gtk_font_selection_bootstrap_fontlist (fontsel);
681 /* Set default preview text */
682 gtk_entry_set_text (GTK_ENTRY (priv->preview),
683 pango_language_get_sample_string (NULL));
685 /* Set search icon and place holder text */
686 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
687 GTK_ENTRY_ICON_SECONDARY,
689 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
691 /** Callback connections **/
692 /* Connect to callback for the live search text entry */
693 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
694 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
695 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
696 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
697 g_signal_connect (G_OBJECT (priv->search_entry),
698 "icon-press", G_CALLBACK (icon_press_cb), priv);
700 /* Size controls callbacks */
701 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
702 "value-changed", G_CALLBACK (slider_change_cb), priv);
703 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
704 "value-changed", G_CALLBACK (spin_change_cb), fontsel);
705 priv->ignore_slider = FALSE;
707 /* Font selection callback */
708 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
709 G_CALLBACK (cursor_changed_cb), fontsel);
711 /* Zoom on preview scroll*/
712 g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
713 G_CALLBACK (zoom_preview_cb), priv);
715 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
716 G_CALLBACK (zoom_preview_cb), priv);
718 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
720 /* Set default focus */
721 gtk_widget_pop_composite_child();
725 * gtk_font_selection_new:
727 * Creates a new #GtkFontSelection.
729 * Return value: a new #GtkFontSelection
732 gtk_font_selection_new (void)
734 GtkFontSelection *fontsel;
736 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
738 return GTK_WIDGET (fontsel);
742 cmp_families (const void *a, const void *b)
744 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
745 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
747 return g_utf8_collate (a_name, b_name);
751 populate_list (GtkTreeView* treeview, GtkListStore* model)
753 GtkStyleContext *style_context;
757 PangoFontDescription *default_font;
759 GtkTreeIter match_row;
763 PangoFontFamily **families;
765 GString *tmp = g_string_new (NULL);
766 GString *family_and_face = g_string_new (NULL);
768 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
772 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
774 gtk_list_store_clear (model);
776 /* Get row header font color */
777 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
778 gtk_style_context_get_color (style_context,
779 GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_INSENSITIVE,
782 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
783 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
784 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
785 color_string = pango_color_to_string (&p_color);
788 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
791 /* Iterate over families and faces */
792 for (i=0; i<n_families; i++)
795 PangoFontFace **faces;
798 const gchar *fam_name = pango_font_family_get_name (families[i]);
800 pango_font_family_list_faces (families[i], &faces, &n_faces);
802 for (j=0; j<n_faces; j++)
804 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
805 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
806 gchar *font_desc = pango_font_description_to_string (pango_desc);
808 /* foreground_color, family_name, face_name, desc, sample string */
809 g_string_printf (family_and_face, "%s %s",
813 g_string_printf (tmp, ROW_FORMAT_STRING,
815 family_and_face->str,
817 pango_language_get_sample_string (NULL));
819 gtk_list_store_append (model, &iter);
820 gtk_list_store_set (model, &iter,
821 FAMILY_COLUMN, families[i],
822 FACE_COLUMN, faces[j],
823 PREVIEW_TITLE_COLUMN, family_and_face->str,
824 PREVIEW_TEXT_COLUMN, tmp->str,
827 /* Select the first font or the default font/face from the style context */
828 if ((i == 0 && j == 0) ||
829 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
832 pango_font_description_free(pango_desc);
839 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
843 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
844 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
845 gtk_tree_path_free(path);
848 g_string_free (family_and_face, TRUE);
849 g_string_free (tmp, TRUE);
850 g_free (color_string);
855 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
857 gboolean result = FALSE;
858 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
860 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
862 gchar *font_name_casefold;
863 gchar *search_text_casefold;
865 gtk_tree_model_get (model, iter,
866 PREVIEW_TITLE_COLUMN, &font_name,
869 /* Covering some corner cases to speed up the result */
870 if (font_name == NULL ||
871 strlen (search_text) > strlen (font_name))
876 if (strlen (search_text) == 0)
882 font_name_casefold = g_utf8_casefold (font_name, -1);
883 search_text_casefold = g_utf8_casefold (search_text, -1);
885 if (g_strrstr (font_name_casefold, search_text_casefold))
888 g_free (search_text_casefold);
889 g_free (font_name_casefold);
895 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
897 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
898 GtkCellRenderer *cell;
899 GtkTreeViewColumn *col;
901 fontsel->priv->model = gtk_list_store_new (4,
902 PANGO_TYPE_FONT_FAMILY,
903 PANGO_TYPE_FONT_FACE,
907 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
909 g_object_unref (fontsel->priv->model);
911 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
913 (gpointer)fontsel->priv,
916 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
917 g_object_unref (fontsel->priv->filter);
919 gtk_tree_view_set_rules_hint (treeview, TRUE);
920 gtk_tree_view_set_headers_visible (treeview, FALSE);
922 cell = gtk_cell_renderer_text_new ();
923 col = gtk_tree_view_column_new_with_attributes ("Family",
925 "markup", PREVIEW_TEXT_COLUMN,
928 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
930 gtk_tree_view_append_column (treeview, col);
932 populate_list (treeview, fontsel->priv->model);
937 gtk_font_selection_finalize (GObject *object)
939 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
941 gtk_font_selection_ref_family (fontsel, NULL);
942 gtk_font_selection_ref_face (fontsel, NULL);
944 #ifndef GTK_DISABLE_DEPRECATED
945 if (fontsel->priv->size_list)
947 g_object_unref (fontsel->priv->size_list);
948 g_object_unref (fontsel->priv->font_list);
949 g_object_unref (fontsel->priv->face_list);
953 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
958 gtk_font_selection_screen_changed (GtkWidget *widget,
959 GdkScreen *previous_screen)
965 gtk_font_selection_style_updated (GtkWidget *widget)
967 /*GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);*/
973 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
974 PangoFontFamily *family)
976 GtkFontSelectionPrivate *priv = fontsel->priv;
979 family = g_object_ref (family);
981 g_object_unref (priv->family);
982 priv->family = family;
986 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
989 GtkFontSelectionPrivate *priv = fontsel->priv;
992 face = g_object_ref (face);
994 g_object_unref (priv->face);
998 #ifndef GTK_DISABLE_DEPRECATED
1000 populate_font_model (GtkFontSelection *fontsel)
1003 PangoFontFamily **families;
1004 GtkFontSelectionPrivate *priv = fontsel->priv;
1007 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
1011 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
1013 gtk_list_store_clear (priv->_font_model);
1015 for (i=0; i<n_families; i++)
1019 gtk_list_store_append (priv->_font_model, &iter);
1020 gtk_list_store_set (priv->_font_model, &iter,
1022 1, pango_font_family_get_name (families[i]),
1025 !strcmp (pango_font_family_get_name (families[i]),
1026 pango_font_family_get_name (priv->family)))
1028 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model),
1032 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1033 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv),
1037 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv),
1038 path, NULL, FALSE, 0.0, 0.0);
1039 gtk_tree_path_free (path);
1048 update_font_list_selection (GtkFontSelection *fontsel)
1052 GtkFontSelectionPrivate *priv = fontsel->priv;
1055 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_font_model), &iter);
1060 gtk_tree_model_get (GTK_TREE_MODEL (priv->_font_model), &iter,
1063 if (strcmp (family_name, pango_font_family_get_name (priv->family)))
1065 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_font_model), &iter);
1066 g_free (family_name);
1069 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_font_model), &iter);
1073 g_free (family_name);
1077 tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1079 priv->ignore_font = TRUE;
1080 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1081 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv), path, NULL, FALSE, 0.0, 0.0);
1083 /* Free resources */
1084 gtk_tree_path_free (path);
1085 g_free (family_name);
1089 update_face_model (fontsel, FALSE);
1093 update_face_model (GtkFontSelection *fontsel, gboolean first)
1095 GtkFontSelectionPrivate *priv = fontsel->priv;
1096 PangoFontFace **faces;
1097 PangoFontDescription *desc;
1101 pango_font_family_list_faces (priv->family, &faces, &n_faces);
1103 gtk_list_store_clear (priv->_face_model);
1105 for (i=0; i<n_faces; i++)
1109 gtk_list_store_append (priv->_face_model, &iter);
1110 gtk_list_store_set (priv->_face_model, &iter,
1112 1, pango_font_face_get_face_name (faces[i]),
1115 if ((!first && faces[i] == priv->face) ||
1121 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model), &iter);
1125 tv = gtk_bin_get_child (GTK_BIN (priv->face_list));
1128 priv->ignore_face = TRUE;
1130 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1132 gtk_tree_path_free (path);
1135 gtk_font_selection_ref_face (fontsel, faces[i]);
1139 desc = pango_font_face_describe (priv->face);
1140 pango_font_description_set_size (desc, priv->size);
1141 font_str = pango_font_description_to_string (desc);
1142 g_object_set (fontsel, "font-name", font_str, NULL);
1144 update_size_list_selection (fontsel);
1146 pango_font_description_free (desc);
1152 update_size_list_selection (GtkFontSelection *fontsel)
1156 GtkFontSelectionPrivate *priv = fontsel->priv;
1157 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->size_list));
1159 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (tv)));
1161 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_size_model), &iter);
1165 gtk_tree_model_get (GTK_TREE_MODEL (priv->_size_model), &iter,
1169 if (size * PANGO_SCALE == priv->size)
1174 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_size_model), &iter);
1179 priv->ignore_size = TRUE;
1180 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1182 gtk_tree_path_free (path);
1185 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_size_model), &iter);
1191 select_family_and_face (GtkFontSelection *fontsel)
1195 GtkFontSelectionPrivate *priv = fontsel->priv;
1196 PangoFontFace *face;
1197 PangoFontFamily *family;
1199 gtk_entry_set_text (GTK_ENTRY (priv->search_entry), "");
1201 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1204 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1206 FAMILY_COLUMN, &family,
1209 if (face == priv->face && family == priv->family)
1213 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter), &iter);
1216 g_object_unref (face);
1217 g_object_unref (family);
1221 priv->ignore_size = TRUE;
1222 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list), path, NULL, FALSE);
1224 gtk_tree_path_free (path);
1225 g_object_unref (face);
1226 g_object_unref (family);
1230 g_object_unref (face);
1231 g_object_unref (family);
1232 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter);
1238 family_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1243 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1244 PangoFontFamily *font;
1246 if (fontsel->priv->ignore_font)
1248 fontsel->priv->ignore_font = FALSE;
1252 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->font_list));
1253 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1258 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_font_model),
1262 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_font_model), &iter,
1266 gtk_font_selection_ref_family (fontsel, font);
1267 update_face_model (fontsel, TRUE);
1269 fontsel->priv->ignore_font = TRUE;
1270 select_family_and_face (fontsel);
1272 gtk_tree_path_free (path);
1273 g_object_unref (font);
1277 face_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1282 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1283 PangoFontFace *face;
1285 if (fontsel->priv->ignore_face)
1287 fontsel->priv->ignore_face = FALSE;
1291 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->face_list));
1292 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1297 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_face_model),
1301 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_face_model), &iter,
1305 gtk_font_selection_ref_face (fontsel, face);
1307 fontsel->priv->ignore_face = TRUE;
1308 select_family_and_face (fontsel);
1310 gtk_tree_path_free (path);
1311 g_object_unref (face);
1315 size_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1320 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1323 if (fontsel->priv->ignore_size)
1325 fontsel->priv->ignore_size = FALSE;
1329 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->size_list));
1330 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1335 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_size_model),
1339 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_size_model), &iter,
1343 gtk_spin_button_set_value (GTK_SPIN_BUTTON (fontsel->priv->size_spin), value);
1344 gtk_tree_path_free (path);
1348 initialize_deprecated_widgets (GtkFontSelection *fontsel)
1350 GtkTreeViewColumn *col;
1351 GtkFontSelectionPrivate *priv = fontsel->priv;
1353 GtkWidget *size_list;
1354 GtkWidget *font_list;
1355 GtkWidget *face_list;
1357 priv->_size_model = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING);
1358 priv->_font_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FAMILY, G_TYPE_STRING);
1359 priv->_face_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FACE, G_TYPE_STRING);
1361 size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_size_model));
1362 font_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_font_model));
1363 face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_face_model));
1365 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (size_list), FALSE);
1366 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (font_list), FALSE);
1367 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (face_list), FALSE);
1369 g_object_unref (priv->_size_model);
1370 g_object_unref (priv->_font_model);
1371 g_object_unref (priv->_face_model);
1373 col = gtk_tree_view_column_new_with_attributes ("Size",
1374 gtk_cell_renderer_text_new (),
1377 gtk_tree_view_append_column (GTK_TREE_VIEW (size_list), col);
1379 col = gtk_tree_view_column_new_with_attributes ("Family",
1380 gtk_cell_renderer_text_new (),
1383 gtk_tree_view_append_column (GTK_TREE_VIEW (font_list), col);
1385 col = gtk_tree_view_column_new_with_attributes ("Face",
1386 gtk_cell_renderer_text_new (),
1389 gtk_tree_view_append_column (GTK_TREE_VIEW (face_list), col);
1392 priv->font_list = gtk_scrolled_window_new (NULL, NULL);
1393 priv->face_list = gtk_scrolled_window_new (NULL, NULL);
1394 priv->size_list = gtk_scrolled_window_new (NULL, NULL);
1396 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->font_list),
1397 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1398 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->face_list),
1399 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1400 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->size_list),
1401 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1403 gtk_container_add (GTK_CONTAINER (priv->font_list), font_list);
1404 gtk_container_add (GTK_CONTAINER (priv->face_list), face_list);
1405 gtk_container_add (GTK_CONTAINER (priv->size_list), size_list);
1407 g_signal_connect (G_OBJECT (font_list), "cursor-changed",
1408 G_CALLBACK (family_list_cursor_changed_cb), fontsel);
1410 g_signal_connect (G_OBJECT (face_list), "cursor-changed",
1411 G_CALLBACK (face_list_cursor_changed_cb), fontsel);
1413 g_signal_connect (G_OBJECT (size_list), "cursor-changed",
1414 G_CALLBACK (size_list_cursor_changed_cb), fontsel);
1416 populate_font_model (fontsel);
1417 cursor_changed_cb (GTK_TREE_VIEW (priv->family_face_list), fontsel);
1420 #endif /* GTK_DISABLE_DEPRECATED */
1422 /*****************************************************************************
1423 * These functions are the main public interface for getting/setting the font.
1424 *****************************************************************************/
1427 * gtk_font_selection_get_family:
1428 * @fontsel: a #GtkFontSelection
1430 * Gets the #PangoFontFamily representing the selected font family.
1432 * Return value: (transfer none): A #PangoFontFamily representing the
1433 * selected font family. Font families are a collection of font
1434 * faces. The returned object is owned by @fontsel and must not
1435 * be modified or freed.
1440 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1442 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1444 return fontsel->priv->family;
1448 * gtk_font_selection_get_face:
1449 * @fontsel: a #GtkFontSelection
1451 * Gets the #PangoFontFace representing the selected font group
1452 * details (i.e. family, slant, weight, width, etc).
1454 * Return value: (transfer none): A #PangoFontFace representing the
1455 * selected font group details. The returned object is owned by
1456 * @fontsel and must not be modified or freed.
1461 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1463 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1465 return fontsel->priv->face;
1469 * gtk_font_selection_get_size:
1470 * @fontsel: a #GtkFontSelection
1472 * The selected font size.
1474 * Return value: A n integer representing the selected font size,
1475 * or -1 if no font size is selected.
1480 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1482 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1484 return fontsel->priv->size;
1488 * gtk_font_selection_get_font_name:
1489 * @fontsel: a #GtkFontSelection
1491 * Gets the currently-selected font name.
1493 * Note that this can be a different string than what you set with
1494 * gtk_font_selection_set_font_name(), as the font selection widget may
1495 * normalize font names and thus return a string with a different structure.
1496 * For example, "Helvetica Italic Bold 12" could be normalized to
1497 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1498 * if you want to compare two font descriptions.
1500 * Return value: (transfer full) (allow-none): A string with the name of the
1501 * current font, or %NULL if no font is selected. You must free this
1502 * string with g_free().
1505 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1507 if (!fontsel->priv->family)
1510 return g_strdup (pango_font_family_get_name (fontsel->priv->family));
1513 /* This sets the current font, then selecting the appropriate list rows. */
1516 * gtk_font_selection_set_font_name:
1517 * @fontsel: a #GtkFontSelection
1518 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1520 * Sets the currently-selected font.
1522 * Note that the @fontsel needs to know the screen in which it will appear
1523 * for this to work; this can be guaranteed by simply making sure that the
1524 * @fontsel is inserted in a toplevel window before you call this function.
1526 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1527 * such font exists or if the @fontsel doesn't belong to a particular
1531 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1532 const gchar *fontname)
1535 PangoFontFamily *family = NULL;
1536 PangoFontFace *face = NULL;
1537 PangoFontDescription *new_desc;
1540 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1546 * gtk_font_selection_get_preview_text:
1547 * @fontsel: a #GtkFontSelection
1549 * Gets the text displayed in the preview area.
1551 * Return value: the text displayed in the preview area.
1552 * This string is owned by the widget and should not be
1555 G_CONST_RETURN gchar*
1556 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1563 * gtk_font_selection_set_preview_text:
1564 * @fontsel: a #GtkFontSelection
1565 * @text: the text to display in the preview area
1567 * Sets the text displayed in the preview area.
1568 * The @text is used to show how the selected font looks.
1571 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1575 GtkFontSelectionPrivate *priv;
1577 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1578 g_return_if_fail (text != NULL);
1580 priv = fontsel->priv;
1584 #ifndef GTK_DISABLE_DEPRECATED
1587 * gtk_font_selection_get_family_list:
1588 * @fontsel: a #GtkFontSelection
1590 * This returns the #GtkTreeView that lists font families, for
1591 * example, 'Sans', 'Serif', etc.
1593 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1598 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1600 GtkFontSelectionPrivate *priv = fontsel->priv;
1601 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1602 if (!priv->font_list)
1603 initialize_deprecated_widgets (fontsel);
1605 return priv->font_list;
1609 * gtk_font_selection_get_face_list:
1610 * @fontsel: a #GtkFontSelection
1612 * This returns the #GtkTreeView which lists all styles available for
1613 * the selected font. For example, 'Regular', 'Bold', etc.
1615 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1620 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1622 GtkFontSelectionPrivate *priv = fontsel->priv;
1623 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1624 if (!priv->face_list)
1625 initialize_deprecated_widgets (fontsel);
1627 return priv->face_list;
1631 * gtk_font_selection_get_size_entry:
1632 * @fontsel: a #GtkFontSelection
1634 * This returns the #GtkEntry used to allow the user to edit the font
1635 * number manually instead of selecting it from the list of font sizes.
1637 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1642 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1644 GtkFontSelectionPrivate *priv = fontsel->priv;
1645 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1647 return priv->size_spin;
1651 * gtk_font_selection_get_size_list:
1652 * @fontsel: a #GtkFontSelection
1654 * This returns the #GtkTreeeView used to list font sizes.
1656 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1661 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1663 GtkFontSelectionPrivate *priv = fontsel->priv;
1664 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1665 if (!priv->size_list)
1666 initialize_deprecated_widgets (fontsel);
1668 return priv->size_list;
1672 * gtk_font_selection_get_preview_entry:
1673 * @fontsel: a #GtkFontSelection
1675 * This returns the #GtkEntry used to display the font as a preview.
1677 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1682 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1684 GtkFontSelectionPrivate *priv = fontsel->priv;
1685 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1687 return priv->preview;
1690 #endif /* GTK_DISABLE_DEPRECATED */
1693 * SECTION:gtkfontseldlg
1694 * @Short_description: A dialog box for selecting fonts
1695 * @Title: GtkFontSelectionDialog
1696 * @See_also: #GtkFontSelection, #GtkDialog
1698 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1700 * To set the font which is initially selected, use
1701 * gtk_font_selection_dialog_set_font_name().
1703 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1705 * To change the text which is shown in the preview area, use
1706 * gtk_font_selection_dialog_set_preview_text().
1708 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1709 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1710 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1711 * exposes the embedded #GtkFontSelection as internal child with the
1712 * name "font_selection". It also exposes the buttons with the names
1713 * "ok_button", "cancel_button" and "apply_button".
1717 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1718 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1719 GtkBuilder *builder,
1720 const gchar *childname);
1722 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1724 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1725 gtk_font_selection_dialog_buildable_interface_init))
1727 static GtkBuildableIface *parent_buildable_iface;
1730 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1732 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1736 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1738 GtkFontSelectionDialogPrivate *priv;
1739 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1740 GtkWidget *action_area, *content_area;
1742 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1743 GTK_TYPE_FONT_SELECTION_DIALOG,
1744 GtkFontSelectionDialogPrivate);
1745 priv = fontseldiag->priv;
1747 content_area = gtk_dialog_get_content_area (dialog);
1748 action_area = gtk_dialog_get_action_area (dialog);
1750 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1751 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1752 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1753 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1755 gtk_widget_push_composite_child ();
1757 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1759 /* Create the content area */
1760 priv->fontsel = gtk_font_selection_new ();
1761 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1762 gtk_widget_show (priv->fontsel);
1763 gtk_box_pack_start (GTK_BOX (content_area),
1764 priv->fontsel, TRUE, TRUE, 0);
1766 /* Create the action area */
1767 priv->cancel_button = gtk_dialog_add_button (dialog,
1769 GTK_RESPONSE_CANCEL);
1771 priv->apply_button = gtk_dialog_add_button (dialog,
1773 GTK_RESPONSE_APPLY);
1774 gtk_widget_hide (priv->apply_button);
1776 priv->ok_button = gtk_dialog_add_button (dialog,
1779 gtk_widget_grab_default (priv->ok_button);
1781 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1784 GTK_RESPONSE_CANCEL,
1787 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1788 _("Font Selection"));
1790 gtk_widget_pop_composite_child ();
1794 * gtk_font_selection_dialog_new:
1795 * @title: the title of the dialog window
1797 * Creates a new #GtkFontSelectionDialog.
1799 * Return value: a new #GtkFontSelectionDialog
1802 gtk_font_selection_dialog_new (const gchar *title)
1804 GtkFontSelectionDialog *fontseldiag;
1806 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1809 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1811 return GTK_WIDGET (fontseldiag);
1815 * gtk_font_selection_dialog_get_font_selection:
1816 * @fsd: a #GtkFontSelectionDialog
1818 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1820 * Returns: (transfer none): the embedded #GtkFontSelection
1825 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1827 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1829 return fsd->priv->fontsel;
1834 * gtk_font_selection_dialog_get_ok_button:
1835 * @fsd: a #GtkFontSelectionDialog
1837 * Gets the 'OK' button.
1839 * Return value: (transfer none): the #GtkWidget used in the dialog
1840 * for the 'OK' button.
1845 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1847 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1849 return fsd->priv->ok_button;
1853 * gtk_font_selection_dialog_get_cancel_button:
1854 * @fsd: a #GtkFontSelectionDialog
1856 * Gets the 'Cancel' button.
1858 * Return value: (transfer none): the #GtkWidget used in the dialog
1859 * for the 'Cancel' button.
1864 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1866 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1868 return fsd->priv->cancel_button;
1872 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1874 parent_buildable_iface = g_type_interface_peek_parent (iface);
1875 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1879 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1880 GtkBuilder *builder,
1881 const gchar *childname)
1883 GtkFontSelectionDialogPrivate *priv;
1885 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1887 if (g_strcmp0 (childname, "ok_button") == 0)
1888 return G_OBJECT (priv->ok_button);
1889 else if (g_strcmp0 (childname, "cancel_button") == 0)
1890 return G_OBJECT (priv->cancel_button);
1891 else if (g_strcmp0 (childname, "apply_button") == 0)
1892 return G_OBJECT (priv->apply_button);
1893 else if (g_strcmp0 (childname, "font_selection") == 0)
1894 return G_OBJECT (priv->fontsel);
1896 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1900 * gtk_font_selection_dialog_get_font_name:
1901 * @fsd: a #GtkFontSelectionDialog
1903 * Gets the currently-selected font name.
1905 * Note that this can be a different string than what you set with
1906 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1907 * may normalize font names and thus return a string with a different
1908 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1909 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1910 * if you want to compare two font descriptions.
1912 * Return value: A string with the name of the current font, or %NULL if no
1913 * font is selected. You must free this string with g_free().
1916 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1918 GtkFontSelectionDialogPrivate *priv;
1920 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1924 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1928 * gtk_font_selection_dialog_set_font_name:
1929 * @fsd: a #GtkFontSelectionDialog
1930 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1932 * Sets the currently selected font.
1934 * Return value: %TRUE if the font selected in @fsd is now the
1935 * @fontname specified, %FALSE otherwise.
1938 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1939 const gchar *fontname)
1941 GtkFontSelectionDialogPrivate *priv;
1943 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1944 g_return_val_if_fail (fontname, FALSE);
1948 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1952 * gtk_font_selection_dialog_get_preview_text:
1953 * @fsd: a #GtkFontSelectionDialog
1955 * Gets the text displayed in the preview area.
1957 * Return value: the text displayed in the preview area.
1958 * This string is owned by the widget and should not be
1961 G_CONST_RETURN gchar*
1962 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1964 GtkFontSelectionDialogPrivate *priv;
1966 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1970 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1974 * gtk_font_selection_dialog_set_preview_text:
1975 * @fsd: a #GtkFontSelectionDialog
1976 * @text: the text to display in the preview area
1978 * Sets the text displayed in the preview area.
1981 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1984 GtkFontSelectionDialogPrivate *priv;
1986 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1987 g_return_if_fail (text != NULL);
1991 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);