1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Massively updated for Pango by Owen Taylor, May 2000
5 * GtkFontSelection widget for Gtk+, by Damon Chaplin, May 1998.
6 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
7 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #include <glib/gprintf.h>
40 #include "gtkfontsel.h"
41 #include "gtkbutton.h"
42 #include "gtkcellrenderertext.h"
48 #include "gtkliststore.h"
52 #include "gtktreeselection.h"
53 #include "gtktreeview.h"
55 #include "gtkscrolledwindow.h"
57 #include "gtkaccessible.h"
58 #include "gtkbuildable.h"
59 #include "gtkprivate.h"
60 #include "gtkalignment.h"
63 #include "gtkspinbutton.h"
64 #include "gtkwidget.h"
68 * @Short_description: A widget for selecting fonts
69 * @Title: GtkFontSelection
70 * @See_also: #GtkFontSelectionDialog
72 * The #GtkFontSelection widget lists the available fonts, styles and sizes,
73 * allowing the user to select a font.
74 * It is used in the #GtkFontSelectionDialog widget to provide a dialog box for
77 * To set the font which is initially selected, use
78 * gtk_font_selection_set_font_name().
80 * To get the selected font use gtk_font_selection_get_font_name().
82 * To change the text which is shown in the preview area, use
83 * gtk_font_selection_set_preview_text().
87 struct _GtkFontSelectionPrivate
89 GtkWidget *search_entry;
90 GtkWidget *family_face_list;
91 GtkWidget *size_slider;
100 PangoFontFamily *family;
102 gboolean ignore_slider;
106 struct _GtkFontSelectionDialogPrivate
110 GtkWidget *ok_button;
111 GtkWidget *apply_button;
112 GtkWidget *cancel_button;
116 /* We don't enable the font and style entries because they don't add
117 * much in terms of visible effect and have a weird effect on keynav.
118 * the Windows font selector has entries similarly positioned but they
119 * act in conjunction with the associated lists to form a single focus
122 #undef INCLUDE_FONT_ENTRIES
124 /* This is the default text shown in the preview entry, though the user
125 can set it. Remember that some fonts only have capital letters. */
126 #define PREVIEW_TEXT N_("abcdefghijk ABCDEFGHIJK")
128 #define DEFAULT_FONT_NAME "Sans 10"
129 #define MAX_FONT_SIZE 999
131 /* This is the initial fixed height and the top padding of the preview entry */
132 #define PREVIEW_HEIGHT 84
133 #define PREVIEW_TOP_PADDING 6
135 /* These are the sizes of the font, style & size lists. */
136 #define FONT_LIST_HEIGHT 136
137 #define FONT_LIST_WIDTH 190
138 #define FONT_STYLE_LIST_WIDTH 170
139 #define FONT_SIZE_LIST_WIDTH 60
141 #define ROW_FORMAT_STRING "<span size=\"small\" foreground=\"%s\">%s <i>%s</i></span>\n<span size=\"large\" font_desc=\"%s\">%s</span>"
143 /* These are what we use as the standard font sizes, for the size list.
145 #define FONT_SIZES_LENGTH 25
146 static const guint16 font_sizes[] = {
147 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28,
148 32, 36, 40, 48, 56, 64, 72
165 static void gtk_font_selection_set_property (GObject *object,
169 static void gtk_font_selection_get_property (GObject *object,
173 static void gtk_font_selection_finalize (GObject *object);
174 static void gtk_font_selection_screen_changed (GtkWidget *widget,
175 GdkScreen *previous_screen);
176 static void gtk_font_selection_style_updated (GtkWidget *widget);
178 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
179 PangoFontFamily *family);
180 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
181 PangoFontFace *face);
182 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
184 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
187 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
190 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
192 gobject_class->finalize = gtk_font_selection_finalize;
193 gobject_class->set_property = gtk_font_selection_set_property;
194 gobject_class->get_property = gtk_font_selection_get_property;
196 widget_class->screen_changed = gtk_font_selection_screen_changed;
197 widget_class->style_updated = gtk_font_selection_style_updated;
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("font-name",
203 P_("The string that represents this font"),
205 GTK_PARAM_READWRITE));
206 g_object_class_install_property (gobject_class,
208 g_param_spec_string ("preview-text",
210 P_("The text to display in order to demonstrate the selected font"),
212 GTK_PARAM_READWRITE));
214 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
218 gtk_font_selection_set_property (GObject *object,
223 GtkFontSelection *fontsel;
225 fontsel = GTK_FONT_SELECTION (object);
230 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
232 case PROP_PREVIEW_TEXT:
233 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 static void gtk_font_selection_get_property (GObject *object,
246 GtkFontSelection *fontsel;
248 fontsel = GTK_FONT_SELECTION (object);
253 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
255 case PROP_PREVIEW_TEXT:
256 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 deleted_text_cb (GtkEntryBuffer *buffer,
270 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
271 GtkWidget *entry = priv->search_entry;
273 if (gtk_entry_buffer_get_length (buffer) == 0)
275 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
276 GTK_ENTRY_ICON_SECONDARY,
280 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
284 inserted_text_cb (GtkEntryBuffer *buffer,
290 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
291 GtkWidget *entry = priv->search_entry;
293 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
295 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
296 GTK_ENTRY_ICON_SECONDARY,
300 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
304 icon_press_cb (GtkEntry *entry,
305 GtkEntryIconPosition pos,
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 /* If we set the silder value manually, we ignore this callback */
318 if (priv->ignore_slider)
320 priv->ignore_slider = FALSE;
324 gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
325 gtk_adjustment_get_value (adjustment));
329 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
331 PangoFontDescription *desc;
332 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
334 gdouble size = gtk_adjustment_get_value (adjustment);
336 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
338 /* We ignore the slider value change callback for both of this set_value call */
339 if (size < gtk_adjustment_get_lower (slider_adj))
341 priv->ignore_slider = TRUE;
342 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
344 else if (size > gtk_adjustment_get_upper (slider_adj))
346 priv->ignore_slider = TRUE;
347 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
350 priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
352 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
353 pango_font_description_set_size (desc, priv->size);
354 gtk_widget_modify_font (priv->preview, desc);
356 gtk_widget_queue_draw (priv->preview);
360 gtk_font_selection_init (GtkFontSelection *fontsel)
362 GtkFontSelectionPrivate *priv;
363 PangoFontDescription *font_desc;
364 GtkWidget *scrolled_win;
365 GtkWidget *alignment;
366 GtkWidget *preview_and_size;
367 GtkWidget *size_controls;
369 GList *focus_chain = NULL;
373 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
374 GTK_TYPE_FONT_SELECTION,
375 GtkFontSelectionPrivate);
376 priv = fontsel->priv;
377 gtk_widget_push_composite_child ();
379 /* Creating fundamental widgets for the private struct */
380 priv->search_entry = gtk_entry_new ();
381 priv->family_face_list = gtk_tree_view_new ();
382 priv->preview = gtk_entry_new ();
383 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
384 (gdouble) font_sizes[0],
385 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
388 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
390 /** Bootstrapping widget layout **/
391 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
393 /* Main font family/face view */
394 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
395 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
396 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
397 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
399 /* Alignment for the preview and size controls */
400 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
401 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
402 PREVIEW_TOP_PADDING, 0, 0, 0);
403 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
405 preview_and_size = gtk_vbox_new (TRUE, 0);
406 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
408 /* The preview entry needs a scrolled window to make sure we have a */
409 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
410 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
411 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
412 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
414 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
416 /* Setting the size requests for various widgets */
417 gtk_widget_set_size_request (priv->family_face_list, -1, 360);
418 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
419 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
421 /* Unset the frame on the preview entry and set a shadow in the scrolled window */
422 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
423 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
424 GTK_SHADOW_ETCHED_IN);
426 /* Packing the slider and the spin in a hbox */
427 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
428 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
429 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
430 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
432 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
433 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
435 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
437 /* Getting the default size */
438 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
439 priv->size = pango_font_description_get_size (font_desc);
443 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
444 (gdouble)(priv->size / PANGO_SCALE));
445 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
446 (gdouble)(priv->size / PANGO_SCALE));
448 gtk_widget_show_all (GTK_WIDGET (fontsel));
449 gtk_widget_hide (GTK_WIDGET (fontsel));
451 /* Treeview column and model bootstrapping */
452 gtk_font_selection_bootstrap_fontlist (fontsel);
454 /* Set default preview text */
455 gtk_entry_set_text (GTK_ENTRY (priv->preview),
456 pango_language_get_sample_string (NULL));
458 /* Set search icon and place holder text */
459 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
460 GTK_ENTRY_ICON_SECONDARY,
462 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
464 /** Callback connections **/
465 /* Connect to callback for the live search text entry */
466 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
467 "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
468 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
469 "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
470 g_signal_connect (G_OBJECT (priv->search_entry),
471 "icon-press", G_CALLBACK (icon_press_cb), (gpointer)priv);
473 /* Size controls callbacks */
474 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
475 "value-changed", G_CALLBACK (slider_change_cb), (gpointer)priv);
476 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
477 "value-changed", G_CALLBACK (spin_change_cb), (gpointer)priv);
478 priv->ignore_slider = FALSE;
480 gtk_widget_pop_composite_child();
484 * gtk_font_selection_new:
486 * Creates a new #GtkFontSelection.
488 * Return value: a n ew #GtkFontSelection
491 gtk_font_selection_new (void)
493 GtkFontSelection *fontsel;
495 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
497 return GTK_WIDGET (fontsel);
501 cmp_families (const void *a, const void *b)
503 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
504 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
506 return g_utf8_collate (a_name, b_name);
510 set_cursor_to_iter (GtkTreeView *view,
513 GtkTreeModel *model = gtk_tree_view_get_model (view);
514 GtkTreePath *path = gtk_tree_model_get_path (model, iter);
516 gtk_tree_view_set_cursor (view, path, NULL, FALSE);
518 gtk_tree_path_free (path);
522 populate_list (GtkTreeView* treeview, GtkListStore* model)
524 GtkStyleContext *style_context;
529 GtkTreeIter match_row;
532 PangoFontFamily **families;
534 GString *tmp = g_string_new (NULL);
536 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
540 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
542 gtk_list_store_clear (model);
544 /* Get row header font color */
545 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
546 gtk_style_context_get_color (style_context,
547 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
550 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
551 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
552 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
553 color_string = pango_color_to_string (&p_color);
555 /* Iterate over families and faces */
556 for (i=0; i<n_families; i++)
559 PangoFontFace **faces;
561 const gchar *fam_name = pango_font_family_get_name (families[i]);
563 pango_font_family_list_faces (families[i], &faces, &n_faces);
565 for (j=0; j<n_faces; j++)
567 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
568 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
569 gchar *font_desc = pango_font_description_to_string (pango_desc);
571 /* foreground_color, family_name, face_name, desc, sample string */
572 g_string_printf (tmp, ROW_FORMAT_STRING,
580 gtk_list_store_append (model, &iter);
581 gtk_list_store_set (model, &iter,
582 FAMILY_COLUMN, families[i],
583 FACE_COLUMN, faces[j],
584 FAMILY_NAME_COLUMN, fam_name,
585 TEXT_COLUMN, tmp->str,
588 if ((i == 0 && j == 0) ||
589 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
594 pango_font_description_free(pango_desc);
601 set_cursor_to_iter (treeview, &match_row);
603 g_string_free (tmp, TRUE);
604 g_free (color_string);
609 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
611 gboolean result = FALSE;
612 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
614 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
616 gchar *font_name_casefold;
617 gchar *search_text_casefold;
619 gtk_tree_model_get (model, iter,
620 FAMILY_NAME_COLUMN, &font_name,
623 /* Covering some corner cases to speed up the result */
624 if (font_name == NULL ||
625 strlen (search_text) > strlen (font_name))
630 if (strlen (search_text) == 0)
636 font_name_casefold = g_utf8_casefold (font_name, -1);
637 search_text_casefold = g_utf8_casefold (search_text, -1);
639 if (g_strrstr (font_name_casefold, search_text_casefold))
642 g_free (search_text_casefold);
643 g_free (font_name_casefold);
649 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
651 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
652 GtkTreeViewColumn *col;
654 fontsel->priv->model = gtk_list_store_new (4,
655 PANGO_TYPE_FONT_FAMILY,
656 PANGO_TYPE_FONT_FACE,
660 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
662 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
664 (gpointer)fontsel->priv,
668 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
670 gtk_tree_view_set_rules_hint (treeview, TRUE);
671 gtk_tree_view_set_headers_visible (treeview, FALSE);
673 col = gtk_tree_view_column_new_with_attributes ("Family",
674 gtk_cell_renderer_text_new (),
675 "markup", TEXT_COLUMN,
677 gtk_tree_view_append_column (treeview, col);
679 populate_list (treeview, fontsel->priv->model);
684 gtk_font_selection_finalize (GObject *object)
686 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
688 gtk_font_selection_ref_family (fontsel, NULL);
689 gtk_font_selection_ref_face (fontsel, NULL);
691 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
695 gtk_font_selection_screen_changed (GtkWidget *widget,
696 GdkScreen *previous_screen)
702 gtk_font_selection_style_updated (GtkWidget *widget)
704 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
710 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
711 PangoFontFamily *family)
713 GtkFontSelectionPrivate *priv = fontsel->priv;
716 family = g_object_ref (family);
718 g_object_unref (priv->family);
719 priv->family = family;
723 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
726 GtkFontSelectionPrivate *priv = fontsel->priv;
729 face = g_object_ref (face);
731 g_object_unref (priv->face);
735 /*****************************************************************************
736 * These functions are the main public interface for getting/setting the font.
737 *****************************************************************************/
740 * gtk_font_selection_get_family_list:
741 * @fontsel: a #GtkFontSelection
743 * This returns the #GtkTreeView that lists font families, for
744 * example, 'Sans', 'Serif', etc.
746 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
751 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
753 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
759 * gtk_font_selection_get_face_list:
760 * @fontsel: a #GtkFontSelection
762 * This returns the #GtkTreeView which lists all styles available for
763 * the selected font. For example, 'Regular', 'Bold', etc.
765 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
770 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
772 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
778 * gtk_font_selection_get_size_entry:
779 * @fontsel: a #GtkFontSelection
781 * This returns the #GtkEntry used to allow the user to edit the font
782 * number manually instead of selecting it from the list of font sizes.
784 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
789 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
791 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
797 * gtk_font_selection_get_size_list:
798 * @fontsel: a #GtkFontSelection
800 * This returns the #GtkTreeeView used to list font sizes.
802 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
807 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
809 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
815 * gtk_font_selection_get_preview_entry:
816 * @fontsel: a #GtkFontSelection
818 * This returns the #GtkEntry used to display the font as a preview.
820 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
825 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
827 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
833 * gtk_font_selection_get_family:
834 * @fontsel: a #GtkFontSelection
836 * Gets the #PangoFontFamily representing the selected font family.
838 * Return value: (transfer none): A #PangoFontFamily representing the
839 * selected font family. Font families are a collection of font
840 * faces. The returned object is owned by @fontsel and must not
841 * be modified or freed.
846 gtk_font_selection_get_family (GtkFontSelection *fontsel)
848 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
854 * gtk_font_selection_get_face:
855 * @fontsel: a #GtkFontSelection
857 * Gets the #PangoFontFace representing the selected font group
858 * details (i.e. family, slant, weight, width, etc).
860 * Return value: (transfer none): A #PangoFontFace representing the
861 * selected font group details. The returned object is owned by
862 * @fontsel and must not be modified or freed.
867 gtk_font_selection_get_face (GtkFontSelection *fontsel)
869 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
875 * gtk_font_selection_get_size:
876 * @fontsel: a #GtkFontSelection
878 * The selected font size.
880 * Return value: A n integer representing the selected font size,
881 * or -1 if no font size is selected.
886 gtk_font_selection_get_size (GtkFontSelection *fontsel)
888 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
890 return fontsel->priv->size;
894 * gtk_font_selection_get_font_name:
895 * @fontsel: a #GtkFontSelection
897 * Gets the currently-selected font name.
899 * Note that this can be a different string than what you set with
900 * gtk_font_selection_set_font_name(), as the font selection widget may
901 * normalize font names and thus return a string with a different structure.
902 * For example, "Helvetica Italic Bold 12" could be normalized to
903 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
904 * if you want to compare two font descriptions.
906 * Return value: A string with the name of the current font, or %NULL if
907 * no font is selected. You must free this string with g_free().
910 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
915 /* This sets the current font, then selecting the appropriate list rows. */
918 * gtk_font_selection_set_font_name:
919 * @fontsel: a #GtkFontSelection
920 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
922 * Sets the currently-selected font.
924 * Note that the @fontsel needs to know the screen in which it will appear
925 * for this to work; this can be guaranteed by simply making sure that the
926 * @fontsel is inserted in a toplevel window before you call this function.
928 * Return value: %TRUE if the font could be set successfully; %FALSE if no
929 * such font exists or if the @fontsel doesn't belong to a particular
933 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
934 const gchar *fontname)
937 PangoFontFamily *family = NULL;
938 PangoFontFace *face = NULL;
939 PangoFontDescription *new_desc;
942 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
948 * gtk_font_selection_get_preview_text:
949 * @fontsel: a #GtkFontSelection
951 * Gets the text displayed in the preview area.
953 * Return value: the text displayed in the preview area.
954 * This string is owned by the widget and should not be
957 G_CONST_RETURN gchar*
958 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
965 * gtk_font_selection_set_preview_text:
966 * @fontsel: a #GtkFontSelection
967 * @text: the text to display in the preview area
969 * Sets the text displayed in the preview area.
970 * The @text is used to show how the selected font looks.
973 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
977 GtkFontSelectionPrivate *priv;
979 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
980 g_return_if_fail (text != NULL);
982 priv = fontsel->priv;
988 * SECTION:gtkfontseldlg
989 * @Short_description: A dialog box for selecting fonts
990 * @Title: GtkFontSelectionDialog
991 * @See_also: #GtkFontSelection, #GtkDialog
993 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
995 * To set the font which is initially selected, use
996 * gtk_font_selection_dialog_set_font_name().
998 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1000 * To change the text which is shown in the preview area, use
1001 * gtk_font_selection_dialog_set_preview_text().
1003 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1004 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1005 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1006 * exposes the embedded #GtkFontSelection as internal child with the
1007 * name "font_selection". It also exposes the buttons with the names
1008 * "ok_button", "cancel_button" and "apply_button".
1012 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1013 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1014 GtkBuilder *builder,
1015 const gchar *childname);
1017 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1019 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1020 gtk_font_selection_dialog_buildable_interface_init))
1022 static GtkBuildableIface *parent_buildable_iface;
1025 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1027 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1031 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1033 GtkFontSelectionDialogPrivate *priv;
1034 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1035 GtkWidget *action_area, *content_area;
1037 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1038 GTK_TYPE_FONT_SELECTION_DIALOG,
1039 GtkFontSelectionDialogPrivate);
1040 priv = fontseldiag->priv;
1042 content_area = gtk_dialog_get_content_area (dialog);
1043 action_area = gtk_dialog_get_action_area (dialog);
1045 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1046 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1047 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1048 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1050 gtk_widget_push_composite_child ();
1052 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1054 /* Create the content area */
1055 priv->fontsel = gtk_font_selection_new ();
1056 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1057 gtk_widget_show (priv->fontsel);
1058 gtk_box_pack_start (GTK_BOX (content_area),
1059 priv->fontsel, TRUE, TRUE, 0);
1061 /* Create the action area */
1062 priv->cancel_button = gtk_dialog_add_button (dialog,
1064 GTK_RESPONSE_CANCEL);
1066 priv->apply_button = gtk_dialog_add_button (dialog,
1068 GTK_RESPONSE_APPLY);
1069 gtk_widget_hide (priv->apply_button);
1071 priv->ok_button = gtk_dialog_add_button (dialog,
1074 gtk_widget_grab_default (priv->ok_button);
1076 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1079 GTK_RESPONSE_CANCEL,
1082 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1083 _("Font Selection"));
1085 gtk_widget_pop_composite_child ();
1089 * gtk_font_selection_dialog_new:
1090 * @title: the title of the dialog window
1092 * Creates a new #GtkFontSelectionDialog.
1094 * Return value: a new #GtkFontSelectionDialog
1097 gtk_font_selection_dialog_new (const gchar *title)
1099 GtkFontSelectionDialog *fontseldiag;
1101 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1104 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1106 return GTK_WIDGET (fontseldiag);
1110 * gtk_font_selection_dialog_get_font_selection:
1111 * @fsd: a #GtkFontSelectionDialog
1113 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1115 * Returns: (transfer none): the embedded #GtkFontSelection
1120 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1122 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1124 return fsd->priv->fontsel;
1129 * gtk_font_selection_dialog_get_ok_button:
1130 * @fsd: a #GtkFontSelectionDialog
1132 * Gets the 'OK' button.
1134 * Return value: (transfer none): the #GtkWidget used in the dialog
1135 * for the 'OK' button.
1140 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1142 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1144 return fsd->priv->ok_button;
1148 * gtk_font_selection_dialog_get_cancel_button:
1149 * @fsd: a #GtkFontSelectionDialog
1151 * Gets the 'Cancel' button.
1153 * Return value: (transfer none): the #GtkWidget used in the dialog
1154 * for the 'Cancel' button.
1159 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1161 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1163 return fsd->priv->cancel_button;
1167 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1169 parent_buildable_iface = g_type_interface_peek_parent (iface);
1170 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1174 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1175 GtkBuilder *builder,
1176 const gchar *childname)
1178 GtkFontSelectionDialogPrivate *priv;
1180 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1182 if (g_strcmp0 (childname, "ok_button") == 0)
1183 return G_OBJECT (priv->ok_button);
1184 else if (g_strcmp0 (childname, "cancel_button") == 0)
1185 return G_OBJECT (priv->cancel_button);
1186 else if (g_strcmp0 (childname, "apply_button") == 0)
1187 return G_OBJECT (priv->apply_button);
1188 else if (g_strcmp0 (childname, "font_selection") == 0)
1189 return G_OBJECT (priv->fontsel);
1191 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1195 * gtk_font_selection_dialog_get_font_name:
1196 * @fsd: a #GtkFontSelectionDialog
1198 * Gets the currently-selected font name.
1200 * Note that this can be a different string than what you set with
1201 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1202 * may normalize font names and thus return a string with a different
1203 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1204 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1205 * if you want to compare two font descriptions.
1207 * Return value: A string with the name of the current font, or %NULL if no
1208 * font is selected. You must free this string with g_free().
1211 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1213 GtkFontSelectionDialogPrivate *priv;
1215 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1219 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1223 * gtk_font_selection_dialog_set_font_name:
1224 * @fsd: a #GtkFontSelectionDialog
1225 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1227 * Sets the currently selected font.
1229 * Return value: %TRUE if the font selected in @fsd is now the
1230 * @fontname specified, %FALSE otherwise.
1233 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1234 const gchar *fontname)
1236 GtkFontSelectionDialogPrivate *priv;
1238 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1239 g_return_val_if_fail (fontname, FALSE);
1243 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1247 * gtk_font_selection_dialog_get_preview_text:
1248 * @fsd: a #GtkFontSelectionDialog
1250 * Gets the text displayed in the preview area.
1252 * Return value: the text displayed in the preview area.
1253 * This string is owned by the widget and should not be
1256 G_CONST_RETURN gchar*
1257 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1259 GtkFontSelectionDialogPrivate *priv;
1261 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1265 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1269 * gtk_font_selection_dialog_set_preview_text:
1270 * @fsd: a #GtkFontSelectionDialog
1271 * @text: the text to display in the preview area
1273 * Sets the text displayed in the preview area.
1276 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1279 GtkFontSelectionDialogPrivate *priv;
1281 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1282 g_return_if_fail (text != NULL);
1286 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);