2 * GTK - The GIMP Toolkit
3 * Copyright (C) 1998 David Abilleira Freijeiro <odaf@nexo.es>
6 * Based on gnome-color-picker by Federico Mena <federico@nuclecu.unam.mx>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GTK+ Team and others 2003. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gtkfontbutton.h"
35 #include "gtkfontchooser.h"
36 #include "gtkfontchooserdialog.h"
38 #include "gtkmarshalers.h"
39 #include "gtkseparator.h"
40 #include "gtkprivate.h"
45 #include "gtkfontchooserutils.h"
49 * SECTION:gtkfontbutton
50 * @Short_description: A button to launch a font chooser dialog
51 * @Title: GtkFontButton
52 * @See_also: #GtkFontChooserDialog, #GtkColorButton.
54 * The #GtkFontButton is a button which displays the currently selected
55 * font an allows to open a font chooser dialog to change the font.
56 * It is suitable widget for selecting a font in a preference dialog.
60 struct _GtkFontButtonPrivate
70 guint show_preview_entry : 1;
72 GtkWidget *font_dialog;
74 GtkWidget *font_label;
75 GtkWidget *size_label;
77 PangoFontDescription *font_desc;
78 PangoFontFamily *font_family;
79 PangoFontFace *font_face;
82 GtkFontFilterFunc font_filter;
83 gpointer font_filter_data;
84 GDestroyNotify font_filter_data_destroy;
106 static void gtk_font_button_finalize (GObject *object);
107 static void gtk_font_button_get_property (GObject *object,
111 static void gtk_font_button_set_property (GObject *object,
116 static void gtk_font_button_clicked (GtkButton *button);
118 /* Dialog response functions */
119 static void response_cb (GtkDialog *dialog,
122 static void dialog_destroy (GtkWidget *widget,
125 /* Auxiliary functions */
126 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
127 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
128 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
130 static guint font_button_signals[LAST_SIGNAL] = { 0 };
133 clear_font_data (GtkFontButton *font_button)
135 GtkFontButtonPrivate *priv = font_button->priv;
137 if (priv->font_family)
138 g_object_unref (priv->font_family);
139 priv->font_family = NULL;
142 g_object_unref (priv->font_face);
143 priv->font_face = NULL;
146 pango_font_description_free (priv->font_desc);
147 priv->font_desc = NULL;
149 g_free (priv->fontname);
150 priv->fontname = NULL;
154 clear_font_filter_data (GtkFontButton *font_button)
156 GtkFontButtonPrivate *priv = font_button->priv;
158 if (priv->font_filter_data_destroy)
159 priv->font_filter_data_destroy (priv->font_filter_data);
160 priv->font_filter = NULL;
161 priv->font_filter_data = NULL;
162 priv->font_filter_data_destroy = NULL;
166 font_description_style_equal (const PangoFontDescription *a,
167 const PangoFontDescription *b)
169 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
170 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
171 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
172 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
176 gtk_font_button_update_font_data (GtkFontButton *font_button)
178 GtkFontButtonPrivate *priv = font_button->priv;
179 PangoFontFamily **families;
180 PangoFontFace **faces;
181 gint n_families, n_faces, i;
184 g_assert (priv->font_desc != NULL);
186 priv->fontname = pango_font_description_to_string (priv->font_desc);
188 family = pango_font_description_get_family (priv->font_desc);
194 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
195 &families, &n_families);
198 for (i = 0; i < n_families; i++)
200 const gchar *name = pango_font_family_get_name (families[i]);
202 if (!g_ascii_strcasecmp (name, family))
204 priv->font_family = g_object_ref (families[i]);
206 pango_font_family_list_faces (families[i], &faces, &n_faces);
212 for (i = 0; i < n_faces; i++)
214 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
216 if (font_description_style_equal (tmp_desc, priv->font_desc))
218 priv->font_face = g_object_ref (faces[i]);
220 pango_font_description_free (tmp_desc);
224 pango_font_description_free (tmp_desc);
231 gtk_font_button_get_preview_text (GtkFontButton *font_button)
233 GtkFontButtonPrivate *priv = font_button->priv;
235 if (priv->font_dialog)
236 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
238 return g_strdup (priv->preview_text);
242 gtk_font_button_set_preview_text (GtkFontButton *font_button,
243 const gchar *preview_text)
245 GtkFontButtonPrivate *priv = font_button->priv;
247 if (priv->font_dialog)
249 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
254 g_free (priv->preview_text);
255 priv->preview_text = g_strdup (preview_text);
260 gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
262 GtkFontButtonPrivate *priv = font_button->priv;
264 if (priv->font_dialog)
265 return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
267 return priv->show_preview_entry;
271 gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
274 GtkFontButtonPrivate *priv = font_button->priv;
276 if (priv->font_dialog)
277 gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
279 priv->show_preview_entry = show != FALSE;
282 static PangoFontFamily *
283 gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
285 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
286 GtkFontButtonPrivate *priv = font_button->priv;
288 return priv->font_family;
291 static PangoFontFace *
292 gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
294 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
295 GtkFontButtonPrivate *priv = font_button->priv;
297 return priv->font_face;
301 gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
303 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
304 GtkFontButtonPrivate *priv = font_button->priv;
306 return priv->font_size;
310 gtk_font_button_font_chooser_set_filter_func (GtkFontChooser *chooser,
311 GtkFontFilterFunc filter_func,
312 gpointer filter_data,
313 GDestroyNotify data_destroy)
315 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
316 GtkFontButtonPrivate *priv = font_button->priv;
318 if (priv->font_dialog)
320 gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
327 clear_font_filter_data (font_button);
328 priv->font_filter = filter_func;
329 priv->font_filter_data = filter_data;
330 priv->font_filter_data_destroy = data_destroy;
334 gtk_font_button_take_font_desc (GtkFontButton *font_button,
335 PangoFontDescription *font_desc)
337 GtkFontButtonPrivate *priv = font_button->priv;
338 GObject *object = G_OBJECT (font_button);
340 if (priv->font_desc && font_desc &&
341 pango_font_description_equal (priv->font_desc, font_desc))
343 pango_font_description_free (font_desc);
347 g_object_freeze_notify (object);
349 clear_font_data (font_button);
352 priv->font_desc = font_desc; /* adopted */
354 priv->font_desc = pango_font_description_from_string (_("Sans 12"));
356 if (pango_font_description_get_size_is_absolute (priv->font_desc))
357 priv->font_size = pango_font_description_get_size (priv->font_desc);
359 priv->font_size = pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
361 gtk_font_button_update_font_data (font_button);
362 gtk_font_button_update_font_info (font_button);
364 if (priv->font_dialog)
365 gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (priv->font_dialog),
368 g_object_notify (G_OBJECT (font_button), "font");
369 g_object_notify (G_OBJECT (font_button), "font-desc");
370 g_object_notify (G_OBJECT (font_button), "font-name");
372 g_object_thaw_notify (object);
375 static const PangoFontDescription *
376 gtk_font_button_get_font_desc (GtkFontButton *font_button)
378 return font_button->priv->font_desc;
382 gtk_font_button_font_chooser_notify (GObject *object,
386 /* We do not forward the notification of the "font" property to the dialog! */
387 if (pspec->name == I_("preview-text") ||
388 pspec->name == I_("show-preview-entry"))
389 g_object_notify_by_pspec (user_data, pspec);
393 gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
395 iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
396 iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
397 iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
398 iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
401 G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
402 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
403 gtk_font_button_font_chooser_iface_init))
406 gtk_font_button_class_init (GtkFontButtonClass *klass)
408 GObjectClass *gobject_class;
409 GtkButtonClass *button_class;
411 gobject_class = (GObjectClass *) klass;
412 button_class = (GtkButtonClass *) klass;
414 gobject_class->finalize = gtk_font_button_finalize;
415 gobject_class->set_property = gtk_font_button_set_property;
416 gobject_class->get_property = gtk_font_button_get_property;
418 button_class->clicked = gtk_font_button_clicked;
420 klass->font_set = NULL;
422 _gtk_font_chooser_install_properties (gobject_class);
425 * GtkFontButton:title:
427 * The title of the font chooser dialog.
431 g_object_class_install_property (gobject_class,
433 g_param_spec_string ("title",
435 P_("The title of the font chooser dialog"),
437 (GTK_PARAM_READABLE |
438 GTK_PARAM_WRITABLE)));
441 * GtkFontButton:font-name:
443 * The name of the currently selected font.
447 g_object_class_install_property (gobject_class,
449 g_param_spec_string ("font-name",
451 P_("The name of the selected font"),
453 (GTK_PARAM_READABLE |
454 GTK_PARAM_WRITABLE)));
457 * GtkFontButton:use-font:
459 * If this property is set to %TRUE, the label will be drawn
460 * in the selected font.
464 g_object_class_install_property (gobject_class,
466 g_param_spec_boolean ("use-font",
467 P_("Use font in label"),
468 P_("Whether the label is drawn in the selected font"),
470 GTK_PARAM_READWRITE));
473 * GtkFontButton:use-size:
475 * If this property is set to %TRUE, the label will be drawn
476 * with the selected font size.
480 g_object_class_install_property (gobject_class,
482 g_param_spec_boolean ("use-size",
483 P_("Use size in label"),
484 P_("Whether the label is drawn with the selected font size"),
486 GTK_PARAM_READWRITE));
489 * GtkFontButton:show-style:
491 * If this property is set to %TRUE, the name of the selected font style
492 * will be shown in the label. For a more WYSIWYG way to show the selected
493 * style, see the ::use-font property.
497 g_object_class_install_property (gobject_class,
499 g_param_spec_boolean ("show-style",
501 P_("Whether the selected font style is shown in the label"),
503 GTK_PARAM_READWRITE));
505 * GtkFontButton:show-size:
507 * If this property is set to %TRUE, the selected font size will be shown
508 * in the label. For a more WYSIWYG way to show the selected size, see the
509 * ::use-size property.
513 g_object_class_install_property (gobject_class,
515 g_param_spec_boolean ("show-size",
517 P_("Whether selected font size is shown in the label"),
519 GTK_PARAM_READWRITE));
522 * GtkFontButton::font-set:
523 * @widget: the object which received the signal.
525 * The ::font-set signal is emitted when the user selects a font.
526 * When handling this signal, use gtk_font_button_get_font_name()
527 * to find out which font was just selected.
529 * Note that this signal is only emitted when the <emphasis>user</emphasis>
530 * changes the font. If you need to react to programmatic font changes
531 * as well, use the notify::font-name signal.
535 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
536 G_TYPE_FROM_CLASS (gobject_class),
538 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
540 g_cclosure_marshal_VOID__VOID,
543 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
547 gtk_font_button_init (GtkFontButton *font_button)
549 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
550 GTK_TYPE_FONT_BUTTON,
551 GtkFontButtonPrivate);
553 /* Initialize fields */
554 font_button->priv->use_font = FALSE;
555 font_button->priv->use_size = FALSE;
556 font_button->priv->show_style = TRUE;
557 font_button->priv->show_size = TRUE;
558 font_button->priv->show_preview_entry = FALSE;
559 font_button->priv->font_dialog = NULL;
560 font_button->priv->font_family = NULL;
561 font_button->priv->font_face = NULL;
562 font_button->priv->font_size = -1;
563 font_button->priv->title = g_strdup (_("Pick a Font"));
565 font_button->priv->inside = gtk_font_button_create_inside (font_button);
566 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
568 gtk_font_button_take_font_desc (font_button, NULL);
572 gtk_font_button_finalize (GObject *object)
574 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
576 if (font_button->priv->font_dialog != NULL)
577 gtk_widget_destroy (font_button->priv->font_dialog);
578 font_button->priv->font_dialog = NULL;
580 g_free (font_button->priv->title);
581 font_button->priv->title = NULL;
583 clear_font_data (font_button);
584 clear_font_filter_data (font_button);
586 g_free (font_button->priv->preview_text);
587 font_button->priv->preview_text = NULL;
589 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
593 gtk_font_button_set_property (GObject *object,
598 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
602 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
603 gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
605 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
606 gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
609 gtk_font_button_set_title (font_button, g_value_get_string (value));
611 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
612 gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
614 case GTK_FONT_CHOOSER_PROP_FONT:
616 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
619 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
622 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
624 case PROP_SHOW_STYLE:
625 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
628 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
631 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
637 gtk_font_button_get_property (GObject *object,
642 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
646 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
647 g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
649 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
650 g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
653 g_value_set_string (value, gtk_font_button_get_title (font_button));
655 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
656 g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
658 case GTK_FONT_CHOOSER_PROP_FONT:
660 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
663 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
666 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
668 case PROP_SHOW_STYLE:
669 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
672 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
675 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
682 * gtk_font_button_new:
684 * Creates a new font picker widget.
686 * Returns: a new font picker widget.
691 gtk_font_button_new (void)
693 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
697 * gtk_font_button_new_with_font:
698 * @fontname: Name of font to display in font chooser dialog
700 * Creates a new font picker widget.
702 * Returns: a new font picker widget.
707 gtk_font_button_new_with_font (const gchar *fontname)
709 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
713 * gtk_font_button_set_title:
714 * @font_button: a #GtkFontButton
715 * @title: a string containing the font chooser dialog title
717 * Sets the title for the font chooser dialog.
722 gtk_font_button_set_title (GtkFontButton *font_button,
726 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
728 old_title = font_button->priv->title;
729 font_button->priv->title = g_strdup (title);
732 if (font_button->priv->font_dialog)
733 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
734 font_button->priv->title);
736 g_object_notify (G_OBJECT (font_button), "title");
740 * gtk_font_button_get_title:
741 * @font_button: a #GtkFontButton
743 * Retrieves the title of the font chooser dialog.
745 * Returns: an internal copy of the title string which must not be freed.
750 gtk_font_button_get_title (GtkFontButton *font_button)
752 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
754 return font_button->priv->title;
758 * gtk_font_button_get_use_font:
759 * @font_button: a #GtkFontButton
761 * Returns whether the selected font is used in the label.
763 * Returns: whether the selected font is used in the label.
768 gtk_font_button_get_use_font (GtkFontButton *font_button)
770 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
772 return font_button->priv->use_font;
776 * gtk_font_button_set_use_font:
777 * @font_button: a #GtkFontButton
778 * @use_font: If %TRUE, font name will be written using font chosen.
780 * If @use_font is %TRUE, the font name will be written using the selected font.
785 gtk_font_button_set_use_font (GtkFontButton *font_button,
788 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
790 use_font = (use_font != FALSE);
792 if (font_button->priv->use_font != use_font)
794 font_button->priv->use_font = use_font;
796 gtk_font_button_label_use_font (font_button);
798 g_object_notify (G_OBJECT (font_button), "use-font");
804 * gtk_font_button_get_use_size:
805 * @font_button: a #GtkFontButton
807 * Returns whether the selected size is used in the label.
809 * Returns: whether the selected size is used in the label.
814 gtk_font_button_get_use_size (GtkFontButton *font_button)
816 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
818 return font_button->priv->use_size;
822 * gtk_font_button_set_use_size:
823 * @font_button: a #GtkFontButton
824 * @use_size: If %TRUE, font name will be written using the selected size.
826 * If @use_size is %TRUE, the font name will be written using the selected size.
831 gtk_font_button_set_use_size (GtkFontButton *font_button,
834 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
836 use_size = (use_size != FALSE);
837 if (font_button->priv->use_size != use_size)
839 font_button->priv->use_size = use_size;
841 gtk_font_button_label_use_font (font_button);
843 g_object_notify (G_OBJECT (font_button), "use-size");
848 * gtk_font_button_get_show_style:
849 * @font_button: a #GtkFontButton
851 * Returns whether the name of the font style will be shown in the label.
853 * Return value: whether the font style will be shown in the label.
858 gtk_font_button_get_show_style (GtkFontButton *font_button)
860 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
862 return font_button->priv->show_style;
866 * gtk_font_button_set_show_style:
867 * @font_button: a #GtkFontButton
868 * @show_style: %TRUE if font style should be displayed in label.
870 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
875 gtk_font_button_set_show_style (GtkFontButton *font_button,
878 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
880 show_style = (show_style != FALSE);
881 if (font_button->priv->show_style != show_style)
883 font_button->priv->show_style = show_style;
885 gtk_font_button_update_font_info (font_button);
887 g_object_notify (G_OBJECT (font_button), "show-style");
893 * gtk_font_button_get_show_size:
894 * @font_button: a #GtkFontButton
896 * Returns whether the font size will be shown in the label.
898 * Return value: whether the font size will be shown in the label.
903 gtk_font_button_get_show_size (GtkFontButton *font_button)
905 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
907 return font_button->priv->show_size;
911 * gtk_font_button_set_show_size:
912 * @font_button: a #GtkFontButton
913 * @show_size: %TRUE if font size should be displayed in dialog.
915 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
920 gtk_font_button_set_show_size (GtkFontButton *font_button,
923 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
925 show_size = (show_size != FALSE);
927 if (font_button->priv->show_size != show_size)
929 font_button->priv->show_size = show_size;
931 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
932 font_button->priv->inside = gtk_font_button_create_inside (font_button);
933 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
935 gtk_font_button_update_font_info (font_button);
937 g_object_notify (G_OBJECT (font_button), "show-size");
943 * gtk_font_button_get_font_name:
944 * @font_button: a #GtkFontButton
946 * Retrieves the name of the currently selected font. This name includes
947 * style and size information as well. If you want to render something
948 * with the font, use this string with pango_font_description_from_string() .
949 * If you're interested in peeking certain values (family name,
950 * style, size, weight) just query these properties from the
951 * #PangoFontDescription object.
953 * Returns: an internal copy of the font name which must not be freed.
958 gtk_font_button_get_font_name (GtkFontButton *font_button)
960 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
962 return font_button->priv->fontname;
966 * gtk_font_button_set_font_name:
967 * @font_button: a #GtkFontButton
968 * @fontname: Name of font to display in font chooser dialog
970 * Sets or updates the currently-displayed font in font picker dialog.
977 gtk_font_button_set_font_name (GtkFontButton *font_button,
978 const gchar *fontname)
980 PangoFontDescription *font_desc;
982 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
983 g_return_val_if_fail (fontname != NULL, FALSE);
985 font_desc = pango_font_description_from_string (fontname);
986 gtk_font_button_take_font_desc (font_button, font_desc);
992 gtk_font_button_clicked (GtkButton *button)
994 GtkFontChooser *font_dialog;
995 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
996 GtkFontButtonPrivate *priv = font_button->priv;
998 if (!font_button->priv->font_dialog)
1002 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
1004 priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
1005 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1007 gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
1009 if (priv->preview_text)
1011 gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
1012 g_free (priv->preview_text);
1013 priv->preview_text = NULL;
1016 if (priv->font_filter)
1018 gtk_font_chooser_set_filter_func (font_dialog,
1020 priv->font_filter_data,
1021 priv->font_filter_data_destroy);
1022 priv->font_filter = NULL;
1023 priv->font_filter_data = NULL;
1024 priv->font_filter_data_destroy = NULL;
1027 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
1029 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
1030 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
1032 gtk_window_set_modal (GTK_WINDOW (font_dialog),
1033 gtk_window_get_modal (GTK_WINDOW (parent)));
1036 g_signal_connect (font_dialog, "notify",
1037 G_CALLBACK (gtk_font_button_font_chooser_notify), button);
1039 g_signal_connect (font_dialog, "response",
1040 G_CALLBACK (response_cb), font_button);
1042 g_signal_connect (font_dialog, "destroy",
1043 G_CALLBACK (dialog_destroy), font_button);
1046 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
1048 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1049 gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
1052 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
1057 response_cb (GtkDialog *dialog,
1061 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1062 GtkFontButtonPrivate *priv = font_button->priv;
1063 GtkFontChooser *font_chooser;
1066 gtk_widget_hide (font_button->priv->font_dialog);
1068 if (response_id != GTK_RESPONSE_OK)
1071 font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1072 object = G_OBJECT (font_chooser);
1074 g_object_freeze_notify (object);
1076 clear_font_data (font_button);
1078 priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
1079 if (priv->font_desc)
1080 priv->fontname = pango_font_description_to_string (priv->font_desc);
1081 priv->font_family = gtk_font_chooser_get_font_family (font_chooser);
1082 if (priv->font_family)
1083 g_object_ref (priv->font_family);
1084 priv->font_face = gtk_font_chooser_get_font_face (font_chooser);
1085 if (priv->font_face)
1086 g_object_ref (priv->font_face);
1087 priv->font_size = gtk_font_chooser_get_font_size (font_chooser);
1089 /* Set label font */
1090 gtk_font_button_update_font_info (font_button);
1092 g_object_notify (G_OBJECT (font_button), "font");
1093 g_object_notify (G_OBJECT (font_button), "font-desc");
1094 g_object_notify (G_OBJECT (font_button), "font-name");
1096 g_object_thaw_notify (object);
1098 /* Emit font_set signal */
1099 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
1103 dialog_destroy (GtkWidget *widget,
1106 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1108 /* Dialog will get destroyed so reference is not valid now */
1109 font_button->priv->font_dialog = NULL;
1113 gtk_font_button_create_inside (GtkFontButton *font_button)
1117 gtk_widget_push_composite_child ();
1119 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1121 font_button->priv->font_label = gtk_label_new (_("Font"));
1123 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1124 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1126 if (font_button->priv->show_size)
1128 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1129 font_button->priv->size_label = gtk_label_new ("14");
1130 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1133 gtk_widget_show_all (widget);
1135 gtk_widget_pop_composite_child ();
1141 gtk_font_button_label_use_font (GtkFontButton *font_button)
1143 PangoFontDescription *desc;
1145 if (font_button->priv->use_font)
1147 desc = pango_font_description_copy (font_button->priv->font_desc);
1149 if (!font_button->priv->use_size)
1150 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1155 gtk_widget_override_font (font_button->priv->font_label, desc);
1158 pango_font_description_free (desc);
1162 gtk_font_button_update_font_info (GtkFontButton *font_button)
1164 GtkFontButtonPrivate *priv = font_button->priv;
1165 gchar *family_style;
1167 g_assert (priv->font_desc != NULL);
1169 if (priv->show_style)
1171 PangoFontDescription *desc = pango_font_description_copy_static (priv->font_desc);
1173 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1174 family_style = pango_font_description_to_string (desc);
1175 pango_font_description_free (desc);
1178 family_style = g_strdup (pango_font_description_get_family (priv->font_desc));
1180 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1181 g_free (family_style);
1183 if (font_button->priv->show_size)
1185 /* mirror Pango, which doesn't translate this either */
1186 gchar *size = g_strdup_printf ("%g%s",
1187 pango_font_description_get_size (priv->font_desc) / (double)PANGO_SCALE,
1188 pango_font_description_get_size_is_absolute (priv->font_desc) ? "px" : "");
1190 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1195 gtk_font_button_label_use_font (font_button);