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"
67 * @Short_description: A widget for selecting fonts
68 * @Title: GtkFontSelection
69 * @See_also: #GtkFontSelectionDialog
71 * The #GtkFontSelection widget lists the available fonts, styles and sizes,
72 * allowing the user to select a font.
73 * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
76 * To set the font which is initially selected, use
77 * gtk_font_selection_set_font_name().
79 * To get the selected font use gtk_font_selection_get_font_name().
81 * To change the text which is shown in the preview area, use
82 * gtk_font_selection_set_preview_text().
86 struct _GtkFontSelectionPrivate
88 GtkWidget *search_entry;
89 GtkWidget *family_face_list;
90 GtkWidget *size_slider;
96 PangoFontFamily *family;
100 struct _GtkFontSelectionDialogPrivate
104 GtkWidget *ok_button;
105 GtkWidget *apply_button;
106 GtkWidget *cancel_button;
110 /* We don't enable the font and style entries because they don't add
111 * much in terms of visible effect and have a weird effect on keynav.
112 * the Windows font selector has entries similarly positioned but they
113 * act in conjunction with the associated lists to form a single focus
116 #undef INCLUDE_FONT_ENTRIES
118 /* This is the default text shown in the preview entry, though the user
119 can set it. Remember that some fonts only have capital letters. */
120 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
122 #define DEFAULT_FONT_NAME "Sans 10"
124 /* This is the initial fixed height and the top padding of the preview entry */
125 #define PREVIEW_HEIGHT 100
126 #define PREVIEW_TOP_PADDING 6
128 /* These are the sizes of the font, style & size lists. */
129 #define FONT_LIST_HEIGHT 136
130 #define FONT_LIST_WIDTH 190
131 #define FONT_STYLE_LIST_WIDTH 170
132 #define FONT_SIZE_LIST_WIDTH 60
134 #define ROW_FORMAT_STRING "<span foreground=\"%s\">%s %s</span>\n<span font_desc=\"%s\">%s</span>"
136 /* These are what we use as the standard font sizes, for the size list.
138 #define FONT_SIZES_LENGTH 25
139 static const guint16 font_sizes[] = {
140 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
141 32, 36, 40, 48, 56, 64, 72
158 static void gtk_font_selection_set_property (GObject *object,
162 static void gtk_font_selection_get_property (GObject *object,
166 static void gtk_font_selection_finalize (GObject *object);
167 static void gtk_font_selection_screen_changed (GtkWidget *widget,
168 GdkScreen *previous_screen);
169 static void gtk_font_selection_style_updated (GtkWidget *widget);
171 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
172 PangoFontFamily *family);
173 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
174 PangoFontFace *face);
175 static void gtk_font_selection_bootstrap_fontlist (GtkTreeView* treeview);
177 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
180 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
185 gobject_class->finalize = gtk_font_selection_finalize;
186 gobject_class->set_property = gtk_font_selection_set_property;
187 gobject_class->get_property = gtk_font_selection_get_property;
189 widget_class->screen_changed = gtk_font_selection_screen_changed;
190 widget_class->style_updated = gtk_font_selection_style_updated;
192 g_object_class_install_property (gobject_class,
194 g_param_spec_string ("font-name",
196 P_("The string that represents this font"),
198 GTK_PARAM_READWRITE));
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("preview-text",
203 P_("The text to display in order to demonstrate the selected font"),
205 GTK_PARAM_READWRITE));
207 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
211 gtk_font_selection_set_property (GObject *object,
216 GtkFontSelection *fontsel;
218 fontsel = GTK_FONT_SELECTION (object);
223 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
225 case PROP_PREVIEW_TEXT:
226 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 static void gtk_font_selection_get_property (GObject *object,
239 GtkFontSelection *fontsel;
241 fontsel = GTK_FONT_SELECTION (object);
246 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
248 case PROP_PREVIEW_TEXT:
249 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 /* Handles key press events on the lists, so that we can trap Enter to
258 * activate the default button on our own.
261 list_row_activated (GtkWidget *widget)
263 GtkWidget *default_widget, *focus_widget;
266 window = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
267 if (!gtk_widget_is_toplevel (GTK_WIDGET (window)))
272 default_widget = gtk_window_get_default_widget (window);
273 focus_widget = gtk_window_get_focus (window);
275 if (widget != default_widget &&
276 !(widget == focus_widget && (!default_widget || !gtk_widget_get_sensitive (default_widget))))
277 gtk_window_activate_default (window);
284 gtk_font_selection_init (GtkFontSelection *fontsel)
286 GtkFontSelectionPrivate *priv;
288 GtkWidget *scrolled_win;
289 GtkWidget *alignment;
290 GtkWidget *preview_and_size;
291 GtkWidget *size_controls;
292 GList *focus_chain = NULL;
295 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
296 GTK_TYPE_FONT_SELECTION,
297 GtkFontSelectionPrivate);
298 priv = fontsel->priv;
299 gtk_widget_push_composite_child ();
301 /* Creating fundamental widgets for the private struct */
302 priv->search_entry = gtk_entry_new ();
303 priv->family_face_list = gtk_tree_view_new ();
304 priv->preview = gtk_entry_new ();
305 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
306 (gdouble) font_sizes[0],
307 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
310 priv->size_spin = gtk_spin_button_new (NULL, 1.0, 0);
312 /* Main font family/face view */
313 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
314 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
316 /* Alignment for the preview and size controls */
317 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
318 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
319 PREVIEW_TOP_PADDING, 0, 0, 0);
321 preview_and_size = gtk_vbox_new (TRUE, 0);
322 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
323 gtk_box_pack_start (GTK_BOX (preview_and_size), priv->preview, FALSE, TRUE, 0);
324 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT);
326 /* Packing the slider and the spin in a hbox */
327 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
328 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
329 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
330 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
332 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
333 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
335 /* Packing everything in the selection */
336 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
337 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
338 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
340 priv->size = 12 * PANGO_SCALE;
344 gtk_widget_show_all (GTK_WIDGET (fontsel));
345 gtk_widget_hide (GTK_WIDGET (fontsel));
347 gtk_font_selection_bootstrap_fontlist (GTK_TREE_VIEW (priv->family_face_list));
349 gtk_widget_pop_composite_child();
353 * gtk_font_selection_new:
355 * Creates a new #GtkFontSelection.
357 * Return value: a n ew #GtkFontSelection
360 gtk_font_selection_new (void)
362 GtkFontSelection *fontsel;
364 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
366 return GTK_WIDGET (fontsel);
370 cmp_families (const void *a, const void *b)
372 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
373 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
375 return g_utf8_collate (a_name, b_name);
379 set_cursor_to_iter (GtkTreeView *view,
382 GtkTreeModel *model = gtk_tree_view_get_model (view);
383 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
385 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
387 gtk_tree_path_free (path);
391 gtk_font_selection_populate_list (GtkTreeView *treeview)
394 PangoFontFamily *match_family;
395 PangoFontFamily **families;
397 GtkTreeIter match_row;
398 GString *tmp = g_string_new (NULL);
399 const gchar* row_format = ROW_FORMAT_STRING;
401 model = GTK_LIST_STORE (gtk_tree_view_get_model (treeview));
403 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
407 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
409 gtk_list_store_clear (model);
411 /* FIXME: Get theme color here */
413 for (i=0; i<n_families; i++)
416 PangoFontFace **faces;
418 const gchar *fam_name = pango_font_family_get_name (families[i]);
420 pango_font_family_list_faces (families[i], &faces, &n_faces);
422 for (j=0; j<n_faces; j++)
424 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
425 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
426 gchar *font_desc = pango_font_description_to_string (pango_desc);
428 /* foreground_color, family_name, face_name, desc, sample string */
429 g_string_printf (tmp, ROW_FORMAT_STRING,
430 "darkgrey", /* FIXME: This has to be a theme color */
437 gtk_list_store_append (model, &iter);
438 gtk_list_store_set (model, &iter,
439 FAMILY_COLUMN, families[i],
440 FACE_COLUMN, faces[j],
441 FAMILY_NAME_COLUMN, fam_name,
442 TEXT_COLUMN, tmp->str,
445 if ((i == 0 && j == 0) ||
446 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
448 match_family = families[i];
452 pango_font_description_free(pango_desc);
459 set_cursor_to_iter (treeview, &match_row);
461 g_string_free (tmp, TRUE);
466 gtk_font_selection_bootstrap_fontlist (GtkTreeView* treeview)
468 GtkTreeViewColumn *col;
469 GtkListStore *fonts_model;
471 fonts_model = gtk_list_store_new (4,
472 PANGO_TYPE_FONT_FAMILY,
473 PANGO_TYPE_FONT_FACE,
476 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fonts_model));
478 gtk_tree_view_set_rules_hint (treeview, TRUE);
479 gtk_tree_view_set_headers_visible (treeview, FALSE);
481 col = gtk_tree_view_column_new_with_attributes ("Family",
482 gtk_cell_renderer_text_new (),
483 "markup", TEXT_COLUMN,
485 gtk_tree_view_append_column (treeview, col);
487 gtk_font_selection_populate_list (treeview);
492 gtk_font_selection_finalize (GObject *object)
494 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
496 gtk_font_selection_ref_family (fontsel, NULL);
497 gtk_font_selection_ref_face (fontsel, NULL);
499 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
503 gtk_font_selection_screen_changed (GtkWidget *widget,
504 GdkScreen *previous_screen)
510 gtk_font_selection_style_updated (GtkWidget *widget)
512 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
518 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
519 PangoFontFamily *family)
521 GtkFontSelectionPrivate *priv = fontsel->priv;
524 family = g_object_ref (family);
526 g_object_unref (priv->family);
527 priv->family = family;
531 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
534 GtkFontSelectionPrivate *priv = fontsel->priv;
537 face = g_object_ref (face);
539 g_object_unref (priv->face);
543 /*****************************************************************************
544 * These functions are the main public interface for getting/setting the font.
545 *****************************************************************************/
548 * gtk_font_selection_get_family_list:
549 * @fontsel: a #GtkFontSelection
551 * This returns the #GtkTreeView that lists font families, for
552 * example, 'Sans', 'Serif', etc.
554 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
559 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
561 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
567 * gtk_font_selection_get_face_list:
568 * @fontsel: a #GtkFontSelection
570 * This returns the #GtkTreeView which lists all styles available for
571 * the selected font. For example, 'Regular', 'Bold', etc.
573 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
578 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
580 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
586 * gtk_font_selection_get_size_entry:
587 * @fontsel: a #GtkFontSelection
589 * This returns the #GtkEntry used to allow the user to edit the font
590 * number manually instead of selecting it from the list of font sizes.
592 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
597 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
599 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
605 * gtk_font_selection_get_size_list:
606 * @fontsel: a #GtkFontSelection
608 * This returns the #GtkTreeeView used to list font sizes.
610 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
615 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
617 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
623 * gtk_font_selection_get_preview_entry:
624 * @fontsel: a #GtkFontSelection
626 * This returns the #GtkEntry used to display the font as a preview.
628 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
633 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
635 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
641 * gtk_font_selection_get_family:
642 * @fontsel: a #GtkFontSelection
644 * Gets the #PangoFontFamily representing the selected font family.
646 * Return value: (transfer none): A #PangoFontFamily representing the
647 * selected font family. Font families are a collection of font
648 * faces. The returned object is owned by @fontsel and must not
649 * be modified or freed.
654 gtk_font_selection_get_family (GtkFontSelection *fontsel)
656 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
662 * gtk_font_selection_get_face:
663 * @fontsel: a #GtkFontSelection
665 * Gets the #PangoFontFace representing the selected font group
666 * details (i.e. family, slant, weight, width, etc).
668 * Return value: (transfer none): A #PangoFontFace representing the
669 * selected font group details. The returned object is owned by
670 * @fontsel and must not be modified or freed.
675 gtk_font_selection_get_face (GtkFontSelection *fontsel)
677 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
683 * gtk_font_selection_get_size:
684 * @fontsel: a #GtkFontSelection
686 * The selected font size.
688 * Return value: A n integer representing the selected font size,
689 * or -1 if no font size is selected.
694 gtk_font_selection_get_size (GtkFontSelection *fontsel)
696 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
702 * gtk_font_selection_get_font_name:
703 * @fontsel: a #GtkFontSelection
705 * Gets the currently-selected font name.
707 * Note that this can be a different string than what you set with
708 * gtk_font_selection_set_font_name(), as the font selection widget may
709 * normalize font names and thus return a string with a different structure.
710 * For example, "Helvetica Italic Bold 12" could be normalized to
711 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
712 * if you want to compare two font descriptions.
714 * Return value: A string with the name of the current font, or %NULL if
715 * no font is selected. You must free this string with g_free().
718 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
723 /* This sets the current font, then selecting the appropriate list rows. */
726 * gtk_font_selection_set_font_name:
727 * @fontsel: a #GtkFontSelection
728 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
730 * Sets the currently-selected font.
732 * Note that the @fontsel needs to know the screen in which it will appear
733 * for this to work; this can be guaranteed by simply making sure that the
734 * @fontsel is inserted in a toplevel window before you call this function.
736 * Return value: %TRUE if the font could be set successfully; %FALSE if no
737 * such font exists or if the @fontsel doesn't belong to a particular
741 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
742 const gchar *fontname)
744 PangoFontFamily *family = NULL;
745 PangoFontFace *face = NULL;
746 PangoFontDescription *new_desc;
748 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
754 * gtk_font_selection_get_preview_text:
755 * @fontsel: a #GtkFontSelection
757 * Gets the text displayed in the preview area.
759 * Return value: the text displayed in the preview area.
760 * This string is owned by the widget and should not be
763 G_CONST_RETURN gchar*
764 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
771 * gtk_font_selection_set_preview_text:
772 * @fontsel: a #GtkFontSelection
773 * @text: the text to display in the preview area
775 * Sets the text displayed in the preview area.
776 * The @text is used to show how the selected font looks.
779 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
782 GtkFontSelectionPrivate *priv;
784 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
785 g_return_if_fail (text != NULL);
787 priv = fontsel->priv;
792 * SECTION:gtkfontseldlg
793 * @Short_description: A dialog box for selecting fonts
794 * @Title: GtkFontSelectionDialog
795 * @See_also: #GtkFontSelection, #GtkDialog
797 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
799 * To set the font which is initially selected, use
800 * gtk_font_selection_dialog_set_font_name().
802 * To get the selected font use gtk_font_selection_dialog_get_font_name().
804 * To change the text which is shown in the preview area, use
805 * gtk_font_selection_dialog_set_preview_text().
807 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
808 * <title>GtkFontSelectionDialog as GtkBuildable</title>
809 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
810 * exposes the embedded #GtkFontSelection as internal child with the
811 * name "font_selection". It also exposes the buttons with the names
812 * "ok_button", "cancel_button" and "apply_button".
816 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
817 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
819 const gchar *childname);
821 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
823 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
824 gtk_font_selection_dialog_buildable_interface_init))
826 static GtkBuildableIface *parent_buildable_iface;
829 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
831 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
835 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
837 GtkFontSelectionDialogPrivate *priv;
838 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
839 GtkWidget *action_area, *content_area;
841 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
842 GTK_TYPE_FONT_SELECTION_DIALOG,
843 GtkFontSelectionDialogPrivate);
844 priv = fontseldiag->priv;
846 content_area = gtk_dialog_get_content_area (dialog);
847 action_area = gtk_dialog_get_action_area (dialog);
849 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
850 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
851 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
852 gtk_box_set_spacing (GTK_BOX (action_area), 6);
854 gtk_widget_push_composite_child ();
856 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
858 /* Create the content area */
859 priv->fontsel = gtk_font_selection_new ();
860 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
861 gtk_widget_show (priv->fontsel);
862 gtk_box_pack_start (GTK_BOX (content_area),
863 priv->fontsel, TRUE, TRUE, 0);
865 /* Create the action area */
866 priv->cancel_button = gtk_dialog_add_button (dialog,
868 GTK_RESPONSE_CANCEL);
870 priv->apply_button = gtk_dialog_add_button (dialog,
873 gtk_widget_hide (priv->apply_button);
875 priv->ok_button = gtk_dialog_add_button (dialog,
878 gtk_widget_grab_default (priv->ok_button);
880 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
886 gtk_window_set_title (GTK_WINDOW (fontseldiag),
887 _("Font Selection"));
889 gtk_widget_pop_composite_child ();
893 * gtk_font_selection_dialog_new:
894 * @title: the title of the dialog window
896 * Creates a new #GtkFontSelectionDialog.
898 * Return value: a new #GtkFontSelectionDialog
901 gtk_font_selection_dialog_new (const gchar *title)
903 GtkFontSelectionDialog *fontseldiag;
905 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
908 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
910 return GTK_WIDGET (fontseldiag);
914 * gtk_font_selection_dialog_get_font_selection:
915 * @fsd: a #GtkFontSelectionDialog
917 * Retrieves the #GtkFontSelection widget embedded in the dialog.
919 * Returns: (transfer none): the embedded #GtkFontSelection
924 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
926 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
928 return fsd->priv->fontsel;
933 * gtk_font_selection_dialog_get_ok_button:
934 * @fsd: a #GtkFontSelectionDialog
936 * Gets the 'OK' button.
938 * Return value: (transfer none): the #GtkWidget used in the dialog
939 * for the 'OK' button.
944 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
946 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
948 return fsd->priv->ok_button;
952 * gtk_font_selection_dialog_get_cancel_button:
953 * @fsd: a #GtkFontSelectionDialog
955 * Gets the 'Cancel' button.
957 * Return value: (transfer none): the #GtkWidget used in the dialog
958 * for the 'Cancel' button.
963 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
965 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
967 return fsd->priv->cancel_button;
971 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
973 parent_buildable_iface = g_type_interface_peek_parent (iface);
974 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
978 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
980 const gchar *childname)
982 GtkFontSelectionDialogPrivate *priv;
984 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
986 if (g_strcmp0 (childname, "ok_button") == 0)
987 return G_OBJECT (priv->ok_button);
988 else if (g_strcmp0 (childname, "cancel_button") == 0)
989 return G_OBJECT (priv->cancel_button);
990 else if (g_strcmp0 (childname, "apply_button") == 0)
991 return G_OBJECT (priv->apply_button);
992 else if (g_strcmp0 (childname, "font_selection") == 0)
993 return G_OBJECT (priv->fontsel);
995 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
999 * gtk_font_selection_dialog_get_font_name:
1000 * @fsd: a #GtkFontSelectionDialog
1002 * Gets the currently-selected font name.
1004 * Note that this can be a different string than what you set with
1005 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1006 * may normalize font names and thus return a string with a different
1007 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1008 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1009 * if you want to compare two font descriptions.
1011 * Return value: A string with the name of the current font, or %NULL if no
1012 * font is selected. You must free this string with g_free().
1015 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1017 GtkFontSelectionDialogPrivate *priv;
1019 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1023 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1027 * gtk_font_selection_dialog_set_font_name:
1028 * @fsd: a #GtkFontSelectionDialog
1029 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1031 * Sets the currently selected font.
1033 * Return value: %TRUE if the font selected in @fsd is now the
1034 * @fontname specified, %FALSE otherwise.
1037 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1038 const gchar *fontname)
1040 GtkFontSelectionDialogPrivate *priv;
1042 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1043 g_return_val_if_fail (fontname, FALSE);
1047 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1051 * gtk_font_selection_dialog_get_preview_text:
1052 * @fsd: a #GtkFontSelectionDialog
1054 * Gets the text displayed in the preview area.
1056 * Return value: the text displayed in the preview area.
1057 * This string is owned by the widget and should not be
1060 G_CONST_RETURN gchar*
1061 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1063 GtkFontSelectionDialogPrivate *priv;
1065 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1069 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1073 * gtk_font_selection_dialog_set_preview_text:
1074 * @fsd: a #GtkFontSelectionDialog
1075 * @text: the text to display in the preview area
1077 * Sets the text displayed in the preview area.
1080 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1083 GtkFontSelectionDialogPrivate *priv;
1085 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1086 g_return_if_fail (text != NULL);
1090 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);