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"
127 #define MAX_FONT_SIZE 999
129 /* This is the initial fixed height and the top padding of the preview entry */
130 #define PREVIEW_HEIGHT 84
131 #define PREVIEW_TOP_PADDING 6
133 /* These are the sizes of the font, style & size lists. */
134 #define FONT_LIST_HEIGHT 136
135 #define FONT_LIST_WIDTH 190
136 #define FONT_STYLE_LIST_WIDTH 170
137 #define FONT_SIZE_LIST_WIDTH 60
139 #define ROW_FORMAT_STRING "<span size=\"small\" foreground=\"%s\">%s <i>%s</i></span>\n<span size=\"large\" font_desc=\"%s\">%s</span>"
141 /* These are what we use as the standard font sizes, for the size list.
143 #define FONT_SIZES_LENGTH 25
144 static const guint16 font_sizes[] = {
145 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
146 32, 36, 40, 48, 56, 64, 72
163 static void gtk_font_selection_set_property (GObject *object,
167 static void gtk_font_selection_get_property (GObject *object,
171 static void gtk_font_selection_finalize (GObject *object);
172 static void gtk_font_selection_screen_changed (GtkWidget *widget,
173 GdkScreen *previous_screen);
174 static void gtk_font_selection_style_updated (GtkWidget *widget);
176 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
177 PangoFontFamily *family);
178 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
179 PangoFontFace *face);
180 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
182 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
185 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
187 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
188 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
190 gobject_class->finalize = gtk_font_selection_finalize;
191 gobject_class->set_property = gtk_font_selection_set_property;
192 gobject_class->get_property = gtk_font_selection_get_property;
194 widget_class->screen_changed = gtk_font_selection_screen_changed;
195 widget_class->style_updated = gtk_font_selection_style_updated;
197 g_object_class_install_property (gobject_class,
199 g_param_spec_string ("font-name",
201 P_("The string that represents this font"),
203 GTK_PARAM_READWRITE));
204 g_object_class_install_property (gobject_class,
206 g_param_spec_string ("preview-text",
208 P_("The text to display in order to demonstrate the selected font"),
210 GTK_PARAM_READWRITE));
212 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
216 gtk_font_selection_set_property (GObject *object,
221 GtkFontSelection *fontsel;
223 fontsel = GTK_FONT_SELECTION (object);
228 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
230 case PROP_PREVIEW_TEXT:
231 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 static void gtk_font_selection_get_property (GObject *object,
244 GtkFontSelection *fontsel;
246 fontsel = GTK_FONT_SELECTION (object);
251 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
253 case PROP_PREVIEW_TEXT:
254 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 deleted_text_cb (GtkEntryBuffer *buffer,
268 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
269 GtkWidget *entry = priv->search_entry;
271 if (gtk_entry_buffer_get_length (buffer) == 0)
273 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
274 GTK_ENTRY_ICON_SECONDARY,
278 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
282 inserted_text_cb (GtkEntryBuffer *buffer,
288 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
289 GtkWidget *entry = priv->search_entry;
291 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
293 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
294 GTK_ENTRY_ICON_SECONDARY,
298 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
302 icon_press_cb (GtkEntry *entry,
303 GtkEntryIconPosition pos,
307 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
309 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
313 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
315 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
317 gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
318 gtk_adjustment_get_value (adjustment));
322 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
324 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
326 priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
330 gtk_font_selection_init (GtkFontSelection *fontsel)
332 GtkFontSelectionPrivate *priv;
333 PangoFontDescription *font_desc;
334 GtkWidget *scrolled_win;
335 GtkWidget *alignment;
336 GtkWidget *preview_and_size;
337 GtkWidget *size_controls;
339 GList *focus_chain = NULL;
343 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
344 GTK_TYPE_FONT_SELECTION,
345 GtkFontSelectionPrivate);
346 priv = fontsel->priv;
347 gtk_widget_push_composite_child ();
349 /* Creating fundamental widgets for the private struct */
350 priv->search_entry = gtk_entry_new ();
351 priv->family_face_list = gtk_tree_view_new ();
352 priv->preview = gtk_entry_new ();
353 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
354 (gdouble) font_sizes[0],
355 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
358 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
360 /** Bootstrapping widget layout **/
361 /* Main font family/face view */
362 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
363 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
365 /* Alignment for the preview and size controls */
366 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
367 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
368 PREVIEW_TOP_PADDING, 0, 0, 0);
370 preview_and_size = gtk_vbox_new (TRUE, 0);
371 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
372 gtk_box_pack_start (GTK_BOX (preview_and_size), priv->preview, FALSE, TRUE, 0);
373 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT);
375 /* Packing the slider and the spin in a hbox */
376 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
377 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
378 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
379 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
381 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
382 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
384 /* Packing everything in the selection */
385 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
386 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
387 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
389 /* Getting the default size */
390 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
391 priv->size = pango_font_description_get_size (font_desc);
395 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
396 (gdouble)(priv->size / PANGO_SCALE));
397 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
398 (gdouble)(priv->size / PANGO_SCALE));
401 gtk_widget_show_all (GTK_WIDGET (fontsel));
402 gtk_widget_hide (GTK_WIDGET (fontsel));
404 /* Treeview column and model bootstrapping */
405 gtk_font_selection_bootstrap_fontlist (fontsel);
407 /* Set default preview text */
408 gtk_entry_set_text (GTK_ENTRY (priv->preview),
409 pango_language_get_sample_string (NULL));
411 /* Set search icon and place holder text */
412 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
413 GTK_ENTRY_ICON_SECONDARY,
415 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
417 /** Callback connections **/
418 /* Connect to callback for the live search text entry */
419 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
420 "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
421 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
422 "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
423 g_signal_connect (G_OBJECT (priv->search_entry),
424 "icon-press", G_CALLBACK (icon_press_cb), (gpointer)priv);
426 /* Size controls callbacks */
427 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
428 "value-changed", G_CALLBACK (slider_change_cb), (gpointer)priv);
429 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
430 "value-changed", G_CALLBACK (spin_change_cb), (gpointer)priv);
432 gtk_widget_pop_composite_child();
436 * gtk_font_selection_new:
438 * Creates a new #GtkFontSelection.
440 * Return value: a n ew #GtkFontSelection
443 gtk_font_selection_new (void)
445 GtkFontSelection *fontsel;
447 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
449 return GTK_WIDGET (fontsel);
453 cmp_families (const void *a, const void *b)
455 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
456 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
458 return g_utf8_collate (a_name, b_name);
462 set_cursor_to_iter (GtkTreeView *view,
465 GtkTreeModel *model = gtk_tree_view_get_model (view);
466 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
468 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
470 gtk_tree_path_free (path);
474 populate_list (GtkTreeView* treeview, GtkListStore* model)
476 GtkStyleContext *style_context;
481 GtkTreeIter match_row;
484 PangoFontFamily **families;
486 GString *tmp = g_string_new (NULL);
488 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
492 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
494 gtk_list_store_clear (model);
496 /* Get row header font color */
497 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
498 gtk_style_context_get_color (style_context,
499 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
502 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
503 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
504 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
505 color_string = pango_color_to_string (&p_color);
507 /* Iterate over families and faces */
508 for (i=0; i<n_families; i++)
511 PangoFontFace **faces;
513 const gchar *fam_name = pango_font_family_get_name (families[i]);
515 pango_font_family_list_faces (families[i], &faces, &n_faces);
517 for (j=0; j<n_faces; j++)
519 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
520 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
521 gchar *font_desc = pango_font_description_to_string (pango_desc);
523 /* foreground_color, family_name, face_name, desc, sample string */
524 g_string_printf (tmp, ROW_FORMAT_STRING,
532 gtk_list_store_append (model, &iter);
533 gtk_list_store_set (model, &iter,
534 FAMILY_COLUMN, families[i],
535 FACE_COLUMN, faces[j],
536 FAMILY_NAME_COLUMN, fam_name,
537 TEXT_COLUMN, tmp->str,
540 if ((i == 0 && j == 0) ||
541 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
546 pango_font_description_free(pango_desc);
553 set_cursor_to_iter (treeview, &match_row);
555 g_string_free (tmp, TRUE);
556 g_free (color_string);
561 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
563 gboolean result = FALSE;
564 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
566 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
568 gchar *font_name_casefold;
569 gchar *search_text_casefold;
571 gtk_tree_model_get (model, iter,
572 FAMILY_NAME_COLUMN, &font_name,
575 /* Covering some corner cases to speed up the result */
576 if ((font_name == NULL) ||
577 (strlen (search_text) > strlen (font_name)))
582 if (strlen (search_text) == 0)
588 font_name_casefold = g_utf8_casefold (font_name, -1);
589 search_text_casefold = g_utf8_casefold (search_text, -1);
591 if (g_strrstr (font_name_casefold, search_text_casefold))
594 g_free (search_text_casefold);
595 g_free (font_name_casefold);
601 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
603 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
604 GtkTreeViewColumn *col;
606 fontsel->priv->model = gtk_list_store_new (4,
607 PANGO_TYPE_FONT_FAMILY,
608 PANGO_TYPE_FONT_FACE,
612 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
614 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
616 (gpointer)fontsel->priv,
620 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
622 gtk_tree_view_set_rules_hint (treeview, TRUE);
623 gtk_tree_view_set_headers_visible (treeview, FALSE);
625 col = gtk_tree_view_column_new_with_attributes ("Family",
626 gtk_cell_renderer_text_new (),
627 "markup", TEXT_COLUMN,
629 gtk_tree_view_append_column (treeview, col);
631 populate_list (treeview, fontsel->priv->model);
636 gtk_font_selection_finalize (GObject *object)
638 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
640 gtk_font_selection_ref_family (fontsel, NULL);
641 gtk_font_selection_ref_face (fontsel, NULL);
643 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
647 gtk_font_selection_screen_changed (GtkWidget *widget,
648 GdkScreen *previous_screen)
654 gtk_font_selection_style_updated (GtkWidget *widget)
656 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
662 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
663 PangoFontFamily *family)
665 GtkFontSelectionPrivate *priv = fontsel->priv;
668 family = g_object_ref (family);
670 g_object_unref (priv->family);
671 priv->family = family;
675 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
678 GtkFontSelectionPrivate *priv = fontsel->priv;
681 face = g_object_ref (face);
683 g_object_unref (priv->face);
687 /*****************************************************************************
688 * These functions are the main public interface for getting/setting the font.
689 *****************************************************************************/
692 * gtk_font_selection_get_family_list:
693 * @fontsel: a #GtkFontSelection
695 * This returns the #GtkTreeView that lists font families, for
696 * example, 'Sans', 'Serif', etc.
698 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
703 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
705 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
711 * gtk_font_selection_get_face_list:
712 * @fontsel: a #GtkFontSelection
714 * This returns the #GtkTreeView which lists all styles available for
715 * the selected font. For example, 'Regular', 'Bold', etc.
717 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
722 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
724 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
730 * gtk_font_selection_get_size_entry:
731 * @fontsel: a #GtkFontSelection
733 * This returns the #GtkEntry used to allow the user to edit the font
734 * number manually instead of selecting it from the list of font sizes.
736 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
741 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
743 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
749 * gtk_font_selection_get_size_list:
750 * @fontsel: a #GtkFontSelection
752 * This returns the #GtkTreeeView used to list font sizes.
754 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
759 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
761 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
767 * gtk_font_selection_get_preview_entry:
768 * @fontsel: a #GtkFontSelection
770 * This returns the #GtkEntry used to display the font as a preview.
772 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
777 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
779 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
785 * gtk_font_selection_get_family:
786 * @fontsel: a #GtkFontSelection
788 * Gets the #PangoFontFamily representing the selected font family.
790 * Return value: (transfer none): A #PangoFontFamily representing the
791 * selected font family. Font families are a collection of font
792 * faces. The returned object is owned by @fontsel and must not
793 * be modified or freed.
798 gtk_font_selection_get_family (GtkFontSelection *fontsel)
800 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
806 * gtk_font_selection_get_face:
807 * @fontsel: a #GtkFontSelection
809 * Gets the #PangoFontFace representing the selected font group
810 * details (i.e. family, slant, weight, width, etc).
812 * Return value: (transfer none): A #PangoFontFace representing the
813 * selected font group details. The returned object is owned by
814 * @fontsel and must not be modified or freed.
819 gtk_font_selection_get_face (GtkFontSelection *fontsel)
821 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
827 * gtk_font_selection_get_size:
828 * @fontsel: a #GtkFontSelection
830 * The selected font size.
832 * Return value: A n integer representing the selected font size,
833 * or -1 if no font size is selected.
838 gtk_font_selection_get_size (GtkFontSelection *fontsel)
840 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
842 return fontsel->priv->size;
846 * gtk_font_selection_get_font_name:
847 * @fontsel: a #GtkFontSelection
849 * Gets the currently-selected font name.
851 * Note that this can be a different string than what you set with
852 * gtk_font_selection_set_font_name(), as the font selection widget may
853 * normalize font names and thus return a string with a different structure.
854 * For example, "Helvetica Italic Bold 12" could be normalized to
855 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
856 * if you want to compare two font descriptions.
858 * Return value: A string with the name of the current font, or %NULL if
859 * no font is selected. You must free this string with g_free().
862 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
867 /* This sets the current font, then selecting the appropriate list rows. */
870 * gtk_font_selection_set_font_name:
871 * @fontsel: a #GtkFontSelection
872 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
874 * Sets the currently-selected font.
876 * Note that the @fontsel needs to know the screen in which it will appear
877 * for this to work; this can be guaranteed by simply making sure that the
878 * @fontsel is inserted in a toplevel window before you call this function.
880 * Return value: %TRUE if the font could be set successfully; %FALSE if no
881 * such font exists or if the @fontsel doesn't belong to a particular
885 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
886 const gchar *fontname)
889 PangoFontFamily *family = NULL;
890 PangoFontFace *face = NULL;
891 PangoFontDescription *new_desc;
894 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
900 * gtk_font_selection_get_preview_text:
901 * @fontsel: a #GtkFontSelection
903 * Gets the text displayed in the preview area.
905 * Return value: the text displayed in the preview area.
906 * This string is owned by the widget and should not be
909 G_CONST_RETURN gchar*
910 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
917 * gtk_font_selection_set_preview_text:
918 * @fontsel: a #GtkFontSelection
919 * @text: the text to display in the preview area
921 * Sets the text displayed in the preview area.
922 * The @text is used to show how the selected font looks.
925 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
929 GtkFontSelectionPrivate *priv;
931 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
932 g_return_if_fail (text != NULL);
934 priv = fontsel->priv;
940 * SECTION:gtkfontseldlg
941 * @Short_description: A dialog box for selecting fonts
942 * @Title: GtkFontSelectionDialog
943 * @See_also: #GtkFontSelection, #GtkDialog
945 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
947 * To set the font which is initially selected, use
948 * gtk_font_selection_dialog_set_font_name().
950 * To get the selected font use gtk_font_selection_dialog_get_font_name().
952 * To change the text which is shown in the preview area, use
953 * gtk_font_selection_dialog_set_preview_text().
955 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
956 * <title>GtkFontSelectionDialog as GtkBuildable</title>
957 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
958 * exposes the embedded #GtkFontSelection as internal child with the
959 * name "font_selection". It also exposes the buttons with the names
960 * "ok_button", "cancel_button" and "apply_button".
964 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
965 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
967 const gchar *childname);
969 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
971 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
972 gtk_font_selection_dialog_buildable_interface_init))
974 static GtkBuildableIface *parent_buildable_iface;
977 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
979 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
983 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
985 GtkFontSelectionDialogPrivate *priv;
986 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
987 GtkWidget *action_area, *content_area;
989 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
990 GTK_TYPE_FONT_SELECTION_DIALOG,
991 GtkFontSelectionDialogPrivate);
992 priv = fontseldiag->priv;
994 content_area = gtk_dialog_get_content_area (dialog);
995 action_area = gtk_dialog_get_action_area (dialog);
997 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
998 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
999 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1000 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1002 gtk_widget_push_composite_child ();
1004 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1006 /* Create the content area */
1007 priv->fontsel = gtk_font_selection_new ();
1008 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1009 gtk_widget_show (priv->fontsel);
1010 gtk_box_pack_start (GTK_BOX (content_area),
1011 priv->fontsel, TRUE, TRUE, 0);
1013 /* Create the action area */
1014 priv->cancel_button = gtk_dialog_add_button (dialog,
1016 GTK_RESPONSE_CANCEL);
1018 priv->apply_button = gtk_dialog_add_button (dialog,
1020 GTK_RESPONSE_APPLY);
1021 gtk_widget_hide (priv->apply_button);
1023 priv->ok_button = gtk_dialog_add_button (dialog,
1026 gtk_widget_grab_default (priv->ok_button);
1028 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1031 GTK_RESPONSE_CANCEL,
1034 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1035 _("Font Selection"));
1037 gtk_widget_pop_composite_child ();
1041 * gtk_font_selection_dialog_new:
1042 * @title: the title of the dialog window
1044 * Creates a new #GtkFontSelectionDialog.
1046 * Return value: a new #GtkFontSelectionDialog
1049 gtk_font_selection_dialog_new (const gchar *title)
1051 GtkFontSelectionDialog *fontseldiag;
1053 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1056 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1058 return GTK_WIDGET (fontseldiag);
1062 * gtk_font_selection_dialog_get_font_selection:
1063 * @fsd: a #GtkFontSelectionDialog
1065 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1067 * Returns: (transfer none): the embedded #GtkFontSelection
1072 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1074 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1076 return fsd->priv->fontsel;
1081 * gtk_font_selection_dialog_get_ok_button:
1082 * @fsd: a #GtkFontSelectionDialog
1084 * Gets the 'OK' button.
1086 * Return value: (transfer none): the #GtkWidget used in the dialog
1087 * for the 'OK' button.
1092 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1094 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1096 return fsd->priv->ok_button;
1100 * gtk_font_selection_dialog_get_cancel_button:
1101 * @fsd: a #GtkFontSelectionDialog
1103 * Gets the 'Cancel' button.
1105 * Return value: (transfer none): the #GtkWidget used in the dialog
1106 * for the 'Cancel' button.
1111 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1113 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1115 return fsd->priv->cancel_button;
1119 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1121 parent_buildable_iface = g_type_interface_peek_parent (iface);
1122 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1126 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1127 GtkBuilder *builder,
1128 const gchar *childname)
1130 GtkFontSelectionDialogPrivate *priv;
1132 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1134 if (g_strcmp0 (childname, "ok_button") == 0)
1135 return G_OBJECT (priv->ok_button);
1136 else if (g_strcmp0 (childname, "cancel_button") == 0)
1137 return G_OBJECT (priv->cancel_button);
1138 else if (g_strcmp0 (childname, "apply_button") == 0)
1139 return G_OBJECT (priv->apply_button);
1140 else if (g_strcmp0 (childname, "font_selection") == 0)
1141 return G_OBJECT (priv->fontsel);
1143 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1147 * gtk_font_selection_dialog_get_font_name:
1148 * @fsd: a #GtkFontSelectionDialog
1150 * Gets the currently-selected font name.
1152 * Note that this can be a different string than what you set with
1153 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1154 * may normalize font names and thus return a string with a different
1155 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1156 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1157 * if you want to compare two font descriptions.
1159 * Return value: A string with the name of the current font, or %NULL if no
1160 * font is selected. You must free this string with g_free().
1163 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1165 GtkFontSelectionDialogPrivate *priv;
1167 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1171 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1175 * gtk_font_selection_dialog_set_font_name:
1176 * @fsd: a #GtkFontSelectionDialog
1177 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1179 * Sets the currently selected font.
1181 * Return value: %TRUE if the font selected in @fsd is now the
1182 * @fontname specified, %FALSE otherwise.
1185 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1186 const gchar *fontname)
1188 GtkFontSelectionDialogPrivate *priv;
1190 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1191 g_return_val_if_fail (fontname, FALSE);
1195 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1199 * gtk_font_selection_dialog_get_preview_text:
1200 * @fsd: a #GtkFontSelectionDialog
1202 * Gets the text displayed in the preview area.
1204 * Return value: the text displayed in the preview area.
1205 * This string is owned by the widget and should not be
1208 G_CONST_RETURN gchar*
1209 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1211 GtkFontSelectionDialogPrivate *priv;
1213 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1217 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1221 * gtk_font_selection_dialog_set_preview_text:
1222 * @fsd: a #GtkFontSelectionDialog
1223 * @text: the text to display in the preview area
1225 * Sets the text displayed in the preview area.
1228 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1231 GtkFontSelectionDialogPrivate *priv;
1233 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1234 g_return_if_fail (text != NULL);
1238 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);