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, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
23 * Modified by the GTK+ Team and others 2003. See the AUTHORS
24 * file for a list of people on the GTK+ Team. See the ChangeLog
25 * files for a list of changes. These files are distributed with
26 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkfontbutton.h"
36 #include "gtkfontchooser.h"
37 #include "gtkfontchooserdialog.h"
39 #include "gtkmarshalers.h"
40 #include "gtkseparator.h"
41 #include "gtkprivate.h"
46 #include "gtkfontchooserutils.h"
50 * SECTION:gtkfontbutton
51 * @Short_description: A button to launch a font chooser dialog
52 * @Title: GtkFontButton
53 * @See_also: #GtkFontChooserDialog, #GtkColorButton.
55 * The #GtkFontButton is a button which displays the currently selected
56 * font an allows to open a font chooser dialog to change the font.
57 * It is suitable widget for selecting a font in a preference dialog.
61 struct _GtkFontButtonPrivate
71 guint show_preview_entry : 1;
73 GtkWidget *font_dialog;
75 GtkWidget *font_label;
76 GtkWidget *size_label;
78 PangoFontDescription *font_desc;
79 PangoFontFamily *font_family;
80 PangoFontFace *font_face;
83 GtkFontFilterFunc font_filter;
84 gpointer font_filter_data;
85 GDestroyNotify font_filter_data_destroy;
107 static void gtk_font_button_finalize (GObject *object);
108 static void gtk_font_button_get_property (GObject *object,
112 static void gtk_font_button_set_property (GObject *object,
117 static void gtk_font_button_clicked (GtkButton *button);
119 /* Dialog response functions */
120 static void response_cb (GtkDialog *dialog,
123 static void dialog_destroy (GtkWidget *widget,
126 /* Auxiliary functions */
127 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
128 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
129 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
131 static guint font_button_signals[LAST_SIGNAL] = { 0 };
134 clear_font_data (GtkFontButton *font_button)
136 GtkFontButtonPrivate *priv = font_button->priv;
138 if (priv->font_family)
139 g_object_unref (priv->font_family);
140 priv->font_family = NULL;
143 g_object_unref (priv->font_face);
144 priv->font_face = NULL;
147 pango_font_description_free (priv->font_desc);
148 priv->font_desc = NULL;
150 g_free (priv->fontname);
151 priv->fontname = NULL;
155 clear_font_filter_data (GtkFontButton *font_button)
157 GtkFontButtonPrivate *priv = font_button->priv;
159 if (priv->font_filter_data_destroy)
160 priv->font_filter_data_destroy (priv->font_filter_data);
161 priv->font_filter = NULL;
162 priv->font_filter_data = NULL;
163 priv->font_filter_data_destroy = NULL;
167 font_description_style_equal (const PangoFontDescription *a,
168 const PangoFontDescription *b)
170 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
171 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
172 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
173 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
177 gtk_font_button_update_font_data (GtkFontButton *font_button)
179 GtkFontButtonPrivate *priv = font_button->priv;
180 PangoFontFamily **families;
181 PangoFontFace **faces;
182 gint n_families, n_faces, i;
185 g_assert (priv->font_desc != NULL);
187 priv->fontname = pango_font_description_to_string (priv->font_desc);
189 family = pango_font_description_get_family (priv->font_desc);
195 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
196 &families, &n_families);
199 for (i = 0; i < n_families; i++)
201 const gchar *name = pango_font_family_get_name (families[i]);
203 if (!g_ascii_strcasecmp (name, family))
205 priv->font_family = g_object_ref (families[i]);
207 pango_font_family_list_faces (families[i], &faces, &n_faces);
213 for (i = 0; i < n_faces; i++)
215 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
217 if (font_description_style_equal (tmp_desc, priv->font_desc))
219 priv->font_face = g_object_ref (faces[i]);
221 pango_font_description_free (tmp_desc);
225 pango_font_description_free (tmp_desc);
232 gtk_font_button_get_preview_text (GtkFontButton *font_button)
234 GtkFontButtonPrivate *priv = font_button->priv;
236 if (priv->font_dialog)
237 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
239 return g_strdup (priv->preview_text);
243 gtk_font_button_set_preview_text (GtkFontButton *font_button,
244 const gchar *preview_text)
246 GtkFontButtonPrivate *priv = font_button->priv;
248 if (priv->font_dialog)
250 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
255 g_free (priv->preview_text);
256 priv->preview_text = g_strdup (preview_text);
261 gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
263 GtkFontButtonPrivate *priv = font_button->priv;
265 if (priv->font_dialog)
266 return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
268 return priv->show_preview_entry;
272 gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
275 GtkFontButtonPrivate *priv = font_button->priv;
277 if (priv->font_dialog)
278 return gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
280 priv->show_preview_entry = show != FALSE;
283 static PangoFontFamily *
284 gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
286 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
287 GtkFontButtonPrivate *priv = font_button->priv;
289 return priv->font_family;
292 static PangoFontFace *
293 gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
295 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
296 GtkFontButtonPrivate *priv = font_button->priv;
298 return priv->font_face;
302 gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
304 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
305 GtkFontButtonPrivate *priv = font_button->priv;
307 return priv->font_size;
311 gtk_font_button_font_chooser_set_filter_func (GtkFontChooser *chooser,
312 GtkFontFilterFunc filter_func,
313 gpointer filter_data,
314 GDestroyNotify data_destroy)
316 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
317 GtkFontButtonPrivate *priv = font_button->priv;
319 if (priv->font_dialog)
321 gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
328 clear_font_filter_data (font_button);
329 priv->font_filter = filter_func;
330 priv->font_filter_data = filter_data;
331 priv->font_filter_data_destroy = data_destroy;
335 gtk_font_button_take_font_desc (GtkFontButton *font_button,
336 PangoFontDescription *font_desc)
338 GtkFontButtonPrivate *priv = font_button->priv;
339 GObject *object = G_OBJECT (font_button);
341 if (priv->font_desc && font_desc &&
342 pango_font_description_equal (priv->font_desc, font_desc))
344 pango_font_description_free (font_desc);
348 g_object_freeze_notify (object);
350 clear_font_data (font_button);
353 priv->font_desc = font_desc; /* adopted */
355 priv->font_desc = pango_font_description_from_string (_("Sans 12"));
357 if (pango_font_description_get_size_is_absolute (priv->font_desc))
358 priv->font_size = pango_font_description_get_size (priv->font_desc);
360 priv->font_size = pango_font_description_get_size (priv->font_desc) / PANGO_SCALE;
362 gtk_font_button_update_font_data (font_button);
363 gtk_font_button_update_font_info (font_button);
365 if (priv->font_dialog)
366 gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (priv->font_dialog),
369 g_object_notify (G_OBJECT (font_button), "font");
370 g_object_notify (G_OBJECT (font_button), "font-desc");
371 g_object_notify (G_OBJECT (font_button), "font-name");
373 g_object_thaw_notify (object);
376 static const PangoFontDescription *
377 gtk_font_button_get_font_desc (GtkFontButton *font_button)
379 return font_button->priv->font_desc;
383 gtk_font_button_font_chooser_notify (GObject *object,
387 /* We do not forward the notification of the "font" property to the dialog! */
388 if (pspec->name == I_("preview-text") ||
389 pspec->name == I_("show-preview-entry"))
390 g_object_notify_by_pspec (user_data, pspec);
394 gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
396 iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
397 iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
398 iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
399 iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
402 G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
403 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
404 gtk_font_button_font_chooser_iface_init))
407 gtk_font_button_class_init (GtkFontButtonClass *klass)
409 GObjectClass *gobject_class;
410 GtkButtonClass *button_class;
412 gobject_class = (GObjectClass *) klass;
413 button_class = (GtkButtonClass *) klass;
415 gobject_class->finalize = gtk_font_button_finalize;
416 gobject_class->set_property = gtk_font_button_set_property;
417 gobject_class->get_property = gtk_font_button_get_property;
419 button_class->clicked = gtk_font_button_clicked;
421 klass->font_set = NULL;
423 _gtk_font_chooser_install_properties (gobject_class);
426 * GtkFontButton:title:
428 * The title of the font chooser dialog.
432 g_object_class_install_property (gobject_class,
434 g_param_spec_string ("title",
436 P_("The title of the font chooser dialog"),
438 (GTK_PARAM_READABLE |
439 GTK_PARAM_WRITABLE)));
442 * GtkFontButton:font-name:
444 * The name of the currently selected font.
448 g_object_class_install_property (gobject_class,
450 g_param_spec_string ("font-name",
452 P_("The name of the selected font"),
454 (GTK_PARAM_READABLE |
455 GTK_PARAM_WRITABLE)));
458 * GtkFontButton:use-font:
460 * If this property is set to %TRUE, the label will be drawn
461 * in the selected font.
465 g_object_class_install_property (gobject_class,
467 g_param_spec_boolean ("use-font",
468 P_("Use font in label"),
469 P_("Whether the label is drawn in the selected font"),
471 GTK_PARAM_READWRITE));
474 * GtkFontButton:use-size:
476 * If this property is set to %TRUE, the label will be drawn
477 * with the selected font size.
481 g_object_class_install_property (gobject_class,
483 g_param_spec_boolean ("use-size",
484 P_("Use size in label"),
485 P_("Whether the label is drawn with the selected font size"),
487 GTK_PARAM_READWRITE));
490 * GtkFontButton:show-style:
492 * If this property is set to %TRUE, the name of the selected font style
493 * will be shown in the label. For a more WYSIWYG way to show the selected
494 * style, see the ::use-font property.
498 g_object_class_install_property (gobject_class,
500 g_param_spec_boolean ("show-style",
502 P_("Whether the selected font style is shown in the label"),
504 GTK_PARAM_READWRITE));
506 * GtkFontButton:show-size:
508 * If this property is set to %TRUE, the selected font size will be shown
509 * in the label. For a more WYSIWYG way to show the selected size, see the
510 * ::use-size property.
514 g_object_class_install_property (gobject_class,
516 g_param_spec_boolean ("show-size",
518 P_("Whether selected font size is shown in the label"),
520 GTK_PARAM_READWRITE));
523 * GtkFontButton::font-set:
524 * @widget: the object which received the signal.
526 * The ::font-set signal is emitted when the user selects a font.
527 * When handling this signal, use gtk_font_button_get_font_name()
528 * to find out which font was just selected.
530 * Note that this signal is only emitted when the <emphasis>user</emphasis>
531 * changes the font. If you need to react to programmatic font changes
532 * as well, use the notify::font-name signal.
536 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
537 G_TYPE_FROM_CLASS (gobject_class),
539 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
541 g_cclosure_marshal_VOID__VOID,
544 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
548 gtk_font_button_init (GtkFontButton *font_button)
550 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
551 GTK_TYPE_FONT_BUTTON,
552 GtkFontButtonPrivate);
554 /* Initialize fields */
555 font_button->priv->use_font = FALSE;
556 font_button->priv->use_size = FALSE;
557 font_button->priv->show_style = TRUE;
558 font_button->priv->show_size = TRUE;
559 font_button->priv->show_preview_entry = FALSE;
560 font_button->priv->font_dialog = NULL;
561 font_button->priv->font_family = NULL;
562 font_button->priv->font_face = NULL;
563 font_button->priv->font_size = -1;
564 font_button->priv->title = g_strdup (_("Pick a Font"));
566 font_button->priv->inside = gtk_font_button_create_inside (font_button);
567 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
569 gtk_font_button_take_font_desc (font_button, NULL);
573 gtk_font_button_finalize (GObject *object)
575 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
577 if (font_button->priv->font_dialog != NULL)
578 gtk_widget_destroy (font_button->priv->font_dialog);
579 font_button->priv->font_dialog = NULL;
581 g_free (font_button->priv->title);
582 font_button->priv->title = NULL;
584 clear_font_data (font_button);
585 clear_font_filter_data (font_button);
587 g_free (font_button->priv->preview_text);
588 font_button->priv->preview_text = NULL;
590 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
594 gtk_font_button_set_property (GObject *object,
599 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
603 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
604 gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
606 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
607 gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
610 gtk_font_button_set_title (font_button, g_value_get_string (value));
612 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
613 gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
615 case GTK_FONT_CHOOSER_PROP_FONT:
617 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
620 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
623 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
625 case PROP_SHOW_STYLE:
626 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
629 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
632 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
638 gtk_font_button_get_property (GObject *object,
643 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
647 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
648 g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
650 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
651 g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
654 g_value_set_string (value, gtk_font_button_get_title (font_button));
656 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
657 g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
659 case GTK_FONT_CHOOSER_PROP_FONT:
661 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
664 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
667 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
669 case PROP_SHOW_STYLE:
670 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
673 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
676 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
683 * gtk_font_button_new:
685 * Creates a new font picker widget.
687 * Returns: a new font picker widget.
692 gtk_font_button_new (void)
694 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
698 * gtk_font_button_new_with_font:
699 * @fontname: Name of font to display in font chooser dialog
701 * Creates a new font picker widget.
703 * Returns: a new font picker widget.
708 gtk_font_button_new_with_font (const gchar *fontname)
710 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
714 * gtk_font_button_set_title:
715 * @font_button: a #GtkFontButton
716 * @title: a string containing the font chooser dialog title
718 * Sets the title for the font chooser dialog.
723 gtk_font_button_set_title (GtkFontButton *font_button,
727 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
729 old_title = font_button->priv->title;
730 font_button->priv->title = g_strdup (title);
733 if (font_button->priv->font_dialog)
734 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
735 font_button->priv->title);
737 g_object_notify (G_OBJECT (font_button), "title");
741 * gtk_font_button_get_title:
742 * @font_button: a #GtkFontButton
744 * Retrieves the title of the font chooser dialog.
746 * Returns: an internal copy of the title string which must not be freed.
751 gtk_font_button_get_title (GtkFontButton *font_button)
753 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
755 return font_button->priv->title;
759 * gtk_font_button_get_use_font:
760 * @font_button: a #GtkFontButton
762 * Returns whether the selected font is used in the label.
764 * Returns: whether the selected font is used in the label.
769 gtk_font_button_get_use_font (GtkFontButton *font_button)
771 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
773 return font_button->priv->use_font;
777 * gtk_font_button_set_use_font:
778 * @font_button: a #GtkFontButton
779 * @use_font: If %TRUE, font name will be written using font chosen.
781 * If @use_font is %TRUE, the font name will be written using the selected font.
786 gtk_font_button_set_use_font (GtkFontButton *font_button,
789 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
791 use_font = (use_font != FALSE);
793 if (font_button->priv->use_font != use_font)
795 font_button->priv->use_font = use_font;
798 gtk_font_button_label_use_font (font_button);
800 gtk_widget_set_style (font_button->priv->font_label, NULL);
802 g_object_notify (G_OBJECT (font_button), "use-font");
808 * gtk_font_button_get_use_size:
809 * @font_button: a #GtkFontButton
811 * Returns whether the selected size is used in the label.
813 * Returns: whether the selected size is used in the label.
818 gtk_font_button_get_use_size (GtkFontButton *font_button)
820 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
822 return font_button->priv->use_size;
826 * gtk_font_button_set_use_size:
827 * @font_button: a #GtkFontButton
828 * @use_size: If %TRUE, font name will be written using the selected size.
830 * If @use_size is %TRUE, the font name will be written using the selected size.
835 gtk_font_button_set_use_size (GtkFontButton *font_button,
838 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
840 use_size = (use_size != FALSE);
841 if (font_button->priv->use_size != use_size)
843 font_button->priv->use_size = use_size;
845 if (font_button->priv->use_font)
846 gtk_font_button_label_use_font (font_button);
848 g_object_notify (G_OBJECT (font_button), "use-size");
853 * gtk_font_button_get_show_style:
854 * @font_button: a #GtkFontButton
856 * Returns whether the name of the font style will be shown in the label.
858 * Return value: whether the font style will be shown in the label.
863 gtk_font_button_get_show_style (GtkFontButton *font_button)
865 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
867 return font_button->priv->show_style;
871 * gtk_font_button_set_show_style:
872 * @font_button: a #GtkFontButton
873 * @show_style: %TRUE if font style should be displayed in label.
875 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
880 gtk_font_button_set_show_style (GtkFontButton *font_button,
883 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
885 show_style = (show_style != FALSE);
886 if (font_button->priv->show_style != show_style)
888 font_button->priv->show_style = show_style;
890 gtk_font_button_update_font_info (font_button);
892 g_object_notify (G_OBJECT (font_button), "show-style");
898 * gtk_font_button_get_show_size:
899 * @font_button: a #GtkFontButton
901 * Returns whether the font size will be shown in the label.
903 * Return value: whether the font size will be shown in the label.
908 gtk_font_button_get_show_size (GtkFontButton *font_button)
910 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
912 return font_button->priv->show_size;
916 * gtk_font_button_set_show_size:
917 * @font_button: a #GtkFontButton
918 * @show_size: %TRUE if font size should be displayed in dialog.
920 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
925 gtk_font_button_set_show_size (GtkFontButton *font_button,
928 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
930 show_size = (show_size != FALSE);
932 if (font_button->priv->show_size != show_size)
934 font_button->priv->show_size = show_size;
936 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
937 font_button->priv->inside = gtk_font_button_create_inside (font_button);
938 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
940 gtk_font_button_update_font_info (font_button);
942 g_object_notify (G_OBJECT (font_button), "show-size");
948 * gtk_font_button_get_font_name:
949 * @font_button: a #GtkFontButton
951 * Retrieves the name of the currently selected font. This name includes
952 * style and size information as well. If you want to render something
953 * with the font, use this string with pango_font_description_from_string() .
954 * If you're interested in peeking certain values (family name,
955 * style, size, weight) just query these properties from the
956 * #PangoFontDescription object.
958 * Returns: an internal copy of the font name which must not be freed.
963 gtk_font_button_get_font_name (GtkFontButton *font_button)
965 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
967 return font_button->priv->fontname;
971 * gtk_font_button_set_font_name:
972 * @font_button: a #GtkFontButton
973 * @fontname: Name of font to display in font chooser dialog
975 * Sets or updates the currently-displayed font in font picker dialog.
982 gtk_font_button_set_font_name (GtkFontButton *font_button,
983 const gchar *fontname)
985 PangoFontDescription *font_desc;
987 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
988 g_return_val_if_fail (fontname != NULL, FALSE);
990 font_desc = pango_font_description_from_string (fontname);
991 gtk_font_button_take_font_desc (font_button, font_desc);
997 gtk_font_button_clicked (GtkButton *button)
999 GtkFontChooser *font_dialog;
1000 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
1001 GtkFontButtonPrivate *priv = font_button->priv;
1003 if (!font_button->priv->font_dialog)
1007 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
1009 priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
1010 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1012 gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
1014 if (priv->preview_text)
1016 gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
1017 g_free (priv->preview_text);
1018 priv->preview_text = NULL;
1021 if (priv->font_filter)
1023 gtk_font_chooser_set_filter_func (font_dialog,
1025 priv->font_filter_data,
1026 priv->font_filter_data_destroy);
1027 priv->font_filter = NULL;
1028 priv->font_filter_data = NULL;
1029 priv->font_filter_data_destroy = NULL;
1032 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
1034 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
1035 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
1037 gtk_window_set_modal (GTK_WINDOW (font_dialog),
1038 gtk_window_get_modal (GTK_WINDOW (parent)));
1041 g_signal_connect (font_dialog, "notify",
1042 G_CALLBACK (gtk_font_button_font_chooser_notify), button);
1044 g_signal_connect (font_dialog, "response",
1045 G_CALLBACK (response_cb), font_button);
1047 g_signal_connect (font_dialog, "destroy",
1048 G_CALLBACK (dialog_destroy), font_button);
1051 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
1053 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
1054 gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
1057 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
1062 response_cb (GtkDialog *dialog,
1066 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1067 GtkFontButtonPrivate *priv = font_button->priv;
1068 GtkFontChooser *font_chooser;
1071 gtk_widget_hide (font_button->priv->font_dialog);
1073 if (response_id != GTK_RESPONSE_OK)
1076 font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1077 object = G_OBJECT (font_chooser);
1079 g_object_freeze_notify (object);
1081 clear_font_data (font_button);
1083 priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
1084 if (priv->font_desc)
1085 priv->fontname = pango_font_description_to_string (priv->font_desc);
1086 priv->font_family = gtk_font_chooser_get_font_family (font_chooser);
1087 if (priv->font_family)
1088 g_object_ref (priv->font_family);
1089 priv->font_face = gtk_font_chooser_get_font_face (font_chooser);
1090 if (priv->font_face)
1091 g_object_ref (priv->font_face);
1092 priv->font_size = gtk_font_chooser_get_font_size (font_chooser);
1094 /* Set label font */
1095 gtk_font_button_update_font_info (font_button);
1097 g_object_notify (G_OBJECT (font_button), "font");
1098 g_object_notify (G_OBJECT (font_button), "font-desc");
1099 g_object_notify (G_OBJECT (font_button), "font-name");
1101 g_object_thaw_notify (object);
1103 /* Emit font_set signal */
1104 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
1108 dialog_destroy (GtkWidget *widget,
1111 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1113 /* Dialog will get destroyed so reference is not valid now */
1114 font_button->priv->font_dialog = NULL;
1118 gtk_font_button_create_inside (GtkFontButton *font_button)
1122 gtk_widget_push_composite_child ();
1124 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1126 font_button->priv->font_label = gtk_label_new (_("Font"));
1128 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1129 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1131 if (font_button->priv->show_size)
1133 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1134 font_button->priv->size_label = gtk_label_new ("14");
1135 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1138 gtk_widget_show_all (widget);
1140 gtk_widget_pop_composite_child ();
1146 gtk_font_button_label_use_font (GtkFontButton *font_button)
1148 PangoFontDescription *desc;
1150 if (!font_button->priv->use_font)
1153 desc = pango_font_description_copy (font_button->priv->font_desc);
1155 if (!font_button->priv->use_size)
1156 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1158 gtk_widget_modify_font (font_button->priv->font_label, desc);
1160 pango_font_description_free (desc);
1164 gtk_font_button_update_font_info (GtkFontButton *font_button)
1166 GtkFontButtonPrivate *priv = font_button->priv;
1167 gchar *family_style;
1169 g_assert (priv->font_desc != NULL);
1171 if (priv->show_style)
1173 PangoFontDescription *desc = pango_font_description_copy_static (priv->font_desc);
1175 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1176 family_style = pango_font_description_to_string (desc);
1177 pango_font_description_free (desc);
1180 family_style = g_strdup (pango_font_description_get_family (priv->font_desc));
1182 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1183 g_free (family_style);
1185 if (font_button->priv->show_size)
1187 gchar *size = g_strdup_printf ("%g",
1188 pango_font_description_get_size (priv->font_desc) / (double)PANGO_SCALE);
1190 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1195 gtk_font_button_label_use_font (font_button);