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 g_debug( "deleted text");
271 inserted_text_cb (GtkEntryBuffer *buffer,
277 g_debug ("inserted text");
281 gtk_font_selection_init (GtkFontSelection *fontsel)
283 GtkFontSelectionPrivate *priv;
284 PangoFontDescription *font_desc;
285 GtkWidget *scrolled_win;
286 GtkWidget *alignment;
287 GtkWidget *preview_and_size;
288 GtkWidget *size_controls;
290 GList *focus_chain = NULL;
294 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
295 GTK_TYPE_FONT_SELECTION,
296 GtkFontSelectionPrivate);
297 priv = fontsel->priv;
298 gtk_widget_push_composite_child ();
300 /* Creating fundamental widgets for the private struct */
301 priv->search_entry = gtk_entry_new ();
302 priv->family_face_list = gtk_tree_view_new ();
303 priv->preview = gtk_entry_new ();
304 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
305 (gdouble) font_sizes[0],
306 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
309 priv->size_spin = gtk_spin_button_new (NULL, 1.0, 0);
312 /** Bootstrapping widget layout **/
313 /* Main font family/face view */
314 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
315 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
317 /* Alignment for the preview and size controls */
318 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
319 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
320 PREVIEW_TOP_PADDING, 0, 0, 0);
322 preview_and_size = gtk_vbox_new (TRUE, 0);
323 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
324 gtk_box_pack_start (GTK_BOX (preview_and_size), priv->preview, FALSE, TRUE, 0);
325 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT);
327 /* Packing the slider and the spin in a hbox */
328 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
329 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
330 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
331 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
333 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
334 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
336 /* Packing everything in the selection */
337 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
338 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
339 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
341 /* Getting the default size */
342 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
343 priv->size = pango_font_description_get_size (font_desc);
347 gtk_widget_show_all (GTK_WIDGET (fontsel));
348 gtk_widget_hide (GTK_WIDGET (fontsel));
350 /* Treeview column and model bootstrapping */
351 gtk_font_selection_bootstrap_fontlist (fontsel);
353 /* Set default preview text */
354 gtk_entry_set_text (GTK_ENTRY (priv->preview),
355 pango_language_get_sample_string (NULL));
357 /** Callback connections **/
358 /* Connect to callback for the live search text entry */
359 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
360 "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
361 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
362 "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
364 gtk_widget_pop_composite_child();
368 * gtk_font_selection_new:
370 * Creates a new #GtkFontSelection.
372 * Return value: a n ew #GtkFontSelection
375 gtk_font_selection_new (void)
377 GtkFontSelection *fontsel;
379 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
381 return GTK_WIDGET (fontsel);
385 cmp_families (const void *a, const void *b)
387 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
388 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
390 return g_utf8_collate (a_name, b_name);
394 set_cursor_to_iter (GtkTreeView *view,
397 GtkTreeModel *model = gtk_tree_view_get_model (view);
398 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
400 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
402 gtk_tree_path_free (path);
406 populate_list (GtkTreeView* treeview, GtkListStore* model)
408 GtkStyleContext *style_context;
413 GtkTreeIter match_row;
416 PangoFontFamily **families;
418 GString *tmp = g_string_new (NULL);
420 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
424 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
426 gtk_list_store_clear (model);
428 /* Get row header font color */
429 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
430 gtk_style_context_get_color (style_context,
431 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
434 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
435 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
436 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
437 color_string = pango_color_to_string (&p_color);
439 /* Iterate over families and faces */
440 for (i=0; i<n_families; i++)
443 PangoFontFace **faces;
445 const gchar *fam_name = pango_font_family_get_name (families[i]);
447 pango_font_family_list_faces (families[i], &faces, &n_faces);
449 for (j=0; j<n_faces; j++)
451 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
452 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
453 gchar *font_desc = pango_font_description_to_string (pango_desc);
455 /* foreground_color, family_name, face_name, desc, sample string */
456 g_string_printf (tmp, ROW_FORMAT_STRING,
464 gtk_list_store_append (model, &iter);
465 gtk_list_store_set (model, &iter,
466 FAMILY_COLUMN, families[i],
467 FACE_COLUMN, faces[j],
468 FAMILY_NAME_COLUMN, fam_name,
469 TEXT_COLUMN, tmp->str,
472 if ((i == 0 && j == 0) ||
473 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
478 pango_font_description_free(pango_desc);
485 set_cursor_to_iter (treeview, &match_row);
487 g_string_free (tmp, TRUE);
488 g_free (color_string);
493 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
495 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
497 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
499 gchar *font_name_casefold;
500 gchar *search_text_casefold;
502 gtk_tree_model_get (model, iter,
503 FAMILY_NAME_COLUMN, &font_name,
506 if (font_name == NULL)
512 font_name_casefold = g_utf8_casefold (font_name, -1);
513 search_text_casefold = g_utf8_casefold (search_text, -1);
515 g_free (search_text_casefold);
516 g_free (font_name_casefold);
522 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
524 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
525 GtkTreeViewColumn *col;
527 fontsel->priv->model = gtk_list_store_new (4,
528 PANGO_TYPE_FONT_FAMILY,
529 PANGO_TYPE_FONT_FACE,
532 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
535 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
537 gtk_tree_view_set_rules_hint (treeview, TRUE);
538 gtk_tree_view_set_headers_visible (treeview, FALSE);
540 col = gtk_tree_view_column_new_with_attributes ("Family",
541 gtk_cell_renderer_text_new (),
542 "markup", TEXT_COLUMN,
544 gtk_tree_view_append_column (treeview, col);
546 populate_list (treeview, fontsel->priv->model);
551 gtk_font_selection_finalize (GObject *object)
553 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
555 gtk_font_selection_ref_family (fontsel, NULL);
556 gtk_font_selection_ref_face (fontsel, NULL);
558 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
562 gtk_font_selection_screen_changed (GtkWidget *widget,
563 GdkScreen *previous_screen)
569 gtk_font_selection_style_updated (GtkWidget *widget)
571 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
577 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
578 PangoFontFamily *family)
580 GtkFontSelectionPrivate *priv = fontsel->priv;
583 family = g_object_ref (family);
585 g_object_unref (priv->family);
586 priv->family = family;
590 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
593 GtkFontSelectionPrivate *priv = fontsel->priv;
596 face = g_object_ref (face);
598 g_object_unref (priv->face);
602 /*****************************************************************************
603 * These functions are the main public interface for getting/setting the font.
604 *****************************************************************************/
607 * gtk_font_selection_get_family_list:
608 * @fontsel: a #GtkFontSelection
610 * This returns the #GtkTreeView that lists font families, for
611 * example, 'Sans', 'Serif', etc.
613 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
618 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
620 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
626 * gtk_font_selection_get_face_list:
627 * @fontsel: a #GtkFontSelection
629 * This returns the #GtkTreeView which lists all styles available for
630 * the selected font. For example, 'Regular', 'Bold', etc.
632 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
637 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
639 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
645 * gtk_font_selection_get_size_entry:
646 * @fontsel: a #GtkFontSelection
648 * This returns the #GtkEntry used to allow the user to edit the font
649 * number manually instead of selecting it from the list of font sizes.
651 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
656 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
658 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
664 * gtk_font_selection_get_size_list:
665 * @fontsel: a #GtkFontSelection
667 * This returns the #GtkTreeeView used to list font sizes.
669 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
674 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
676 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
682 * gtk_font_selection_get_preview_entry:
683 * @fontsel: a #GtkFontSelection
685 * This returns the #GtkEntry used to display the font as a preview.
687 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
692 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
694 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
700 * gtk_font_selection_get_family:
701 * @fontsel: a #GtkFontSelection
703 * Gets the #PangoFontFamily representing the selected font family.
705 * Return value: (transfer none): A #PangoFontFamily representing the
706 * selected font family. Font families are a collection of font
707 * faces. The returned object is owned by @fontsel and must not
708 * be modified or freed.
713 gtk_font_selection_get_family (GtkFontSelection *fontsel)
715 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
721 * gtk_font_selection_get_face:
722 * @fontsel: a #GtkFontSelection
724 * Gets the #PangoFontFace representing the selected font group
725 * details (i.e. family, slant, weight, width, etc).
727 * Return value: (transfer none): A #PangoFontFace representing the
728 * selected font group details. The returned object is owned by
729 * @fontsel and must not be modified or freed.
734 gtk_font_selection_get_face (GtkFontSelection *fontsel)
736 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
742 * gtk_font_selection_get_size:
743 * @fontsel: a #GtkFontSelection
745 * The selected font size.
747 * Return value: A n integer representing the selected font size,
748 * or -1 if no font size is selected.
753 gtk_font_selection_get_size (GtkFontSelection *fontsel)
755 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
757 return fontsel->priv->size;
761 * gtk_font_selection_get_font_name:
762 * @fontsel: a #GtkFontSelection
764 * Gets the currently-selected font name.
766 * Note that this can be a different string than what you set with
767 * gtk_font_selection_set_font_name(), as the font selection widget may
768 * normalize font names and thus return a string with a different structure.
769 * For example, "Helvetica Italic Bold 12" could be normalized to
770 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
771 * if you want to compare two font descriptions.
773 * Return value: A string with the name of the current font, or %NULL if
774 * no font is selected. You must free this string with g_free().
777 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
782 /* This sets the current font, then selecting the appropriate list rows. */
785 * gtk_font_selection_set_font_name:
786 * @fontsel: a #GtkFontSelection
787 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
789 * Sets the currently-selected font.
791 * Note that the @fontsel needs to know the screen in which it will appear
792 * for this to work; this can be guaranteed by simply making sure that the
793 * @fontsel is inserted in a toplevel window before you call this function.
795 * Return value: %TRUE if the font could be set successfully; %FALSE if no
796 * such font exists or if the @fontsel doesn't belong to a particular
800 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
801 const gchar *fontname)
804 PangoFontFamily *family = NULL;
805 PangoFontFace *face = NULL;
806 PangoFontDescription *new_desc;
809 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
815 * gtk_font_selection_get_preview_text:
816 * @fontsel: a #GtkFontSelection
818 * Gets the text displayed in the preview area.
820 * Return value: the text displayed in the preview area.
821 * This string is owned by the widget and should not be
824 G_CONST_RETURN gchar*
825 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
832 * gtk_font_selection_set_preview_text:
833 * @fontsel: a #GtkFontSelection
834 * @text: the text to display in the preview area
836 * Sets the text displayed in the preview area.
837 * The @text is used to show how the selected font looks.
840 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
844 GtkFontSelectionPrivate *priv;
846 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
847 g_return_if_fail (text != NULL);
849 priv = fontsel->priv;
855 * SECTION:gtkfontseldlg
856 * @Short_description: A dialog box for selecting fonts
857 * @Title: GtkFontSelectionDialog
858 * @See_also: #GtkFontSelection, #GtkDialog
860 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
862 * To set the font which is initially selected, use
863 * gtk_font_selection_dialog_set_font_name().
865 * To get the selected font use gtk_font_selection_dialog_get_font_name().
867 * To change the text which is shown in the preview area, use
868 * gtk_font_selection_dialog_set_preview_text().
870 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
871 * <title>GtkFontSelectionDialog as GtkBuildable</title>
872 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
873 * exposes the embedded #GtkFontSelection as internal child with the
874 * name "font_selection". It also exposes the buttons with the names
875 * "ok_button", "cancel_button" and "apply_button".
879 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
880 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
882 const gchar *childname);
884 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
886 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
887 gtk_font_selection_dialog_buildable_interface_init))
889 static GtkBuildableIface *parent_buildable_iface;
892 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
894 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
898 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
900 GtkFontSelectionDialogPrivate *priv;
901 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
902 GtkWidget *action_area, *content_area;
904 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
905 GTK_TYPE_FONT_SELECTION_DIALOG,
906 GtkFontSelectionDialogPrivate);
907 priv = fontseldiag->priv;
909 content_area = gtk_dialog_get_content_area (dialog);
910 action_area = gtk_dialog_get_action_area (dialog);
912 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
913 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
914 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
915 gtk_box_set_spacing (GTK_BOX (action_area), 6);
917 gtk_widget_push_composite_child ();
919 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
921 /* Create the content area */
922 priv->fontsel = gtk_font_selection_new ();
923 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
924 gtk_widget_show (priv->fontsel);
925 gtk_box_pack_start (GTK_BOX (content_area),
926 priv->fontsel, TRUE, TRUE, 0);
928 /* Create the action area */
929 priv->cancel_button = gtk_dialog_add_button (dialog,
931 GTK_RESPONSE_CANCEL);
933 priv->apply_button = gtk_dialog_add_button (dialog,
936 gtk_widget_hide (priv->apply_button);
938 priv->ok_button = gtk_dialog_add_button (dialog,
941 gtk_widget_grab_default (priv->ok_button);
943 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
949 gtk_window_set_title (GTK_WINDOW (fontseldiag),
950 _("Font Selection"));
952 gtk_widget_pop_composite_child ();
956 * gtk_font_selection_dialog_new:
957 * @title: the title of the dialog window
959 * Creates a new #GtkFontSelectionDialog.
961 * Return value: a new #GtkFontSelectionDialog
964 gtk_font_selection_dialog_new (const gchar *title)
966 GtkFontSelectionDialog *fontseldiag;
968 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
971 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
973 return GTK_WIDGET (fontseldiag);
977 * gtk_font_selection_dialog_get_font_selection:
978 * @fsd: a #GtkFontSelectionDialog
980 * Retrieves the #GtkFontSelection widget embedded in the dialog.
982 * Returns: (transfer none): the embedded #GtkFontSelection
987 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
989 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
991 return fsd->priv->fontsel;
996 * gtk_font_selection_dialog_get_ok_button:
997 * @fsd: a #GtkFontSelectionDialog
999 * Gets the 'OK' button.
1001 * Return value: (transfer none): the #GtkWidget used in the dialog
1002 * for the 'OK' button.
1007 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1009 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1011 return fsd->priv->ok_button;
1015 * gtk_font_selection_dialog_get_cancel_button:
1016 * @fsd: a #GtkFontSelectionDialog
1018 * Gets the 'Cancel' button.
1020 * Return value: (transfer none): the #GtkWidget used in the dialog
1021 * for the 'Cancel' button.
1026 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1028 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1030 return fsd->priv->cancel_button;
1034 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1036 parent_buildable_iface = g_type_interface_peek_parent (iface);
1037 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1041 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1042 GtkBuilder *builder,
1043 const gchar *childname)
1045 GtkFontSelectionDialogPrivate *priv;
1047 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1049 if (g_strcmp0 (childname, "ok_button") == 0)
1050 return G_OBJECT (priv->ok_button);
1051 else if (g_strcmp0 (childname, "cancel_button") == 0)
1052 return G_OBJECT (priv->cancel_button);
1053 else if (g_strcmp0 (childname, "apply_button") == 0)
1054 return G_OBJECT (priv->apply_button);
1055 else if (g_strcmp0 (childname, "font_selection") == 0)
1056 return G_OBJECT (priv->fontsel);
1058 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1062 * gtk_font_selection_dialog_get_font_name:
1063 * @fsd: a #GtkFontSelectionDialog
1065 * Gets the currently-selected font name.
1067 * Note that this can be a different string than what you set with
1068 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1069 * may normalize font names and thus return a string with a different
1070 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1071 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1072 * if you want to compare two font descriptions.
1074 * Return value: A string with the name of the current font, or %NULL if no
1075 * font is selected. You must free this string with g_free().
1078 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1080 GtkFontSelectionDialogPrivate *priv;
1082 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1086 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1090 * gtk_font_selection_dialog_set_font_name:
1091 * @fsd: a #GtkFontSelectionDialog
1092 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1094 * Sets the currently selected font.
1096 * Return value: %TRUE if the font selected in @fsd is now the
1097 * @fontname specified, %FALSE otherwise.
1100 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1101 const gchar *fontname)
1103 GtkFontSelectionDialogPrivate *priv;
1105 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1106 g_return_val_if_fail (fontname, FALSE);
1110 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1114 * gtk_font_selection_dialog_get_preview_text:
1115 * @fsd: a #GtkFontSelectionDialog
1117 * Gets the text displayed in the preview area.
1119 * Return value: the text displayed in the preview area.
1120 * This string is owned by the widget and should not be
1123 G_CONST_RETURN gchar*
1124 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1126 GtkFontSelectionDialogPrivate *priv;
1128 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1132 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1136 * gtk_font_selection_dialog_set_preview_text:
1137 * @fsd: a #GtkFontSelectionDialog
1138 * @text: the text to display in the preview area
1140 * Sets the text displayed in the preview area.
1143 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1146 GtkFontSelectionDialogPrivate *priv;
1148 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1149 g_return_if_fail (text != NULL);
1153 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);