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 14
146 static const gint font_sizes[] = {
147 6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
164 static void gtk_font_selection_set_property (GObject *object,
168 static void gtk_font_selection_get_property (GObject *object,
172 static void gtk_font_selection_finalize (GObject *object);
173 static void gtk_font_selection_screen_changed (GtkWidget *widget,
174 GdkScreen *previous_screen);
175 static void gtk_font_selection_style_updated (GtkWidget *widget);
177 static void gtk_font_selection_ref_family (GtkFontSelection *fontsel,
178 PangoFontFamily *family);
179 static void gtk_font_selection_ref_face (GtkFontSelection *fontsel,
180 PangoFontFace *face);
181 static void gtk_font_selection_bootstrap_fontlist (GtkFontSelection *fontsel);
183 G_DEFINE_TYPE (GtkFontSelection, gtk_font_selection, GTK_TYPE_VBOX)
186 gtk_font_selection_class_init (GtkFontSelectionClass *klass)
188 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
191 gobject_class->finalize = gtk_font_selection_finalize;
192 gobject_class->set_property = gtk_font_selection_set_property;
193 gobject_class->get_property = gtk_font_selection_get_property;
195 widget_class->screen_changed = gtk_font_selection_screen_changed;
196 widget_class->style_updated = gtk_font_selection_style_updated;
198 g_object_class_install_property (gobject_class,
200 g_param_spec_string ("font-name",
202 P_("The string that represents this font"),
204 GTK_PARAM_READWRITE));
205 g_object_class_install_property (gobject_class,
207 g_param_spec_string ("preview-text",
209 P_("The text to display in order to demonstrate the selected font"),
211 GTK_PARAM_READWRITE));
213 g_type_class_add_private (klass, sizeof (GtkFontSelectionPrivate));
217 gtk_font_selection_set_property (GObject *object,
222 GtkFontSelection *fontsel;
224 fontsel = GTK_FONT_SELECTION (object);
229 gtk_font_selection_set_font_name (fontsel, g_value_get_string (value));
231 case PROP_PREVIEW_TEXT:
232 gtk_font_selection_set_preview_text (fontsel, g_value_get_string (value));
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 static void gtk_font_selection_get_property (GObject *object,
245 GtkFontSelection *fontsel;
247 fontsel = GTK_FONT_SELECTION (object);
252 g_value_take_string (value, gtk_font_selection_get_font_name (fontsel));
254 case PROP_PREVIEW_TEXT:
255 g_value_set_string (value, gtk_font_selection_get_preview_text (fontsel));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 deleted_text_cb (GtkEntryBuffer *buffer,
269 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
270 GtkWidget *entry = priv->search_entry;
272 if (gtk_entry_buffer_get_length (buffer) == 0)
274 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
275 GTK_ENTRY_ICON_SECONDARY,
279 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
283 inserted_text_cb (GtkEntryBuffer *buffer,
289 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)user_data;
290 GtkWidget *entry = priv->search_entry;
292 if (g_strcmp0 (gtk_entry_get_icon_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY),
294 gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
295 GTK_ENTRY_ICON_SECONDARY,
299 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->filter));
303 icon_press_cb (GtkEntry *entry,
304 GtkEntryIconPosition pos,
308 gtk_entry_buffer_delete_text (gtk_entry_get_buffer (entry), 0, -1);
312 slider_change_cb (GtkAdjustment *adjustment, gpointer data)
314 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
316 /* If we set the silder value manually, we ignore this callback */
317 if (priv->ignore_slider)
319 priv->ignore_slider = FALSE;
323 gtk_adjustment_set_value (gtk_spin_button_get_adjustment( GTK_SPIN_BUTTON(priv->size_spin)),
324 gtk_adjustment_get_value (adjustment));
328 spin_change_cb (GtkAdjustment *adjustment, gpointer data)
330 PangoFontDescription *desc;
331 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*)data;
333 gdouble size = gtk_adjustment_get_value (adjustment);
335 GtkAdjustment *slider_adj = gtk_range_get_adjustment (GTK_RANGE (priv->size_slider));
337 /* We ignore the slider value change callback for both of this set_value call */
338 if (size < gtk_adjustment_get_lower (slider_adj))
340 priv->ignore_slider = TRUE;
341 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_lower (slider_adj));
343 else if (size > gtk_adjustment_get_upper (slider_adj))
345 priv->ignore_slider = TRUE;
346 gtk_adjustment_set_value (slider_adj, gtk_adjustment_get_upper (slider_adj));
349 priv->size = ((gint)gtk_adjustment_get_value (adjustment)) * PANGO_SCALE;
351 desc = pango_context_get_font_description (gtk_widget_get_pango_context (priv->preview));
352 pango_font_description_set_size (desc, priv->size);
353 gtk_widget_override_font (priv->preview, desc);
355 gtk_widget_queue_draw (priv->preview);
359 set_range_marks (GtkWidget* size_slider, gint* sizes, gint length)
363 gtk_scale_clear_marks (GTK_SCALE (size_slider));
365 for (i=0; i<length; i++)
366 gtk_scale_add_mark (GTK_SCALE (size_slider),
368 GTK_POS_BOTTOM, NULL);
372 cursor_changed_cb (GtkTreeView *treeview, gpointer data)
375 PangoFontFamily *family;
377 PangoFontDescription *desc;
383 GtkTreePath *path = gtk_tree_path_new ();
385 GtkFontSelection *fontsel = (GtkFontSelection*)data;
387 gtk_tree_view_get_cursor (treeview, &path, NULL);
392 if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (fontsel->priv->model), &iter, path))
395 gtk_tree_model_get (GTK_TREE_MODEL (fontsel->priv->model), &iter,
397 FAMILY_COLUMN, &family,
398 FAMILY_NAME_COLUMN, &family_name,
400 gtk_tree_path_free (path);
403 if (!face || !family_name || !family)
405 g_free (family_name);
406 g_object_unref (face);
407 g_object_unref (family);
411 desc = pango_font_face_describe (face);
412 pango_font_description_set_size (desc, fontsel->priv->size);
413 gtk_widget_override_font (fontsel->priv->preview, desc);
415 pango_font_face_list_sizes (face, &sizes, &n_sizes);
416 /* It seems not many fonts actually have a sane set of sizes */
417 /* set_range_marks (priv->size_slider, sizes, n_sizes); */
419 gtk_font_selection_ref_family (fontsel, family);
420 gtk_font_selection_ref_face (fontsel, face);
423 g_free (family_name);
424 g_object_unref ((gpointer)face);
425 pango_font_description_free(desc);
429 gtk_font_selection_init (GtkFontSelection *fontsel)
431 GtkFontSelectionPrivate *priv;
432 PangoFontDescription *font_desc;
433 GtkWidget *scrolled_win;
434 GtkWidget *alignment;
435 GtkWidget *preview_and_size;
436 GtkWidget *size_controls;
438 GList *focus_chain = NULL;
442 fontsel->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontsel,
443 GTK_TYPE_FONT_SELECTION,
444 GtkFontSelectionPrivate);
445 priv = fontsel->priv;
446 gtk_widget_push_composite_child ();
448 /* Creating fundamental widgets for the private struct */
449 priv->search_entry = gtk_entry_new ();
450 priv->family_face_list = gtk_tree_view_new ();
451 priv->preview = gtk_entry_new ();
452 priv->size_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL,
453 (gdouble) font_sizes[0],
454 (gdouble) font_sizes[FONT_SIZES_LENGTH - 1],
457 priv->size_spin = gtk_spin_button_new_with_range (0.0, (gdouble)(G_MAXINT / PANGO_SCALE), 1.0);
459 /** Bootstrapping widget layout **/
460 gtk_box_set_spacing (GTK_BOX (fontsel), 6);
461 gtk_box_pack_start (GTK_BOX (fontsel), priv->search_entry, FALSE, TRUE, 0);
463 /* Main font family/face view */
464 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
465 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
466 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
467 gtk_container_add (GTK_CONTAINER (scrolled_win), priv->family_face_list);
469 /* Alignment for the preview and size controls */
470 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
471 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
472 PREVIEW_TOP_PADDING, 0, 0, 0);
473 gtk_box_pack_start (GTK_BOX (fontsel), scrolled_win, TRUE, TRUE, 0);
475 preview_and_size = gtk_vbox_new (TRUE, 0);
476 gtk_box_set_homogeneous (GTK_BOX (preview_and_size), FALSE);
478 /* The preview entry needs a scrolled window to make sure we have a */
479 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
480 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
481 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
482 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_win),
484 gtk_box_pack_start (GTK_BOX (preview_and_size), scrolled_win, FALSE, FALSE, 0);
486 /* Setting the size requests for various widgets */
487 gtk_widget_set_size_request (GTK_WIDGET (fontsel), -1, 460);
488 gtk_widget_set_size_request (scrolled_win, -1, PREVIEW_HEIGHT);
489 gtk_widget_set_size_request (priv->preview, -1, PREVIEW_HEIGHT - 6);
491 /* Unset the frame on the preview entry and set a shadow in the scrolled window */
492 gtk_entry_set_has_frame (GTK_ENTRY (priv->preview), FALSE);
493 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
496 /* Packing the slider and the spin in a hbox */
497 size_controls = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
498 gtk_scale_set_draw_value (GTK_SCALE (priv->size_slider), FALSE);
499 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_slider, TRUE, TRUE, 0);
500 gtk_box_pack_start (GTK_BOX (size_controls), priv->size_spin, FALSE, TRUE, 0);
502 gtk_widget_set_valign (priv->size_spin, GTK_ALIGN_START);
504 gtk_box_pack_start (GTK_BOX (preview_and_size), size_controls, FALSE, FALSE, 0);
505 gtk_container_add (GTK_CONTAINER (alignment), preview_and_size);
507 gtk_box_pack_start (GTK_BOX (fontsel), GTK_WIDGET(alignment), FALSE, TRUE, 0);
509 /* Getting the default size */
510 font_desc = pango_context_get_font_description (gtk_widget_get_pango_context (GTK_WIDGET (fontsel)));
511 priv->size = pango_font_description_get_size (font_desc);
515 gtk_adjustment_set_value (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider)),
516 (gdouble)(priv->size / PANGO_SCALE));
517 gtk_adjustment_set_value (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin)),
518 (gdouble)(priv->size / PANGO_SCALE));
520 gtk_widget_show_all (GTK_WIDGET (fontsel));
521 gtk_widget_hide (GTK_WIDGET (fontsel));
523 /* Treeview column and model bootstrapping */
524 gtk_font_selection_bootstrap_fontlist (fontsel);
526 /* Set default preview text */
527 gtk_entry_set_text (GTK_ENTRY (priv->preview),
528 pango_language_get_sample_string (NULL));
530 /* Set search icon and place holder text */
531 gtk_entry_set_icon_from_stock (GTK_ENTRY (priv->search_entry),
532 GTK_ENTRY_ICON_SECONDARY,
534 gtk_entry_set_placeholder_text (GTK_ENTRY (priv->search_entry), _("Search font name"));
536 /** Callback connections **/
537 /* Connect to callback for the live search text entry */
538 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
539 "deleted-text", G_CALLBACK (deleted_text_cb), (gpointer)priv);
540 g_signal_connect (G_OBJECT (gtk_entry_get_buffer (GTK_ENTRY (priv->search_entry))),
541 "inserted-text", G_CALLBACK (inserted_text_cb), (gpointer)priv);
542 g_signal_connect (G_OBJECT (priv->search_entry),
543 "icon-press", G_CALLBACK (icon_press_cb), (gpointer)priv);
545 /* Size controls callbacks */
546 g_signal_connect (G_OBJECT (gtk_range_get_adjustment (GTK_RANGE (priv->size_slider))),
547 "value-changed", G_CALLBACK (slider_change_cb), (gpointer)priv);
548 g_signal_connect (G_OBJECT (gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (priv->size_spin))),
549 "value-changed", G_CALLBACK (spin_change_cb), (gpointer)priv);
550 priv->ignore_slider = FALSE;
552 /* Font selection callbacks */
553 g_signal_connect (G_OBJECT (priv->family_face_list), "cursor-changed",
554 G_CALLBACK (cursor_changed_cb), (gpointer)fontsel);
557 set_range_marks (priv->size_slider, (gint*)font_sizes, FONT_SIZES_LENGTH);
559 gtk_widget_pop_composite_child();
563 * gtk_font_selection_new:
565 * Creates a new #GtkFontSelection.
567 * Return value: a n ew #GtkFontSelection
570 gtk_font_selection_new (void)
572 GtkFontSelection *fontsel;
574 fontsel = g_object_new (GTK_TYPE_FONT_SELECTION, NULL);
576 return GTK_WIDGET (fontsel);
580 cmp_families (const void *a, const void *b)
582 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
583 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
585 return g_utf8_collate (a_name, b_name);
589 populate_list (GtkTreeView* treeview, GtkListStore* model)
591 GtkStyleContext *style_context;
596 GtkTreeIter match_row;
600 PangoFontFamily **families;
602 GString *tmp = g_string_new (NULL);
604 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (treeview)),
608 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
610 gtk_list_store_clear (model);
612 /* Get row header font color */
613 style_context = gtk_widget_get_style_context (GTK_WIDGET (treeview));
614 gtk_style_context_get_color (style_context,
615 GTK_STATE_FLAG_NORMAL |GTK_STATE_FLAG_INSENSITIVE,
618 p_color.red = (guint16)((gdouble)G_MAXUINT16 * g_color.red);
619 p_color.green = (guint16)((gdouble)G_MAXUINT16 * g_color.green);
620 p_color.blue = (guint16)((gdouble)G_MAXUINT16 * g_color.blue);
621 color_string = pango_color_to_string (&p_color);
623 /* Iterate over families and faces */
624 for (i=0; i<n_families; i++)
627 PangoFontFace **faces;
629 const gchar *fam_name = pango_font_family_get_name (families[i]);
631 pango_font_family_list_faces (families[i], &faces, &n_faces);
633 for (j=0; j<n_faces; j++)
635 PangoFontDescription *pango_desc = pango_font_face_describe (faces[j]);
636 const gchar *face_name = pango_font_face_get_face_name (faces[j]);
637 gchar *font_desc = pango_font_description_to_string (pango_desc);
639 /* foreground_color, family_name, face_name, desc, sample string */
640 g_string_printf (tmp, ROW_FORMAT_STRING,
648 gtk_list_store_append (model, &iter);
649 gtk_list_store_set (model, &iter,
650 FAMILY_COLUMN, families[i],
651 FACE_COLUMN, faces[j],
652 FAMILY_NAME_COLUMN, fam_name,
653 TEXT_COLUMN, tmp->str,
656 if ((i == 0 && j == 0) ||
657 (!g_ascii_strcasecmp (face_name, "sans") && j == 0))
662 pango_font_description_free(pango_desc);
669 path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &match_row);
670 gtk_tree_view_set_cursor (treeview, path, NULL, FALSE);
672 gtk_tree_path_free(path);
673 g_string_free (tmp, TRUE);
674 g_free (color_string);
679 visible_func (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
681 gboolean result = FALSE;
682 GtkFontSelectionPrivate *priv = (GtkFontSelectionPrivate*) data;
684 const gchar *search_text = (const gchar*)gtk_entry_get_text (GTK_ENTRY (priv->search_entry));
686 gchar *font_name_casefold;
687 gchar *search_text_casefold;
689 gtk_tree_model_get (model, iter,
690 FAMILY_NAME_COLUMN, &font_name,
693 /* Covering some corner cases to speed up the result */
694 if (font_name == NULL ||
695 strlen (search_text) > strlen (font_name))
700 if (strlen (search_text) == 0)
706 font_name_casefold = g_utf8_casefold (font_name, -1);
707 search_text_casefold = g_utf8_casefold (search_text, -1);
709 if (g_strrstr (font_name_casefold, search_text_casefold))
712 g_free (search_text_casefold);
713 g_free (font_name_casefold);
719 gtk_font_selection_bootstrap_fontlist (GtkFontSelection* fontsel)
721 GtkTreeView *treeview = GTK_TREE_VIEW (fontsel->priv->family_face_list);
722 GtkCellRenderer *cell;
723 GtkTreeViewColumn *col;
725 fontsel->priv->model = gtk_list_store_new (4,
726 PANGO_TYPE_FONT_FAMILY,
727 PANGO_TYPE_FONT_FACE,
731 fontsel->priv->filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (fontsel->priv->model),
733 g_object_unref (fontsel->priv->model);
735 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (fontsel->priv->filter),
737 (gpointer)fontsel->priv,
741 gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (fontsel->priv->filter));
742 g_object_unref (fontsel->priv->filter);
744 gtk_tree_view_set_rules_hint (treeview, TRUE);
745 gtk_tree_view_set_headers_visible (treeview, FALSE);
747 cell = gtk_cell_renderer_text_new ();
748 col = gtk_tree_view_column_new_with_attributes ("Family",
750 "markup", TEXT_COLUMN,
754 g_object_set (cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
756 gtk_tree_view_append_column (treeview, col);
758 populate_list (treeview, fontsel->priv->model);
763 gtk_font_selection_finalize (GObject *object)
765 GtkFontSelection *fontsel = GTK_FONT_SELECTION (object);
767 gtk_font_selection_ref_family (fontsel, NULL);
768 gtk_font_selection_ref_face (fontsel, NULL);
770 G_OBJECT_CLASS (gtk_font_selection_parent_class)->finalize (object);
774 gtk_font_selection_screen_changed (GtkWidget *widget,
775 GdkScreen *previous_screen)
781 gtk_font_selection_style_updated (GtkWidget *widget)
783 GTK_WIDGET_CLASS (gtk_font_selection_parent_class)->style_updated (widget);
789 gtk_font_selection_ref_family (GtkFontSelection *fontsel,
790 PangoFontFamily *family)
792 GtkFontSelectionPrivate *priv = fontsel->priv;
795 family = g_object_ref (family);
797 g_object_unref (priv->family);
798 priv->family = family;
802 gtk_font_selection_ref_face (GtkFontSelection *fontsel,
805 GtkFontSelectionPrivate *priv = fontsel->priv;
808 face = g_object_ref (face);
810 g_object_unref (priv->face);
814 /*****************************************************************************
815 * These functions are the main public interface for getting/setting the font.
816 *****************************************************************************/
819 * gtk_font_selection_get_family_list:
820 * @fontsel: a #GtkFontSelection
822 * This returns the #GtkTreeView that lists font families, for
823 * example, 'Sans', 'Serif', etc.
825 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
830 gtk_font_selection_get_family_list (GtkFontSelection *fontsel)
832 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
838 * gtk_font_selection_get_face_list:
839 * @fontsel: a #GtkFontSelection
841 * This returns the #GtkTreeView which lists all styles available for
842 * the selected font. For example, 'Regular', 'Bold', etc.
844 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
849 gtk_font_selection_get_face_list (GtkFontSelection *fontsel)
851 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
857 * gtk_font_selection_get_size_entry:
858 * @fontsel: a #GtkFontSelection
860 * This returns the #GtkEntry used to allow the user to edit the font
861 * number manually instead of selecting it from the list of font sizes.
863 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
868 gtk_font_selection_get_size_entry (GtkFontSelection *fontsel)
870 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
876 * gtk_font_selection_get_size_list:
877 * @fontsel: a #GtkFontSelection
879 * This returns the #GtkTreeeView used to list font sizes.
881 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
886 gtk_font_selection_get_size_list (GtkFontSelection *fontsel)
888 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
894 * gtk_font_selection_get_preview_entry:
895 * @fontsel: a #GtkFontSelection
897 * This returns the #GtkEntry used to display the font as a preview.
899 * Return value: (transfer none): A #GtkWidget that is part of @fontsel
904 gtk_font_selection_get_preview_entry (GtkFontSelection *fontsel)
906 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
912 * gtk_font_selection_get_family:
913 * @fontsel: a #GtkFontSelection
915 * Gets the #PangoFontFamily representing the selected font family.
917 * Return value: (transfer none): A #PangoFontFamily representing the
918 * selected font family. Font families are a collection of font
919 * faces. The returned object is owned by @fontsel and must not
920 * be modified or freed.
925 gtk_font_selection_get_family (GtkFontSelection *fontsel)
927 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
933 * gtk_font_selection_get_face:
934 * @fontsel: a #GtkFontSelection
936 * Gets the #PangoFontFace representing the selected font group
937 * details (i.e. family, slant, weight, width, etc).
939 * Return value: (transfer none): A #PangoFontFace representing the
940 * selected font group details. The returned object is owned by
941 * @fontsel and must not be modified or freed.
946 gtk_font_selection_get_face (GtkFontSelection *fontsel)
948 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), NULL);
954 * gtk_font_selection_get_size:
955 * @fontsel: a #GtkFontSelection
957 * The selected font size.
959 * Return value: A n integer representing the selected font size,
960 * or -1 if no font size is selected.
965 gtk_font_selection_get_size (GtkFontSelection *fontsel)
967 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), -1);
969 return fontsel->priv->size;
973 * gtk_font_selection_get_font_name:
974 * @fontsel: a #GtkFontSelection
976 * Gets the currently-selected font name.
978 * Note that this can be a different string than what you set with
979 * gtk_font_selection_set_font_name(), as the font selection widget may
980 * normalize font names and thus return a string with a different structure.
981 * For example, "Helvetica Italic Bold 12" could be normalized to
982 * "Helvetica Bold Italic 12". Use pango_font_description_equal()
983 * if you want to compare two font descriptions.
985 * Return value: A string with the name of the current font, or %NULL if
986 * no font is selected. You must free this string with g_free().
989 gtk_font_selection_get_font_name (GtkFontSelection *fontsel)
994 /* This sets the current font, then selecting the appropriate list rows. */
997 * gtk_font_selection_set_font_name:
998 * @fontsel: a #GtkFontSelection
999 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1001 * Sets the currently-selected font.
1003 * Note that the @fontsel needs to know the screen in which it will appear
1004 * for this to work; this can be guaranteed by simply making sure that the
1005 * @fontsel is inserted in a toplevel window before you call this function.
1007 * Return value: %TRUE if the font could be set successfully; %FALSE if no
1008 * such font exists or if the @fontsel doesn't belong to a particular
1012 gtk_font_selection_set_font_name (GtkFontSelection *fontsel,
1013 const gchar *fontname)
1016 PangoFontFamily *family = NULL;
1017 PangoFontFace *face = NULL;
1018 PangoFontDescription *new_desc;
1021 g_return_val_if_fail (GTK_IS_FONT_SELECTION (fontsel), FALSE);
1027 * gtk_font_selection_get_preview_text:
1028 * @fontsel: a #GtkFontSelection
1030 * Gets the text displayed in the preview area.
1032 * Return value: the text displayed in the preview area.
1033 * This string is owned by the widget and should not be
1036 G_CONST_RETURN gchar*
1037 gtk_font_selection_get_preview_text (GtkFontSelection *fontsel)
1044 * gtk_font_selection_set_preview_text:
1045 * @fontsel: a #GtkFontSelection
1046 * @text: the text to display in the preview area
1048 * Sets the text displayed in the preview area.
1049 * The @text is used to show how the selected font looks.
1052 gtk_font_selection_set_preview_text (GtkFontSelection *fontsel,
1056 GtkFontSelectionPrivate *priv;
1058 g_return_if_fail (GTK_IS_FONT_SELECTION (fontsel));
1059 g_return_if_fail (text != NULL);
1061 priv = fontsel->priv;
1067 * SECTION:gtkfontseldlg
1068 * @Short_description: A dialog box for selecting fonts
1069 * @Title: GtkFontSelectionDialog
1070 * @See_also: #GtkFontSelection, #GtkDialog
1072 * The #GtkFontSelectionDialog widget is a dialog box for selecting a font.
1074 * To set the font which is initially selected, use
1075 * gtk_font_selection_dialog_set_font_name().
1077 * To get the selected font use gtk_font_selection_dialog_get_font_name().
1079 * To change the text which is shown in the preview area, use
1080 * gtk_font_selection_dialog_set_preview_text().
1082 * <refsect2 id="GtkFontSelectionDialog-BUILDER-UI">
1083 * <title>GtkFontSelectionDialog as GtkBuildable</title>
1084 * The GtkFontSelectionDialog implementation of the GtkBuildable interface
1085 * exposes the embedded #GtkFontSelection as internal child with the
1086 * name "font_selection". It also exposes the buttons with the names
1087 * "ok_button", "cancel_button" and "apply_button".
1091 static void gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface);
1092 static GObject * gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1093 GtkBuilder *builder,
1094 const gchar *childname);
1096 G_DEFINE_TYPE_WITH_CODE (GtkFontSelectionDialog, gtk_font_selection_dialog,
1098 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
1099 gtk_font_selection_dialog_buildable_interface_init))
1101 static GtkBuildableIface *parent_buildable_iface;
1104 gtk_font_selection_dialog_class_init (GtkFontSelectionDialogClass *klass)
1106 g_type_class_add_private (klass, sizeof (GtkFontSelectionDialogPrivate));
1110 gtk_font_selection_dialog_init (GtkFontSelectionDialog *fontseldiag)
1112 GtkFontSelectionDialogPrivate *priv;
1113 GtkDialog *dialog = GTK_DIALOG (fontseldiag);
1114 GtkWidget *action_area, *content_area;
1116 fontseldiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontseldiag,
1117 GTK_TYPE_FONT_SELECTION_DIALOG,
1118 GtkFontSelectionDialogPrivate);
1119 priv = fontseldiag->priv;
1121 content_area = gtk_dialog_get_content_area (dialog);
1122 action_area = gtk_dialog_get_action_area (dialog);
1124 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
1125 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
1126 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
1127 gtk_box_set_spacing (GTK_BOX (action_area), 6);
1129 gtk_widget_push_composite_child ();
1131 gtk_window_set_resizable (GTK_WINDOW (fontseldiag), TRUE);
1133 /* Create the content area */
1134 priv->fontsel = gtk_font_selection_new ();
1135 gtk_container_set_border_width (GTK_CONTAINER (priv->fontsel), 5);
1136 gtk_widget_show (priv->fontsel);
1137 gtk_box_pack_start (GTK_BOX (content_area),
1138 priv->fontsel, TRUE, TRUE, 0);
1140 /* Create the action area */
1141 priv->cancel_button = gtk_dialog_add_button (dialog,
1143 GTK_RESPONSE_CANCEL);
1145 priv->apply_button = gtk_dialog_add_button (dialog,
1147 GTK_RESPONSE_APPLY);
1148 gtk_widget_hide (priv->apply_button);
1150 priv->ok_button = gtk_dialog_add_button (dialog,
1153 gtk_widget_grab_default (priv->ok_button);
1155 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontseldiag),
1158 GTK_RESPONSE_CANCEL,
1161 gtk_window_set_title (GTK_WINDOW (fontseldiag),
1162 _("Font Selection"));
1164 gtk_widget_pop_composite_child ();
1168 * gtk_font_selection_dialog_new:
1169 * @title: the title of the dialog window
1171 * Creates a new #GtkFontSelectionDialog.
1173 * Return value: a new #GtkFontSelectionDialog
1176 gtk_font_selection_dialog_new (const gchar *title)
1178 GtkFontSelectionDialog *fontseldiag;
1180 fontseldiag = g_object_new (GTK_TYPE_FONT_SELECTION_DIALOG, NULL);
1183 gtk_window_set_title (GTK_WINDOW (fontseldiag), title);
1185 return GTK_WIDGET (fontseldiag);
1189 * gtk_font_selection_dialog_get_font_selection:
1190 * @fsd: a #GtkFontSelectionDialog
1192 * Retrieves the #GtkFontSelection widget embedded in the dialog.
1194 * Returns: (transfer none): the embedded #GtkFontSelection
1199 gtk_font_selection_dialog_get_font_selection (GtkFontSelectionDialog *fsd)
1201 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1203 return fsd->priv->fontsel;
1208 * gtk_font_selection_dialog_get_ok_button:
1209 * @fsd: a #GtkFontSelectionDialog
1211 * Gets the 'OK' button.
1213 * Return value: (transfer none): the #GtkWidget used in the dialog
1214 * for the 'OK' button.
1219 gtk_font_selection_dialog_get_ok_button (GtkFontSelectionDialog *fsd)
1221 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1223 return fsd->priv->ok_button;
1227 * gtk_font_selection_dialog_get_cancel_button:
1228 * @fsd: a #GtkFontSelectionDialog
1230 * Gets the 'Cancel' button.
1232 * Return value: (transfer none): the #GtkWidget used in the dialog
1233 * for the 'Cancel' button.
1238 gtk_font_selection_dialog_get_cancel_button (GtkFontSelectionDialog *fsd)
1240 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1242 return fsd->priv->cancel_button;
1246 gtk_font_selection_dialog_buildable_interface_init (GtkBuildableIface *iface)
1248 parent_buildable_iface = g_type_interface_peek_parent (iface);
1249 iface->get_internal_child = gtk_font_selection_dialog_buildable_get_internal_child;
1253 gtk_font_selection_dialog_buildable_get_internal_child (GtkBuildable *buildable,
1254 GtkBuilder *builder,
1255 const gchar *childname)
1257 GtkFontSelectionDialogPrivate *priv;
1259 priv = GTK_FONT_SELECTION_DIALOG (buildable)->priv;
1261 if (g_strcmp0 (childname, "ok_button") == 0)
1262 return G_OBJECT (priv->ok_button);
1263 else if (g_strcmp0 (childname, "cancel_button") == 0)
1264 return G_OBJECT (priv->cancel_button);
1265 else if (g_strcmp0 (childname, "apply_button") == 0)
1266 return G_OBJECT (priv->apply_button);
1267 else if (g_strcmp0 (childname, "font_selection") == 0)
1268 return G_OBJECT (priv->fontsel);
1270 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
1274 * gtk_font_selection_dialog_get_font_name:
1275 * @fsd: a #GtkFontSelectionDialog
1277 * Gets the currently-selected font name.
1279 * Note that this can be a different string than what you set with
1280 * gtk_font_selection_dialog_set_font_name(), as the font selection widget
1281 * may normalize font names and thus return a string with a different
1282 * structure. For example, "Helvetica Italic Bold 12" could be normalized
1283 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
1284 * if you want to compare two font descriptions.
1286 * Return value: A string with the name of the current font, or %NULL if no
1287 * font is selected. You must free this string with g_free().
1290 gtk_font_selection_dialog_get_font_name (GtkFontSelectionDialog *fsd)
1292 GtkFontSelectionDialogPrivate *priv;
1294 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1298 return gtk_font_selection_get_font_name (GTK_FONT_SELECTION (priv->fontsel));
1302 * gtk_font_selection_dialog_set_font_name:
1303 * @fsd: a #GtkFontSelectionDialog
1304 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
1306 * Sets the currently selected font.
1308 * Return value: %TRUE if the font selected in @fsd is now the
1309 * @fontname specified, %FALSE otherwise.
1312 gtk_font_selection_dialog_set_font_name (GtkFontSelectionDialog *fsd,
1313 const gchar *fontname)
1315 GtkFontSelectionDialogPrivate *priv;
1317 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), FALSE);
1318 g_return_val_if_fail (fontname, FALSE);
1322 return gtk_font_selection_set_font_name (GTK_FONT_SELECTION (priv->fontsel), fontname);
1326 * gtk_font_selection_dialog_get_preview_text:
1327 * @fsd: a #GtkFontSelectionDialog
1329 * Gets the text displayed in the preview area.
1331 * Return value: the text displayed in the preview area.
1332 * This string is owned by the widget and should not be
1335 G_CONST_RETURN gchar*
1336 gtk_font_selection_dialog_get_preview_text (GtkFontSelectionDialog *fsd)
1338 GtkFontSelectionDialogPrivate *priv;
1340 g_return_val_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd), NULL);
1344 return gtk_font_selection_get_preview_text (GTK_FONT_SELECTION (priv->fontsel));
1348 * gtk_font_selection_dialog_set_preview_text:
1349 * @fsd: a #GtkFontSelectionDialog
1350 * @text: the text to display in the preview area
1352 * Sets the text displayed in the preview area.
1355 gtk_font_selection_dialog_set_preview_text (GtkFontSelectionDialog *fsd,
1358 GtkFontSelectionDialogPrivate *priv;
1360 g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (fsd));
1361 g_return_if_fail (text != NULL);
1365 gtk_font_selection_set_preview_text (GTK_FONT_SELECTION (priv->fontsel), text);