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);
397 priv->ignore_size = TRUE;
398 update_size_list_selection (fontsel);
400 gtk_widget_queue_draw (priv->preview);
404 set_range_marks (GtkFontSelectionPrivate *priv,
405 GtkWidget* size_slider,
415 sizes = (gint*)font_sizes;
416 length = FONT_SIZES_LENGTH;
419 gtk_scale_clear_marks (GTK_SCALE (size_slider));
421 adj = gtk_range_get_adjustment(GTK_RANGE (size_slider));
423 gtk_adjustment_set_lower (adj, (gdouble) sizes[0]);
424 gtk_adjustment_set_upper (adj, (gdouble) sizes[length-1]);
426 value = gtk_adjustment_get_value (adj);
427 if (value > (gdouble) sizes[length-1])
429 gtk_adjustment_set_value (adj, (gdouble) sizes[length-1]);
430 priv->ignore_slider = TRUE;
432 else if (value < (gdouble) sizes[0])
434 gtk_adjustment_set_value (adj, (gdouble) sizes[0]);
435 priv->ignore_slider = TRUE;
438 #ifndef GTK_DISABLE_DEPRECATED
439 if (!priv->_size_model)
441 for (i=0; i<length; i++)
442 gtk_scale_add_mark (GTK_SCALE (size_slider),
444 GTK_POS_BOTTOM, NULL);
448 GString *size_str = g_string_new (NULL);
449 gtk_list_store_clear (priv->_size_model);
451 for (i=0; i<length; i++)
455 g_string_printf (size_str, "%d", sizes[i]);
457 gtk_scale_add_mark (GTK_SCALE (size_slider),
459 GTK_POS_BOTTOM, NULL);
461 gtk_list_store_append (priv->_size_model, &iter);
462 gtk_list_store_set (priv->_size_model, &iter,
467 g_string_free (size_str, TRUE);
470 for (i=0; i<length; i++)
471 gtk_scale_add_mark (GTK_SCALE (size_slider),
473 GTK_POS_BOTTOM, NULL);
478 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
480 PangoFontFamily *family;
482 PangoFontDescription *desc;
488 GtkTreePath *path = gtk_tree_path_new ();
490 GtkFontSelection *fontsel = (GtkFontSelection*)data;
492 gtk_tree_view_get_cursor (treeview, &path, NULL);
497 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->filter), &iter, path))
499 gtk_tree_path_free (path);
504 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->filter), &iter,
506 FAMILY_COLUMN, &family,
509 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
511 gtk_tree_path_free (path);
514 if (!face || !family)
516 g_object_unref (face);
517 g_object_unref (family);
521 desc = pango_font_face_describe (face);
522 pango_font_description_set_size (desc, fontsel->priv->size);
523 gtk_widget_override_font (fontsel->priv->preview, desc);
525 pango_font_face_list_sizes (face, &sizes, &n_sizes);
526 /* It seems not many fonts actually have a sane set of sizes */
527 for (i=0; i<n_sizes; i++)
528 sizes[i] = sizes[i] / PANGO_SCALE;
530 set_range_marks (fontsel->priv, fontsel->priv->size_slider, sizes, n_sizes);
532 gtk_font_selection_ref_family (fontsel, family);
533 gtk_font_selection_ref_face (fontsel, face);
535 #ifndef GTK_DISABLE_DEPRECATED
536 if (fontsel->priv->_font_model)
537 update_font_list_selection (fontsel);
541 g_object_unref ((gpointer)family);
542 g_object_unref ((gpointer)face);
543 pango_font_description_free(desc);
547 zoom_preview_cb (GtkWidget *scrolled_window, GdkEventScroll *event, gpointer data)
549 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
551 GtkAdjustment *adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin));
553 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT)
554 gtk_adjustment_set_value (adj,
555 gtk_adjustment_get_value (adj) +
556 gtk_adjustment_get_step_increment (adj));
557 else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT)
558 gtk_adjustment_set_value (adj,
559 gtk_adjustment_get_value (adj) -
560 gtk_adjustment_get_step_increment (adj));
565 gtk_font_selection_init (GtkFontSelection *fontsel)
567 GtkFontSelectionPrivate *priv;
568 PangoFontDescription *font_desc;
569 GtkWidget *scrolled_win;
570 GtkWidget *preview_and_size;
571 GtkWidget *size_controls;
573 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
574 GTK_TYPE_FONT_SELECTION,
575 GtkFontSelectionPrivate);
577 priv = fontsel->priv;
579 #ifndef GTK_DISABLE_DEPRECATED
580 priv->size_list = NULL;
581 priv->font_list = NULL;
582 priv->face_list = NULL;
584 priv->_size_model = NULL;
585 priv->_font_model = NULL;
586 priv->_face_model = NULL;
588 priv->ignore_size = FALSE;
589 priv->ignore_face = FALSE;
590 priv->ignore_font = FALSE;
591 #endif /* GTK_DISABLE_DEPRECATED */
593 /* Getting the default size */
594 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
595 priv->size = pango_font_description_get_size (font_desc);
599 gtk_widget_push_composite_child ();
601 /* Creating fundamental widgets for the private struct */
602 priv->search_entry = gtk_entry_new ();
603 priv->family_face_list = gtk_tree_view_new ();
604 priv->preview = gtk_entry_new ();
605 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
606 (gdouble) font_sizes[0],
607 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
610 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
612 /** Bootstrapping widget layout **/
613 gtk_box_set_spacing (GTK_BOX (fontsel), 6);
614 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
616 /* Main font family/face view */
617 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
618 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
619 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
620 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
621 GTK_SHADOW_ETCHED_IN);
622 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
624 /* Alignment for the preview and size controls */
625 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
627 preview_and_size = gtk_vbox_new (TRUE, 0);
628 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
629 gtk_box_set_spacing (GTK_BOX (preview_and_size), 6);
631 /* The preview entry needs a scrolled window to make sure we have a */
632 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
633 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
634 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
635 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
636 GTK_SHADOW_ETCHED_IN);
637 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
639 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
641 /* Setting the size requests for various widgets */
642 gtk_widget_set_size_request (GTK_WIDGET (fontsel), FONTSEL_WIDTH, FONTSEL_HEIGHT);
643 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
644 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
646 /* Unset the frame on the preview entry */
647 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
649 /* Packing the slider and the spin in a hbox */
650 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
651 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
652 gtk_box_set_spacing (GTK_BOX (size_controls), 6);
653 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
654 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
656 gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
658 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
660 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(preview_and_size), FALSE, TRUE, 0);
662 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
663 (gdouble)(priv->size / PANGO_SCALE));
664 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
665 (gdouble)(priv->size / PANGO_SCALE));
667 gtk_widget_show_all (GTK_WIDGET (fontsel));
668 gtk_widget_hide (GTK_WIDGET (fontsel));
670 /* Treeview column and model bootstrapping */
671 gtk_font_selection_bootstrap_fontlist (fontsel);
673 /* Set default preview text */
674 gtk_entry_set_text (GTK_ENTRY (priv->preview),
675 pango_language_get_sample_string (NULL));
677 /* Set search icon and place holder text */
678 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
679 GTK_ENTRY_ICON_SECONDARY,
681 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
683 /** Callback connections **/
684 /* Connect to callback for the live search text entry */
685 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
686 "deleted-text", G_CALLBACK (deleted_text_cb), priv);
687 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
688 "inserted-text", G_CALLBACK (inserted_text_cb), priv);
689 g_signal_connect (G_OBJECT (priv->search_entry),
690 "icon-press", G_CALLBACK (icon_press_cb), priv);
692 /* Size controls callbacks */
693 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
694 "value-changed", G_CALLBACK (slider_change_cb), priv);
695 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
696 "value-changed", G_CALLBACK (spin_change_cb), fontsel);
697 priv->ignore_slider = FALSE;
699 /* Font selection callback */
700 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
701 G_CALLBACK (cursor_changed_cb), fontsel);
703 /* Zoom on preview scroll*/
704 g_signal_connect (G_OBJECT (scrolled_win), "scroll-event",
705 G_CALLBACK (zoom_preview_cb), priv);
707 g_signal_connect (G_OBJECT (priv->size_slider), "scroll-event",
708 G_CALLBACK (zoom_preview_cb), priv);
710 set_range_marks (priv, priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
712 /* Set default focus */
713 gtk_widget_pop_composite_child();
717 * gtk_font_selection_new:
719 * Creates a new #GtkFontSelection.
721 * Return value: a new #GtkFontSelection
724 gtk_font_selection_new (void)
726 GtkFontSelection *fontsel;
728 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
730 return GTK_WIDGET (fontsel);
734 cmp_families (const void *a, const void *b)
736 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
737 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
739 return g_utf8_collate (a_name, b_name);
743 populate_list (GtkTreeView* treeview, GtkListStore* model)
745 GtkStyleContext *style_context;
749 PangoFontDescription *default_font;
751 GtkTreeIter match_row;
755 PangoFontFamily **families;
757 GString *tmp = g_string_new (NULL);
758 GString *family_and_face = g_string_new (NULL);
760 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
764 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
766 gtk_list_store_clear (model);
768 /* Get row header font color */
769 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
770 gtk_style_context_get_color (style_context,
771 GTK_STATE_FLAG_NORMAL | GTK_STATE_FLAG_INSENSITIVE,
774 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
775 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
776 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
777 color_string = pango_color_to_string (&p_color);
780 default_font = (PangoFontDescription*) gtk_style_context_get_font (style_context,
783 /* Iterate over families and faces */
784 for (i=0; i<n_families; i++)
787 PangoFontFace **faces;
790 const gchar *fam_name = pango_font_family_get_name (families[i]);
792 pango_font_family_list_faces (families[i], &faces, &n_faces);
794 for (j=0; j<n_faces; j++)
796 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
797 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
798 gchar *font_desc = pango_font_description_to_string (pango_desc);
800 /* foreground_color, family_name, face_name, desc, sample string */
801 g_string_printf (family_and_face, "%s %s",
805 g_string_printf (tmp, ROW_FORMAT_STRING,
807 family_and_face->str,
809 pango_language_get_sample_string (NULL));
811 gtk_list_store_append (model, &iter);
812 gtk_list_store_set (model, &iter,
813 FAMILY_COLUMN, families[i],
814 FACE_COLUMN, faces[j],
815 PREVIEW_TITLE_COLUMN, family_and_face->str,
816 PREVIEW_TEXT_COLUMN, tmp->str,
819 /* Select the first font or the default font/face from the style context */
820 if ((i == 0 && j == 0) ||
821 (!strcmp (fam_name, pango_font_description_get_family (default_font)) && j == 0))
824 pango_font_description_free(pango_desc);
831 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
835 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
836 gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0.0, 0.0);
837 gtk_tree_path_free(path);
840 g_string_free (family_and_face, TRUE);
841 g_string_free (tmp, TRUE);
842 g_free (color_string);
847 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
849 gboolean result = FALSE;
850 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
852 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
854 gchar *font_name_casefold;
855 gchar *search_text_casefold;
857 gtk_tree_model_get (model, iter,
858 PREVIEW_TITLE_COLUMN, &font_name,
861 /* Covering some corner cases to speed up the result */
862 if (font_name == NULL ||
863 strlen (search_text) > strlen (font_name))
868 if (strlen (search_text) == 0)
874 font_name_casefold = g_utf8_casefold (font_name, -1);
875 search_text_casefold = g_utf8_casefold (search_text, -1);
877 if (g_strrstr (font_name_casefold, search_text_casefold))
880 g_free (search_text_casefold);
881 g_free (font_name_casefold);
887 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
889 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
890 GtkCellRenderer *cell;
891 GtkTreeViewColumn *col;
893 fontsel->priv->model = gtk_list_store_new (4,
894 PANGO_TYPE_FONT_FAMILY,
895 PANGO_TYPE_FONT_FACE,
899 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
901 g_object_unref (fontsel->priv->model);
903 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
905 (gpointer)fontsel->priv,
908 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
909 g_object_unref (fontsel->priv->filter);
911 gtk_tree_view_set_rules_hint (treeview, TRUE);
912 gtk_tree_view_set_headers_visible (treeview, FALSE);
914 cell = gtk_cell_renderer_text_new ();
915 col = gtk_tree_view_column_new_with_attributes ("Family",
917 "markup", PREVIEW_TEXT_COLUMN,
920 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
922 gtk_tree_view_append_column (treeview, col);
924 populate_list (treeview, fontsel->priv->model);
929 gtk_font_selection_finalize (GObject *object)
931 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
933 gtk_font_selection_ref_family (fontsel, NULL);
934 gtk_font_selection_ref_face (fontsel, NULL);
936 #ifndef GTK_DISABLE_DEPRECATED
937 if (fontsel->priv->size_list)
939 g_object_unref (fontsel->priv->size_list);
940 g_object_unref (fontsel->priv->font_list);
941 g_object_unref (fontsel->priv->face_list);
945 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
950 gtk_font_selection_screen_changed (GtkWidget *widget,
951 GdkScreen *previous_screen)
957 gtk_font_selection_style_updated (GtkWidget *widget)
959 /*GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);*/
965 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
966 PangoFontFamily *family)
968 GtkFontSelectionPrivate *priv = fontsel->priv;
971 family = g_object_ref (family);
973 g_object_unref (priv->family);
974 priv->family = family;
978 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
981 GtkFontSelectionPrivate *priv = fontsel->priv;
984 face = g_object_ref (face);
986 g_object_unref (priv->face);
990 #ifndef GTK_DISABLE_DEPRECATED
992 populate_font_model (GtkFontSelection *fontsel)
995 PangoFontFamily **families;
996 GtkFontSelectionPrivate *priv = fontsel->priv;
999 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)),
1003 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
1005 gtk_list_store_clear (priv->_font_model);
1007 for (i=0; i<n_families; i++)
1011 gtk_list_store_append (priv->_font_model, &iter);
1012 gtk_list_store_set (priv->_font_model, &iter,
1014 1, pango_font_family_get_name (families[i]),
1017 !strcmp (pango_font_family_get_name (families[i]),
1018 pango_font_family_get_name (priv->family)))
1020 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model),
1024 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1025 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv),
1029 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv),
1030 path, NULL, FALSE, 0.0, 0.0);
1031 gtk_tree_path_free (path);
1040 update_font_list_selection (GtkFontSelection *fontsel)
1044 GtkFontSelectionPrivate *priv = fontsel->priv;
1047 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_font_model), &iter);
1052 gtk_tree_model_get (GTK_TREE_MODEL (priv->_font_model), &iter,
1055 if (strcmp (family_name, pango_font_family_get_name (priv->family)))
1057 gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_font_model), &iter);
1058 g_free (family_name);
1061 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_font_model), &iter);
1065 g_free (family_name);
1069 tv = gtk_bin_get_child (GTK_BIN (priv->font_list));
1071 priv->ignore_font = TRUE;
1072 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1073 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (tv), path, NULL, FALSE, 0.0, 0.0);
1075 /* Free resources */
1076 gtk_tree_path_free (path);
1077 g_free (family_name);
1081 update_face_model (fontsel, FALSE);
1085 update_face_model (GtkFontSelection *fontsel, gboolean first)
1087 GtkFontSelectionPrivate *priv = fontsel->priv;
1088 PangoFontFace **faces;
1091 pango_font_family_list_faces (priv->family, &faces, &n_faces);
1093 gtk_list_store_clear (priv->_face_model);
1095 for (i=0; i<n_faces; i++)
1099 gtk_list_store_append (priv->_face_model, &iter);
1100 gtk_list_store_set (priv->_face_model, &iter,
1102 1, pango_font_face_get_face_name (faces[i]),
1105 if (!first && faces[i] == priv->face ||
1111 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_face_model), &iter);
1115 tv = gtk_bin_get_child (GTK_BIN (priv->face_list));
1118 priv->ignore_face = TRUE;
1120 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1122 gtk_tree_path_free (path);
1125 gtk_font_selection_ref_face (fontsel, faces[i]);
1128 update_size_list_selection (fontsel);
1133 update_size_list_selection (GtkFontSelection *fontsel)
1137 GtkFontSelectionPrivate *priv = fontsel->priv;
1139 GtkWidget *tv = gtk_bin_get_child (GTK_BIN (priv->size_list));
1141 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (tv));
1143 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->_size_model), &iter);
1147 gtk_tree_model_get (GTK_TREE_MODEL (priv->_size_model), &iter,
1151 if (size * PANGO_SCALE == priv->size)
1156 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->_size_model), &iter);
1161 priv->ignore_size = TRUE;
1162 gtk_tree_view_set_cursor (GTK_TREE_VIEW (tv), path, NULL, FALSE);
1164 gtk_tree_path_free (path);
1167 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->_size_model), &iter);
1173 select_family_and_face (GtkFontSelection *fontsel)
1177 GtkFontSelectionPrivate *priv = fontsel->priv;
1178 PangoFontFace *face;
1179 PangoFontFamily *family;
1181 gtk_entry_set_text (priv->search_entry, "");
1183 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->filter), &iter);
1187 gtk_tree_model_get (GTK_TREE_MODEL (priv->filter), &iter,
1189 FAMILY_COLUMN, &family,
1192 if (face == priv->face && family == priv->family)
1196 path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->filter), &iter);
1199 g_object_unref (face);
1200 g_object_unref (family);
1204 priv->ignore_size = TRUE;
1205 gtk_tree_view_set_cursor (GTK_TREE_VIEW (priv->family_face_list), path, NULL, FALSE);
1207 gtk_tree_path_free (path);
1208 g_object_unref (face);
1209 g_object_unref (family);
1213 g_object_unref (face);
1214 g_object_unref (family);
1215 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (priv->filter), &iter);
1221 family_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1226 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1227 PangoFontFamily *font;
1229 if (fontsel->priv->ignore_font)
1231 fontsel->priv->ignore_font = FALSE;
1235 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->font_list));
1236 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1241 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_font_model),
1245 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_font_model), &iter,
1249 gtk_font_selection_ref_family (fontsel, font);
1250 update_face_model (fontsel, TRUE);
1252 fontsel->priv->ignore_font = TRUE;
1253 select_family_and_face (fontsel);
1255 gtk_tree_path_free (path);
1256 g_object_unref (font);
1260 face_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1265 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1266 PangoFontFace *face;
1268 if (fontsel->priv->ignore_face)
1270 fontsel->priv->ignore_face = FALSE;
1274 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->face_list));
1275 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1280 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_face_model),
1284 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_face_model), &iter,
1288 gtk_font_selection_ref_face (fontsel, face);
1290 fontsel->priv->ignore_face = TRUE;
1291 select_family_and_face (fontsel);
1293 gtk_tree_path_free (path);
1294 g_object_unref (face);
1298 size_list_cursor_changed_cb (GtkTreeView *treeview, gpointer data)
1303 GtkFontSelection *fontsel = (GtkFontSelection*)data;
1306 if (fontsel->priv->ignore_size)
1308 fontsel->priv->ignore_size = FALSE;
1312 tv = gtk_bin_get_child (GTK_BIN (fontsel->priv->size_list));
1313 gtk_tree_view_get_cursor (GTK_TREE_VIEW (tv), &path, NULL);
1318 gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->_size_model),
1322 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->_size_model), &iter,
1326 gtk_spin_button_set_value (fontsel->priv->size_spin, value);
1327 gtk_tree_path_free (path);
1331 initialize_deprecated_widgets (GtkFontSelection *fontsel)
1333 GtkTreeViewColumn *col;
1334 GtkCellRenderer *cell;
1335 GtkFontSelectionPrivate *priv = fontsel->priv;
1337 GtkWidget *size_list;
1338 GtkWidget *font_list;
1339 GtkWidget *face_list;
1341 priv->_size_model = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING);
1342 priv->_font_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FAMILY, G_TYPE_STRING);
1343 priv->_face_model = gtk_list_store_new (2, PANGO_TYPE_FONT_FACE, G_TYPE_STRING);
1345 size_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_size_model));
1346 font_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_font_model));
1347 face_list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->_face_model));
1349 g_object_unref (priv->_size_model);
1350 g_object_unref (priv->_font_model);
1351 g_object_unref (priv->_face_model);
1353 col = gtk_tree_view_column_new_with_attributes ("Size",
1354 gtk_cell_renderer_text_new (),
1357 gtk_tree_view_append_column (GTK_TREE_VIEW (size_list), col);
1359 col = gtk_tree_view_column_new_with_attributes ("Family",
1360 gtk_cell_renderer_text_new (),
1363 gtk_tree_view_append_column (GTK_TREE_VIEW (font_list), col);
1365 col = gtk_tree_view_column_new_with_attributes ("Face",
1366 gtk_cell_renderer_text_new (),
1369 gtk_tree_view_append_column (GTK_TREE_VIEW (face_list), col);
1372 priv->font_list = gtk_scrolled_window_new (NULL, NULL);
1373 priv->face_list = gtk_scrolled_window_new (NULL, NULL);
1374 priv->size_list = gtk_scrolled_window_new (NULL, NULL);
1376 gtk_container_add (GTK_CONTAINER (priv->font_list), font_list);
1377 gtk_container_add (GTK_CONTAINER (priv->face_list), face_list);
1378 gtk_container_add (GTK_CONTAINER (priv->size_list), size_list);
1380 g_signal_connect (G_OBJECT (font_list), "cursor-changed",
1381 G_CALLBACK (family_list_cursor_changed_cb), fontsel);
1383 g_signal_connect (G_OBJECT (face_list), "cursor-changed",
1384 G_CALLBACK (face_list_cursor_changed_cb), fontsel);
1386 g_signal_connect (G_OBJECT (size_list), "cursor-changed",
1387 G_CALLBACK (size_list_cursor_changed_cb), fontsel);
1389 populate_font_model (fontsel);
1390 cursor_changed_cb (priv->family_face_list, priv);
1393 #endif /* GTK_DISABLE_DEPRECATED */
1395 /*****************************************************************************
1396 * These functions are the main public interface for getting/setting the font.
1397 *****************************************************************************/
1400 * gtk_font_selection_get_family:
1401 * @fontsel: a #GtkFontSelection
1403 * Gets the #PangoFontFamily representing the selected font family.
1405 * Return value: (transfer none): A #PangoFontFamily representing the
1406 * selected font family. Font families are a collection of font
1407 * faces. The returned object is owned by @fontsel and must not
1408 * be modified or freed.
1413 gtk_font_selection_get_family (GtkFontSelection *fontsel)
1415 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1417 return fontsel->priv->family;
1421 * gtk_font_selection_get_face:
1422 * @fontsel: a #GtkFontSelection
1424 * Gets the #PangoFontFace representing the selected font group
1425 * details (i.e. family, slant, weight, width, etc).
1427 * Return value: (transfer none): A #PangoFontFace representing the
1428 * selected font group details. The returned object is owned by
1429 * @fontsel and must not be modified or freed.
1434 gtk_font_selection_get_face (GtkFontSelection *fontsel)
1436 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1438 return fontsel->priv->face;
1442 * gtk_font_selection_get_size:
1443 * @fontsel: a #GtkFontSelection
1445 * The selected font size.
1447 * Return value: A n integer representing the selected font size,
1448 * or -1 if no font size is selected.
1453 gtk_font_selection_get_size (GtkFontSelection *fontsel)
1455 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
1457 return fontsel->priv->size;
1461 * gtk_font_selection_get_font_name:
1462 * @fontsel: a #GtkFontSelection
1464 * Gets the currently-selected font name.
1466 * Note that this can be a different string than what you set with
1467 * gtk_font_selection_set_font_name(), as the font selection widget may
1468 * normalize font names and thus return a string with a different structure.
1469 * For example, "Helvetica Italic Bold 12" could be normalized to
1470 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
1471 * if you want to compare two font descriptions.
1473 * Return value: (transfer full) (allow-none): A string with the name of the
1474 * current font, or %NULL if no font is selected. You must free this
1475 * string with g_free().
1478 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
1480 if (!fontsel->priv->family)
1483 return g_strdup (pango_font_family_get_name (fontsel->priv->family));
1486 /* This sets the current font, then selecting the appropriate list rows. */
1489 * gtk_font_selection_set_font_name:
1490 * @fontsel: a #GtkFontSelection
1491 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1493 * Sets the currently-selected font.
1495 * Note that the @fontsel needs to know the screen in which it will appear
1496 * for this to work; this can be guaranteed by simply making sure that the
1497 * @fontsel is inserted in a toplevel window before you call this function.
1499 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1500 * such font exists or if the @fontsel doesn't belong to a particular
1504 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1505 const gchar *fontname)
1508 PangoFontFamily *family = NULL;
1509 PangoFontFace *face = NULL;
1510 PangoFontDescription *new_desc;
1513 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1519 * gtk_font_selection_get_preview_text:
1520 * @fontsel: a #GtkFontSelection
1522 * Gets the text displayed in the preview area.
1524 * Return value: the text displayed in the preview area.
1525 * This string is owned by the widget and should not be
1528 G_CONST_RETURN gchar*
1529 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1536 * gtk_font_selection_set_preview_text:
1537 * @fontsel: a #GtkFontSelection
1538 * @text: the text to display in the preview area
1540 * Sets the text displayed in the preview area.
1541 * The @text is used to show how the selected font looks.
1544 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1548 GtkFontSelectionPrivate *priv;
1550 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1551 g_return_if_fail (text != NULL);
1553 priv = fontsel->priv;
1557 #ifndef GTK_DISABLE_DEPRECATED
1560 * gtk_font_selection_get_family_list:
1561 * @fontsel: a #GtkFontSelection
1563 * This returns the #GtkTreeView that lists font families, for
1564 * example, 'Sans', 'Serif', etc.
1566 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1571 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
1573 GtkFontSelectionPrivate *priv = fontsel->priv;
1574 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1575 if (!priv->font_list)
1576 initialize_deprecated_widgets (fontsel);
1578 return priv->font_list;
1582 * gtk_font_selection_get_face_list:
1583 * @fontsel: a #GtkFontSelection
1585 * This returns the #GtkTreeView which lists all styles available for
1586 * the selected font. For example, 'Regular', 'Bold', etc.
1588 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1593 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
1595 GtkFontSelectionPrivate *priv = fontsel->priv;
1596 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1597 if (!priv->face_list)
1598 initialize_deprecated_widgets (fontsel);
1600 return priv->face_list;
1604 * gtk_font_selection_get_size_entry:
1605 * @fontsel: a #GtkFontSelection
1607 * This returns the #GtkEntry used to allow the user to edit the font
1608 * number manually instead of selecting it from the list of font sizes.
1610 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1615 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
1617 GtkFontSelectionPrivate *priv = fontsel->priv;
1618 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1620 return priv->size_spin;
1624 * gtk_font_selection_get_size_list:
1625 * @fontsel: a #GtkFontSelection
1627 * This returns the #GtkTreeeView used to list font sizes.
1629 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1634 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
1636 GtkFontSelectionPrivate *priv = fontsel->priv;
1637 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1638 if (!priv->size_list)
1639 initialize_deprecated_widgets (fontsel);
1641 return priv->size_list;
1645 * gtk_font_selection_get_preview_entry:
1646 * @fontsel: a #GtkFontSelection
1648 * This returns the #GtkEntry used to display the font as a preview.
1650 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
1655 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
1657 GtkFontSelectionPrivate *priv = fontsel->priv;
1658 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
1660 return priv->preview;
1663 #endif /* GTK_DISABLE_DEPRECATED */
1666 * SECTION:gtkfontseldlg
1667 * @Short_description: A dialog box for selecting fonts
1668 * @Title: GtkFontSelectionDialog
1669 * @See_also: #GtkFontSelection, #GtkDialog
1671 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1673 * To set the font which is initially selected, use
1674 * gtk_font_selection_dialog_set_font_name().
1676 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1678 * To change the text which is shown in the preview area, use
1679 * gtk_font_selection_dialog_set_preview_text().
1681 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1682 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1683 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1684 * exposes the embedded #GtkFontSelection as internal child with the
1685 * name "font_selection". It also exposes the buttons with the names
1686 * "ok_button", "cancel_button" and "apply_button".
1690 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1691 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1692 GtkBuilder *builder,
1693 const gchar *childname);
1695 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1697 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1698 gtk_font_selection_dialog_buildable_interface_init))
1700 static GtkBuildableIface *parent_buildable_iface;
1703 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1705 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1709 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1711 GtkFontSelectionDialogPrivate *priv;
1712 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1713 GtkWidget *action_area, *content_area;
1715 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1716 GTK_TYPE_FONT_SELECTION_DIALOG,
1717 GtkFontSelectionDialogPrivate);
1718 priv = fontseldiag->priv;
1720 content_area = gtk_dialog_get_content_area (dialog);
1721 action_area = gtk_dialog_get_action_area (dialog);
1723 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1724 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1725 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1726 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1728 gtk_widget_push_composite_child ();
1730 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1732 /* Create the content area */
1733 priv->fontsel = gtk_font_selection_new ();
1734 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1735 gtk_widget_show (priv->fontsel);
1736 gtk_box_pack_start (GTK_BOX (content_area),
1737 priv->fontsel, TRUE, TRUE, 0);
1739 /* Create the action area */
1740 priv->cancel_button = gtk_dialog_add_button (dialog,
1742 GTK_RESPONSE_CANCEL);
1744 priv->apply_button = gtk_dialog_add_button (dialog,
1746 GTK_RESPONSE_APPLY);
1747 gtk_widget_hide (priv->apply_button);
1749 priv->ok_button = gtk_dialog_add_button (dialog,
1752 gtk_widget_grab_default (priv->ok_button);
1754 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1757 GTK_RESPONSE_CANCEL,
1760 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1761 _("Font Selection"));
1763 gtk_widget_pop_composite_child ();
1767 * gtk_font_selection_dialog_new:
1768 * @title: the title of the dialog window
1770 * Creates a new #GtkFontSelectionDialog.
1772 * Return value: a new #GtkFontSelectionDialog
1775 gtk_font_selection_dialog_new (const gchar *title)
1777 GtkFontSelectionDialog *fontseldiag;
1779 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1782 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1784 return GTK_WIDGET (fontseldiag);
1788 * gtk_font_selection_dialog_get_font_selection:
1789 * @fsd: a #GtkFontSelectionDialog
1791 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1793 * Returns: (transfer none): the embedded #GtkFontSelection
1798 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1800 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1802 return fsd->priv->fontsel;
1807 * gtk_font_selection_dialog_get_ok_button:
1808 * @fsd: a #GtkFontSelectionDialog
1810 * Gets the 'OK' button.
1812 * Return value: (transfer none): the #GtkWidget used in the dialog
1813 * for the 'OK' button.
1818 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1820 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1822 return fsd->priv->ok_button;
1826 * gtk_font_selection_dialog_get_cancel_button:
1827 * @fsd: a #GtkFontSelectionDialog
1829 * Gets the 'Cancel' button.
1831 * Return value: (transfer none): the #GtkWidget used in the dialog
1832 * for the 'Cancel' button.
1837 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1839 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1841 return fsd->priv->cancel_button;
1845 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1847 parent_buildable_iface = g_type_interface_peek_parent (iface);
1848 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1852 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1853 GtkBuilder *builder,
1854 const gchar *childname)
1856 GtkFontSelectionDialogPrivate *priv;
1858 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1860 if (g_strcmp0 (childname, "ok_button") == 0)
1861 return G_OBJECT (priv->ok_button);
1862 else if (g_strcmp0 (childname, "cancel_button") == 0)
1863 return G_OBJECT (priv->cancel_button);
1864 else if (g_strcmp0 (childname, "apply_button") == 0)
1865 return G_OBJECT (priv->apply_button);
1866 else if (g_strcmp0 (childname, "font_selection") == 0)
1867 return G_OBJECT (priv->fontsel);
1869 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1873 * gtk_font_selection_dialog_get_font_name:
1874 * @fsd: a #GtkFontSelectionDialog
1876 * Gets the currently-selected font name.
1878 * Note that this can be a different string than what you set with
1879 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1880 * may normalize font names and thus return a string with a different
1881 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1882 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1883 * if you want to compare two font descriptions.
1885 * Return value: A string with the name of the current font, or %NULL if no
1886 * font is selected. You must free this string with g_free().
1889 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1891 GtkFontSelectionDialogPrivate *priv;
1893 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1897 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1901 * gtk_font_selection_dialog_set_font_name:
1902 * @fsd: a #GtkFontSelectionDialog
1903 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1905 * Sets the currently selected font.
1907 * Return value: %TRUE if the font selected in @fsd is now the
1908 * @fontname specified, %FALSE otherwise.
1911 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1912 const gchar *fontname)
1914 GtkFontSelectionDialogPrivate *priv;
1916 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1917 g_return_val_if_fail (fontname, FALSE);
1921 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1925 * gtk_font_selection_dialog_get_preview_text:
1926 * @fsd: a #GtkFontSelectionDialog
1928 * Gets the text displayed in the preview area.
1930 * Return value: the text displayed in the preview area.
1931 * This string is owned by the widget and should not be
1934 G_CONST_RETURN gchar*
1935 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1937 GtkFontSelectionDialogPrivate *priv;
1939 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1943 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1947 * gtk_font_selection_dialog_set_preview_text:
1948 * @fsd: a #GtkFontSelectionDialog
1949 * @text: the text to display in the preview area
1951 * Sets the text displayed in the preview area.
1954 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1957 GtkFontSelectionDialogPrivate *priv;
1959 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1960 g_return_if_fail (text != NULL);
1964 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);