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"
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;
104 struct _GtkFontSelectionDialogPrivate
108 GtkWidget *ok_button;
109 GtkWidget *apply_button;
110 GtkWidget *cancel_button;
114 /* We don't enable the font and style entries because they don't add
115 * much in terms of visible effect and have a weird effect on keynav.
116 * the Windows font selector has entries similarly positioned but they
117 * act in conjunction with the associated lists to form a single focus
120 #undef INCLUDE_FONT_ENTRIES
122 /* This is the default text shown in the preview entry, though the user
123 can set it. Remember that some fonts only have capital letters. */
124 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
126 #define DEFAULT_FONT_NAME "Sans 10"
128 /* This is the initial fixed height and the top padding of the preview entry */
129 #define PREVIEW_HEIGHT 84
130 #define PREVIEW_TOP_PADDING 6
132 /* These are the sizes of the font, style & size lists. */
133 #define FONT_LIST_HEIGHT 136
134 #define FONT_LIST_WIDTH 190
135 #define FONT_STYLE_LIST_WIDTH 170
136 #define FONT_SIZE_LIST_WIDTH 60
138 #define ROW_FORMAT_STRING "<span size=\"small\" foreground=\"%s\">%s <i>%s</i></span>\n<span size=\"large\" font_desc=\"%s\">%s</span>"
140 /* These are what we use as the standard font sizes, for the size list.
142 #define FONT_SIZES_LENGTH 25
143 static const guint16 font_sizes[] = {
144 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
145 32, 36, 40, 48, 56, 64, 72
162 static void gtk_font_selection_set_property (GObject *object,
166 static void gtk_font_selection_get_property (GObject *object,
170 static void gtk_font_selection_finalize (GObject *object);
171 static void gtk_font_selection_screen_changed (GtkWidget *widget,
172 GdkScreen *previous_screen);
173 static void gtk_font_selection_style_updated (GtkWidget *widget);
175 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
176 PangoFontFamily *family);
177 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
178 PangoFontFace *face);
179 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
181 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
184 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
186 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
189 gobject_class->finalize = gtk_font_selection_finalize;
190 gobject_class->set_property = gtk_font_selection_set_property;
191 gobject_class->get_property = gtk_font_selection_get_property;
193 widget_class->screen_changed = gtk_font_selection_screen_changed;
194 widget_class->style_updated = gtk_font_selection_style_updated;
196 g_object_class_install_property (gobject_class,
198 g_param_spec_string ("font-name",
200 P_("The string that represents this font"),
202 GTK_PARAM_READWRITE));
203 g_object_class_install_property (gobject_class,
205 g_param_spec_string ("preview-text",
207 P_("The text to display in order to demonstrate the selected font"),
209 GTK_PARAM_READWRITE));
211 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
215 gtk_font_selection_set_property (GObject *object,
220 GtkFontSelection *fontsel;
222 fontsel = GTK_FONT_SELECTION (object);
227 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
229 case PROP_PREVIEW_TEXT:
230 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 static void gtk_font_selection_get_property (GObject *object,
243 GtkFontSelection *fontsel;
245 fontsel = GTK_FONT_SELECTION (object);
250 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
252 case PROP_PREVIEW_TEXT:
253 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 deleted_text_cb (GtkEntryBuffer *buffer,
267 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
268 GtkWidget *entry = priv->search_entry;
270 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
274 inserted_text_cb (GtkEntryBuffer *buffer,
280 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
281 GtkWidget *entry = priv->search_entry;
283 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
287 gtk_font_selection_init (GtkFontSelection *fontsel)
289 GtkFontSelectionPrivate *priv;
290 PangoFontDescription *font_desc;
291 GtkWidget *scrolled_win;
292 GtkWidget *alignment;
293 GtkWidget *preview_and_size;
294 GtkWidget *size_controls;
296 GList *focus_chain = NULL;
300 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
301 GTK_TYPE_FONT_SELECTION,
302 GtkFontSelectionPrivate);
303 priv = fontsel->priv;
304 gtk_widget_push_composite_child ();
306 /* Creating fundamental widgets for the private struct */
307 priv->search_entry = gtk_entry_new ();
308 priv->family_face_list = gtk_tree_view_new ();
309 priv->preview = gtk_entry_new ();
310 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
311 (gdouble) font_sizes[0],
312 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
315 priv->size_spin = gtk_spin_button_new (NULL, 1.0, 0);
318 /** Bootstrapping widget layout **/
319 /* Main font family/face view */
320 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
321 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
323 /* Alignment for the preview and size controls */
324 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
325 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
326 PREVIEW_TOP_PADDING, 0, 0, 0);
328 preview_and_size = gtk_vbox_new (TRUE, 0);
329 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
330 gtk_box_pack_start (GTK_BOX (preview_and_size), priv->preview, FALSE, TRUE, 0);
331 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT);
333 /* Packing the slider and the spin in a hbox */
334 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
335 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
336 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
337 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
339 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
340 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
342 /* Packing everything in the selection */
343 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
344 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
345 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
347 /* Getting the default size */
348 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
349 priv->size = pango_font_description_get_size (font_desc);
353 gtk_widget_show_all (GTK_WIDGET (fontsel));
354 gtk_widget_hide (GTK_WIDGET (fontsel));
356 /* Treeview column and model bootstrapping */
357 gtk_font_selection_bootstrap_fontlist (fontsel);
359 /* Set default preview text */
360 gtk_entry_set_text (GTK_ENTRY (priv->preview),
361 pango_language_get_sample_string (NULL));
363 /* Set search icon and place holder text */
364 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
365 GTK_ENTRY_ICON_SECONDARY,
367 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), N_("Search font name"));
369 /** Callback connections **/
370 /* Connect to callback for the live search text entry */
371 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
372 "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
373 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
374 "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
376 gtk_widget_pop_composite_child();
380 * gtk_font_selection_new:
382 * Creates a new #GtkFontSelection.
384 * Return value: a n ew #GtkFontSelection
387 gtk_font_selection_new (void)
389 GtkFontSelection *fontsel;
391 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
393 return GTK_WIDGET (fontsel);
397 cmp_families (const void *a, const void *b)
399 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
400 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
402 return g_utf8_collate (a_name, b_name);
406 set_cursor_to_iter (GtkTreeView *view,
409 GtkTreeModel *model = gtk_tree_view_get_model (view);
410 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
412 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
414 gtk_tree_path_free (path);
418 populate_list (GtkTreeView* treeview, GtkListStore* model)
420 GtkStyleContext *style_context;
425 GtkTreeIter match_row;
428 PangoFontFamily **families;
430 GString *tmp = g_string_new (NULL);
432 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
436 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
438 gtk_list_store_clear (model);
440 /* Get row header font color */
441 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
442 gtk_style_context_get_color (style_context,
443 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
446 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
447 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
448 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
449 color_string = pango_color_to_string (&p_color);
451 /* Iterate over families and faces */
452 for (i=0; i<n_families; i++)
455 PangoFontFace **faces;
457 const gchar *fam_name = pango_font_family_get_name (families[i]);
459 pango_font_family_list_faces (families[i], &faces, &n_faces);
461 for (j=0; j<n_faces; j++)
463 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
464 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
465 gchar *font_desc = pango_font_description_to_string (pango_desc);
467 /* foreground_color, family_name, face_name, desc, sample string */
468 g_string_printf (tmp, ROW_FORMAT_STRING,
476 gtk_list_store_append (model, &iter);
477 gtk_list_store_set (model, &iter,
478 FAMILY_COLUMN, families[i],
479 FACE_COLUMN, faces[j],
480 FAMILY_NAME_COLUMN, fam_name,
481 TEXT_COLUMN, tmp->str,
484 if ((i == 0 && j == 0) ||
485 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
490 pango_font_description_free(pango_desc);
497 set_cursor_to_iter (treeview, &match_row);
499 g_string_free (tmp, TRUE);
500 g_free (color_string);
505 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
507 gboolean result = FALSE;
508 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
510 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
512 gchar *font_name_casefold;
513 gchar *search_text_casefold;
515 gtk_tree_model_get (model, iter,
516 FAMILY_NAME_COLUMN, &font_name,
519 if (font_name == NULL)
525 font_name_casefold = g_utf8_casefold (font_name, -1);
526 search_text_casefold = g_utf8_casefold (search_text, -1);
528 if (g_strrstr (font_name_casefold, search_text_casefold))
531 g_free (search_text_casefold);
532 g_free (font_name_casefold);
538 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
540 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
541 GtkTreeViewColumn *col;
543 fontsel->priv->model = gtk_list_store_new (4,
544 PANGO_TYPE_FONT_FAMILY,
545 PANGO_TYPE_FONT_FACE,
549 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
551 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
553 (gpointer)fontsel->priv,
557 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
559 gtk_tree_view_set_rules_hint (treeview, TRUE);
560 gtk_tree_view_set_headers_visible (treeview, FALSE);
562 col = gtk_tree_view_column_new_with_attributes ("Family",
563 gtk_cell_renderer_text_new (),
564 "markup", TEXT_COLUMN,
566 gtk_tree_view_append_column (treeview, col);
568 populate_list (treeview, fontsel->priv->model);
573 gtk_font_selection_finalize (GObject *object)
575 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
577 gtk_font_selection_ref_family (fontsel, NULL);
578 gtk_font_selection_ref_face (fontsel, NULL);
580 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
584 gtk_font_selection_screen_changed (GtkWidget *widget,
585 GdkScreen *previous_screen)
591 gtk_font_selection_style_updated (GtkWidget *widget)
593 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
599 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
600 PangoFontFamily *family)
602 GtkFontSelectionPrivate *priv = fontsel->priv;
605 family = g_object_ref (family);
607 g_object_unref (priv->family);
608 priv->family = family;
612 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
615 GtkFontSelectionPrivate *priv = fontsel->priv;
618 face = g_object_ref (face);
620 g_object_unref (priv->face);
624 /*****************************************************************************
625 * These functions are the main public interface for getting/setting the font.
626 *****************************************************************************/
629 * gtk_font_selection_get_family_list:
630 * @fontsel: a #GtkFontSelection
632 * This returns the #GtkTreeView that lists font families, for
633 * example, 'Sans', 'Serif', etc.
635 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
640 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
642 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
648 * gtk_font_selection_get_face_list:
649 * @fontsel: a #GtkFontSelection
651 * This returns the #GtkTreeView which lists all styles available for
652 * the selected font. For example, 'Regular', 'Bold', etc.
654 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
659 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
661 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
667 * gtk_font_selection_get_size_entry:
668 * @fontsel: a #GtkFontSelection
670 * This returns the #GtkEntry used to allow the user to edit the font
671 * number manually instead of selecting it from the list of font sizes.
673 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
678 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
680 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
686 * gtk_font_selection_get_size_list:
687 * @fontsel: a #GtkFontSelection
689 * This returns the #GtkTreeeView used to list font sizes.
691 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
696 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
698 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
704 * gtk_font_selection_get_preview_entry:
705 * @fontsel: a #GtkFontSelection
707 * This returns the #GtkEntry used to display the font as a preview.
709 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
714 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
716 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
722 * gtk_font_selection_get_family:
723 * @fontsel: a #GtkFontSelection
725 * Gets the #PangoFontFamily representing the selected font family.
727 * Return value: (transfer none): A #PangoFontFamily representing the
728 * selected font family. Font families are a collection of font
729 * faces. The returned object is owned by @fontsel and must not
730 * be modified or freed.
735 gtk_font_selection_get_family (GtkFontSelection *fontsel)
737 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
743 * gtk_font_selection_get_face:
744 * @fontsel: a #GtkFontSelection
746 * Gets the #PangoFontFace representing the selected font group
747 * details (i.e. family, slant, weight, width, etc).
749 * Return value: (transfer none): A #PangoFontFace representing the
750 * selected font group details. The returned object is owned by
751 * @fontsel and must not be modified or freed.
756 gtk_font_selection_get_face (GtkFontSelection *fontsel)
758 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
764 * gtk_font_selection_get_size:
765 * @fontsel: a #GtkFontSelection
767 * The selected font size.
769 * Return value: A n integer representing the selected font size,
770 * or -1 if no font size is selected.
775 gtk_font_selection_get_size (GtkFontSelection *fontsel)
777 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
779 return fontsel->priv->size;
783 * gtk_font_selection_get_font_name:
784 * @fontsel: a #GtkFontSelection
786 * Gets the currently-selected font name.
788 * Note that this can be a different string than what you set with
789 * gtk_font_selection_set_font_name(), as the font selection widget may
790 * normalize font names and thus return a string with a different structure.
791 * For example, "Helvetica Italic Bold 12" could be normalized to
792 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
793 * if you want to compare two font descriptions.
795 * Return value: A string with the name of the current font, or %NULL if
796 * no font is selected. You must free this string with g_free().
799 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
804 /* This sets the current font, then selecting the appropriate list rows. */
807 * gtk_font_selection_set_font_name:
808 * @fontsel: a #GtkFontSelection
809 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
811 * Sets the currently-selected font.
813 * Note that the @fontsel needs to know the screen in which it will appear
814 * for this to work; this can be guaranteed by simply making sure that the
815 * @fontsel is inserted in a toplevel window before you call this function.
817 * Return value: %TRUE if the font could be set successfully; %FALSE if no
818 * such font exists or if the @fontsel doesn't belong to a particular
822 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
823 const gchar *fontname)
826 PangoFontFamily *family = NULL;
827 PangoFontFace *face = NULL;
828 PangoFontDescription *new_desc;
831 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
837 * gtk_font_selection_get_preview_text:
838 * @fontsel: a #GtkFontSelection
840 * Gets the text displayed in the preview area.
842 * Return value: the text displayed in the preview area.
843 * This string is owned by the widget and should not be
846 G_CONST_RETURN gchar*
847 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
854 * gtk_font_selection_set_preview_text:
855 * @fontsel: a #GtkFontSelection
856 * @text: the text to display in the preview area
858 * Sets the text displayed in the preview area.
859 * The @text is used to show how the selected font looks.
862 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
866 GtkFontSelectionPrivate *priv;
868 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
869 g_return_if_fail (text != NULL);
871 priv = fontsel->priv;
877 * SECTION:gtkfontseldlg
878 * @Short_description: A dialog box for selecting fonts
879 * @Title: GtkFontSelectionDialog
880 * @See_also: #GtkFontSelection, #GtkDialog
882 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
884 * To set the font which is initially selected, use
885 * gtk_font_selection_dialog_set_font_name().
887 * To get the selected font use gtk_font_selection_dialog_get_font_name().
889 * To change the text which is shown in the preview area, use
890 * gtk_font_selection_dialog_set_preview_text().
892 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
893 * <title>GtkFontSelectionDialog as GtkBuildable</title>
894 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
895 * exposes the embedded #GtkFontSelection as internal child with the
896 * name "font_selection". It also exposes the buttons with the names
897 * "ok_button", "cancel_button" and "apply_button".
901 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
902 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
904 const gchar *childname);
906 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
908 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
909 gtk_font_selection_dialog_buildable_interface_init))
911 static GtkBuildableIface *parent_buildable_iface;
914 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
916 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
920 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
922 GtkFontSelectionDialogPrivate *priv;
923 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
924 GtkWidget *action_area, *content_area;
926 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
927 GTK_TYPE_FONT_SELECTION_DIALOG,
928 GtkFontSelectionDialogPrivate);
929 priv = fontseldiag->priv;
931 content_area = gtk_dialog_get_content_area (dialog);
932 action_area = gtk_dialog_get_action_area (dialog);
934 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
935 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
936 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
937 gtk_box_set_spacing (GTK_BOX (action_area), 6);
939 gtk_widget_push_composite_child ();
941 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
943 /* Create the content area */
944 priv->fontsel = gtk_font_selection_new ();
945 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
946 gtk_widget_show (priv->fontsel);
947 gtk_box_pack_start (GTK_BOX (content_area),
948 priv->fontsel, TRUE, TRUE, 0);
950 /* Create the action area */
951 priv->cancel_button = gtk_dialog_add_button (dialog,
953 GTK_RESPONSE_CANCEL);
955 priv->apply_button = gtk_dialog_add_button (dialog,
958 gtk_widget_hide (priv->apply_button);
960 priv->ok_button = gtk_dialog_add_button (dialog,
963 gtk_widget_grab_default (priv->ok_button);
965 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
971 gtk_window_set_title (GTK_WINDOW (fontseldiag),
972 _("Font Selection"));
974 gtk_widget_pop_composite_child ();
978 * gtk_font_selection_dialog_new:
979 * @title: the title of the dialog window
981 * Creates a new #GtkFontSelectionDialog.
983 * Return value: a new #GtkFontSelectionDialog
986 gtk_font_selection_dialog_new (const gchar *title)
988 GtkFontSelectionDialog *fontseldiag;
990 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
993 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
995 return GTK_WIDGET (fontseldiag);
999 * gtk_font_selection_dialog_get_font_selection:
1000 * @fsd: a #GtkFontSelectionDialog
1002 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1004 * Returns: (transfer none): the embedded #GtkFontSelection
1009 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1011 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1013 return fsd->priv->fontsel;
1018 * gtk_font_selection_dialog_get_ok_button:
1019 * @fsd: a #GtkFontSelectionDialog
1021 * Gets the 'OK' button.
1023 * Return value: (transfer none): the #GtkWidget used in the dialog
1024 * for the 'OK' button.
1029 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1031 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1033 return fsd->priv->ok_button;
1037 * gtk_font_selection_dialog_get_cancel_button:
1038 * @fsd: a #GtkFontSelectionDialog
1040 * Gets the 'Cancel' button.
1042 * Return value: (transfer none): the #GtkWidget used in the dialog
1043 * for the 'Cancel' button.
1048 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1050 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1052 return fsd->priv->cancel_button;
1056 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1058 parent_buildable_iface = g_type_interface_peek_parent (iface);
1059 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1063 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1064 GtkBuilder *builder,
1065 const gchar *childname)
1067 GtkFontSelectionDialogPrivate *priv;
1069 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1071 if (g_strcmp0 (childname, "ok_button") == 0)
1072 return G_OBJECT (priv->ok_button);
1073 else if (g_strcmp0 (childname, "cancel_button") == 0)
1074 return G_OBJECT (priv->cancel_button);
1075 else if (g_strcmp0 (childname, "apply_button") == 0)
1076 return G_OBJECT (priv->apply_button);
1077 else if (g_strcmp0 (childname, "font_selection") == 0)
1078 return G_OBJECT (priv->fontsel);
1080 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1084 * gtk_font_selection_dialog_get_font_name:
1085 * @fsd: a #GtkFontSelectionDialog
1087 * Gets the currently-selected font name.
1089 * Note that this can be a different string than what you set with
1090 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1091 * may normalize font names and thus return a string with a different
1092 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1093 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1094 * if you want to compare two font descriptions.
1096 * Return value: A string with the name of the current font, or %NULL if no
1097 * font is selected. You must free this string with g_free().
1100 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1102 GtkFontSelectionDialogPrivate *priv;
1104 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1108 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1112 * gtk_font_selection_dialog_set_font_name:
1113 * @fsd: a #GtkFontSelectionDialog
1114 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1116 * Sets the currently selected font.
1118 * Return value: %TRUE if the font selected in @fsd is now the
1119 * @fontname specified, %FALSE otherwise.
1122 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1123 const gchar *fontname)
1125 GtkFontSelectionDialogPrivate *priv;
1127 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1128 g_return_val_if_fail (fontname, FALSE);
1132 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1136 * gtk_font_selection_dialog_get_preview_text:
1137 * @fsd: a #GtkFontSelectionDialog
1139 * Gets the text displayed in the preview area.
1141 * Return value: the text displayed in the preview area.
1142 * This string is owned by the widget and should not be
1145 G_CONST_RETURN gchar*
1146 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1148 GtkFontSelectionDialogPrivate *priv;
1150 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1154 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1158 * gtk_font_selection_dialog_set_preview_text:
1159 * @fsd: a #GtkFontSelectionDialog
1160 * @text: the text to display in the preview area
1162 * Sets the text displayed in the preview area.
1165 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1168 GtkFontSelectionDialogPrivate *priv;
1170 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1171 g_return_if_fail (text != NULL);
1175 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);