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 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_filter_data (GtkFontButton *font_button)
135 GtkFontButtonPrivate *priv = font_button->priv;
137 if (priv->font_filter_data_destroy)
138 priv->font_filter_data_destroy (priv->font_filter_data);
139 priv->font_filter = NULL;
140 priv->font_filter_data = NULL;
141 priv->font_filter_data_destroy = NULL;
145 gtk_font_button_get_preview_text (GtkFontButton *font_button)
147 GtkFontButtonPrivate *priv = font_button->priv;
149 if (priv->font_dialog)
150 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
152 return g_strdup (priv->preview_text);
156 gtk_font_button_set_preview_text (GtkFontButton *font_button,
157 const gchar *preview_text)
159 GtkFontButtonPrivate *priv = font_button->priv;
161 if (priv->font_dialog)
163 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
168 g_free (priv->preview_text);
169 priv->preview_text = g_strdup (preview_text);
174 gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
176 GtkFontButtonPrivate *priv = font_button->priv;
178 if (priv->font_dialog)
179 return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
181 return priv->show_preview_entry;
185 gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
188 GtkFontButtonPrivate *priv = font_button->priv;
190 if (priv->font_dialog)
191 return gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
193 priv->show_preview_entry = show != FALSE;
196 static PangoFontFamily *
197 gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
199 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
200 GtkFontButtonPrivate *priv = font_button->priv;
202 return priv->font_family;
205 static PangoFontFace *
206 gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
208 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
209 GtkFontButtonPrivate *priv = font_button->priv;
211 return priv->font_face;
215 gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
217 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
218 GtkFontButtonPrivate *priv = font_button->priv;
220 return priv->font_size;
224 gtk_font_button_font_chooser_set_filter_func (GtkFontChooser *chooser,
225 GtkFontFilterFunc filter_func,
226 gpointer filter_data,
227 GDestroyNotify data_destroy)
229 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
230 GtkFontButtonPrivate *priv = font_button->priv;
232 if (priv->font_dialog)
234 gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
241 clear_font_filter_data (font_button);
242 priv->font_filter = filter_func;
243 priv->font_filter_data = filter_data;
244 priv->font_filter_data_destroy = data_destroy;
248 gtk_font_button_font_chooser_notify (GObject *object,
252 /* We do not forward the notification of the "font" property to the dialog! */
253 if (pspec->name == I_("preview-text") ||
254 pspec->name == I_("show-preview-entry"))
255 g_object_notify_by_pspec (user_data, pspec);
259 gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
261 iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
262 iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
263 iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
264 iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
267 G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
268 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
269 gtk_font_button_font_chooser_iface_init))
272 gtk_font_button_class_init (GtkFontButtonClass *klass)
274 GObjectClass *gobject_class;
275 GtkButtonClass *button_class;
277 gobject_class = (GObjectClass *) klass;
278 button_class = (GtkButtonClass *) klass;
280 gobject_class->finalize = gtk_font_button_finalize;
281 gobject_class->set_property = gtk_font_button_set_property;
282 gobject_class->get_property = gtk_font_button_get_property;
284 button_class->clicked = gtk_font_button_clicked;
286 klass->font_set = NULL;
288 _gtk_font_chooser_install_properties (gobject_class);
291 * GtkFontButton:title:
293 * The title of the font chooser dialog.
297 g_object_class_install_property (gobject_class,
299 g_param_spec_string ("title",
301 P_("The title of the font chooser dialog"),
303 (GTK_PARAM_READABLE |
304 GTK_PARAM_WRITABLE)));
307 * GtkFontButton:font-name:
309 * The name of the currently selected font.
313 g_object_class_install_property (gobject_class,
315 g_param_spec_string ("font-name",
317 P_("The name of the selected font"),
319 (GTK_PARAM_READABLE |
320 GTK_PARAM_WRITABLE)));
323 * GtkFontButton:use-font:
325 * If this property is set to %TRUE, the label will be drawn
326 * in the selected font.
330 g_object_class_install_property (gobject_class,
332 g_param_spec_boolean ("use-font",
333 P_("Use font in label"),
334 P_("Whether the label is drawn in the selected font"),
336 GTK_PARAM_READWRITE));
339 * GtkFontButton:use-size:
341 * If this property is set to %TRUE, the label will be drawn
342 * with the selected font size.
346 g_object_class_install_property (gobject_class,
348 g_param_spec_boolean ("use-size",
349 P_("Use size in label"),
350 P_("Whether the label is drawn with the selected font size"),
352 GTK_PARAM_READWRITE));
355 * GtkFontButton:show-style:
357 * If this property is set to %TRUE, the name of the selected font style
358 * will be shown in the label. For a more WYSIWYG way to show the selected
359 * style, see the ::use-font property.
363 g_object_class_install_property (gobject_class,
365 g_param_spec_boolean ("show-style",
367 P_("Whether the selected font style is shown in the label"),
369 GTK_PARAM_READWRITE));
371 * GtkFontButton:show-size:
373 * If this property is set to %TRUE, the selected font size will be shown
374 * in the label. For a more WYSIWYG way to show the selected size, see the
375 * ::use-size property.
379 g_object_class_install_property (gobject_class,
381 g_param_spec_boolean ("show-size",
383 P_("Whether selected font size is shown in the label"),
385 GTK_PARAM_READWRITE));
388 * GtkFontButton::font-set:
389 * @widget: the object which received the signal.
391 * The ::font-set signal is emitted when the user selects a font.
392 * When handling this signal, use gtk_font_button_get_font_name()
393 * to find out which font was just selected.
395 * Note that this signal is only emitted when the <emphasis>user</emphasis>
396 * changes the font. If you need to react to programmatic font changes
397 * as well, use the notify::font-name signal.
401 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
402 G_TYPE_FROM_CLASS (gobject_class),
404 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
406 g_cclosure_marshal_VOID__VOID,
409 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
413 gtk_font_button_init (GtkFontButton *font_button)
415 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
416 GTK_TYPE_FONT_BUTTON,
417 GtkFontButtonPrivate);
419 /* Initialize fields */
420 font_button->priv->fontname = g_strdup (_("Sans 12"));
421 font_button->priv->use_font = FALSE;
422 font_button->priv->use_size = FALSE;
423 font_button->priv->show_style = TRUE;
424 font_button->priv->show_size = TRUE;
425 font_button->priv->show_preview_entry = FALSE;
426 font_button->priv->font_dialog = NULL;
427 font_button->priv->font_family = NULL;
428 font_button->priv->font_face = NULL;
429 font_button->priv->font_size = -1;
430 font_button->priv->title = g_strdup (_("Pick a Font"));
432 font_button->priv->inside = gtk_font_button_create_inside (font_button);
433 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
435 gtk_font_button_update_font_info (font_button);
440 gtk_font_button_finalize (GObject *object)
442 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
444 if (font_button->priv->font_dialog != NULL)
445 gtk_widget_destroy (font_button->priv->font_dialog);
446 font_button->priv->font_dialog = NULL;
448 g_free (font_button->priv->fontname);
449 font_button->priv->fontname = NULL;
451 g_free (font_button->priv->title);
452 font_button->priv->title = NULL;
454 clear_font_filter_data (font_button);
456 g_free (font_button->priv->preview_text);
457 font_button->priv->preview_text = NULL;
459 if (font_button->priv->font_family)
460 g_object_unref (font_button->priv->font_family);
461 font_button->priv->font_family = NULL;
463 if (font_button->priv->font_face)
464 g_object_unref (font_button->priv->font_face);
465 font_button->priv->font_face = NULL;
467 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
471 gtk_font_button_set_property (GObject *object,
476 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
480 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
481 gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
483 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
484 gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
487 gtk_font_button_set_title (font_button, g_value_get_string (value));
489 case GTK_FONT_CHOOSER_PROP_FONT:
491 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
494 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
497 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
499 case PROP_SHOW_STYLE:
500 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
503 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
506 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
512 gtk_font_button_get_property (GObject *object,
517 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
521 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
522 g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
524 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
525 g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
528 g_value_set_string (value, gtk_font_button_get_title (font_button));
530 case GTK_FONT_CHOOSER_PROP_FONT:
532 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
535 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
538 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
540 case PROP_SHOW_STYLE:
541 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
544 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
547 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
554 * gtk_font_button_new:
556 * Creates a new font picker widget.
558 * Returns: a new font picker widget.
563 gtk_font_button_new (void)
565 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
569 * gtk_font_button_new_with_font:
570 * @fontname: Name of font to display in font chooser dialog
572 * Creates a new font picker widget.
574 * Returns: a new font picker widget.
579 gtk_font_button_new_with_font (const gchar *fontname)
581 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
585 * gtk_font_button_set_title:
586 * @font_button: a #GtkFontButton
587 * @title: a string containing the font chooser dialog title
589 * Sets the title for the font chooser dialog.
594 gtk_font_button_set_title (GtkFontButton *font_button,
598 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
600 old_title = font_button->priv->title;
601 font_button->priv->title = g_strdup (title);
604 if (font_button->priv->font_dialog)
605 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
606 font_button->priv->title);
608 g_object_notify (G_OBJECT (font_button), "title");
612 * gtk_font_button_get_title:
613 * @font_button: a #GtkFontButton
615 * Retrieves the title of the font chooser dialog.
617 * Returns: an internal copy of the title string which must not be freed.
622 gtk_font_button_get_title (GtkFontButton *font_button)
624 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
626 return font_button->priv->title;
630 * gtk_font_button_get_use_font:
631 * @font_button: a #GtkFontButton
633 * Returns whether the selected font is used in the label.
635 * Returns: whether the selected font is used in the label.
640 gtk_font_button_get_use_font (GtkFontButton *font_button)
642 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
644 return font_button->priv->use_font;
648 * gtk_font_button_set_use_font:
649 * @font_button: a #GtkFontButton
650 * @use_font: If %TRUE, font name will be written using font chosen.
652 * If @use_font is %TRUE, the font name will be written using the selected font.
657 gtk_font_button_set_use_font (GtkFontButton *font_button,
660 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
662 use_font = (use_font != FALSE);
664 if (font_button->priv->use_font != use_font)
666 font_button->priv->use_font = use_font;
669 gtk_font_button_label_use_font (font_button);
671 gtk_widget_set_style (font_button->priv->font_label, NULL);
673 g_object_notify (G_OBJECT (font_button), "use-font");
679 * gtk_font_button_get_use_size:
680 * @font_button: a #GtkFontButton
682 * Returns whether the selected size is used in the label.
684 * Returns: whether the selected size is used in the label.
689 gtk_font_button_get_use_size (GtkFontButton *font_button)
691 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
693 return font_button->priv->use_size;
697 * gtk_font_button_set_use_size:
698 * @font_button: a #GtkFontButton
699 * @use_size: If %TRUE, font name will be written using the selected size.
701 * If @use_size is %TRUE, the font name will be written using the selected size.
706 gtk_font_button_set_use_size (GtkFontButton *font_button,
709 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
711 use_size = (use_size != FALSE);
712 if (font_button->priv->use_size != use_size)
714 font_button->priv->use_size = use_size;
716 if (font_button->priv->use_font)
717 gtk_font_button_label_use_font (font_button);
719 g_object_notify (G_OBJECT (font_button), "use-size");
724 * gtk_font_button_get_show_style:
725 * @font_button: a #GtkFontButton
727 * Returns whether the name of the font style will be shown in the label.
729 * Return value: whether the font style will be shown in the label.
734 gtk_font_button_get_show_style (GtkFontButton *font_button)
736 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
738 return font_button->priv->show_style;
742 * gtk_font_button_set_show_style:
743 * @font_button: a #GtkFontButton
744 * @show_style: %TRUE if font style should be displayed in label.
746 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
751 gtk_font_button_set_show_style (GtkFontButton *font_button,
754 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
756 show_style = (show_style != FALSE);
757 if (font_button->priv->show_style != show_style)
759 font_button->priv->show_style = show_style;
761 gtk_font_button_update_font_info (font_button);
763 g_object_notify (G_OBJECT (font_button), "show-style");
769 * gtk_font_button_get_show_size:
770 * @font_button: a #GtkFontButton
772 * Returns whether the font size will be shown in the label.
774 * Return value: whether the font size will be shown in the label.
779 gtk_font_button_get_show_size (GtkFontButton *font_button)
781 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
783 return font_button->priv->show_size;
787 * gtk_font_button_set_show_size:
788 * @font_button: a #GtkFontButton
789 * @show_size: %TRUE if font size should be displayed in dialog.
791 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
796 gtk_font_button_set_show_size (GtkFontButton *font_button,
799 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
801 show_size = (show_size != FALSE);
803 if (font_button->priv->show_size != show_size)
805 font_button->priv->show_size = show_size;
807 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
808 font_button->priv->inside = gtk_font_button_create_inside (font_button);
809 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
811 gtk_font_button_update_font_info (font_button);
813 g_object_notify (G_OBJECT (font_button), "show-size");
819 * gtk_font_button_get_font_name:
820 * @font_button: a #GtkFontButton
822 * Retrieves the name of the currently selected font. This name includes
823 * style and size information as well. If you want to render something
824 * with the font, use this string with pango_font_description_from_string() .
825 * If you're interested in peeking certain values (family name,
826 * style, size, weight) just query these properties from the
827 * #PangoFontDescription object.
829 * Returns: an internal copy of the font name which must not be freed.
834 gtk_font_button_get_font_name (GtkFontButton *font_button)
836 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
838 return font_button->priv->fontname;
842 * gtk_font_button_set_font_name:
843 * @font_button: a #GtkFontButton
844 * @fontname: Name of font to display in font chooser dialog
846 * Sets or updates the currently-displayed font in font picker dialog.
848 * Returns: Return value of gtk_font_chooser_dialog_set_font_name() if the
849 * font chooser dialog exists, otherwise %FALSE.
854 gtk_font_button_set_font_name (GtkFontButton *font_button,
855 const gchar *fontname)
859 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
860 g_return_val_if_fail (fontname != NULL, FALSE);
862 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
864 old_fontname = font_button->priv->fontname;
865 font_button->priv->fontname = g_strdup (fontname);
866 g_free (old_fontname);
869 gtk_font_button_update_font_info (font_button);
871 if (font_button->priv->font_dialog)
872 gtk_font_chooser_set_font (GTK_FONT_CHOOSER (font_button->priv->font_dialog),
873 font_button->priv->fontname);
875 g_object_notify (G_OBJECT (font_button), "font");
876 g_object_notify (G_OBJECT (font_button), "font-name");
882 gtk_font_button_clicked (GtkButton *button)
884 GtkFontChooser *font_dialog;
885 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
886 GtkFontButtonPrivate *priv = font_button->priv;
888 if (!font_button->priv->font_dialog)
892 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
894 priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
895 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
897 gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
899 if (priv->preview_text)
901 gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
902 g_free (priv->preview_text);
903 priv->preview_text = NULL;
906 if (priv->font_filter)
908 gtk_font_chooser_set_filter_func (font_dialog,
910 priv->font_filter_data,
911 priv->font_filter_data_destroy);
912 priv->font_filter = NULL;
913 priv->font_filter_data = NULL;
914 priv->font_filter_data_destroy = NULL;
917 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
919 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
920 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
922 gtk_window_set_modal (GTK_WINDOW (font_dialog),
923 gtk_window_get_modal (GTK_WINDOW (parent)));
926 g_signal_connect (font_dialog, "notify",
927 G_CALLBACK (gtk_font_button_font_chooser_notify), button);
929 g_signal_connect (font_dialog, "response",
930 G_CALLBACK (response_cb), font_button);
932 g_signal_connect (font_dialog, "destroy",
933 G_CALLBACK (dialog_destroy), font_button);
936 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
938 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
939 gtk_font_chooser_set_font (font_dialog, font_button->priv->fontname);
942 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
947 response_cb (GtkDialog *dialog,
951 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
952 GtkFontButtonPrivate *priv = font_button->priv;
953 GtkFontChooser *font_chooser;
955 gtk_widget_hide (font_button->priv->font_dialog);
957 if (response_id != GTK_RESPONSE_OK)
960 font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
962 g_free (priv->fontname);
963 priv->fontname = gtk_font_chooser_get_font (font_chooser);
965 if (priv->font_family)
966 g_object_unref (priv->font_family);
967 priv->font_family = gtk_font_chooser_get_family (font_chooser);
968 if (priv->font_family)
969 g_object_ref (priv->font_family);
972 g_object_unref (priv->font_face);
973 priv->font_face = gtk_font_chooser_get_face (font_chooser);
975 g_object_ref (priv->font_face);
977 priv->font_size = gtk_font_chooser_get_size (font_chooser);
980 gtk_font_button_update_font_info (font_button);
982 g_object_notify (G_OBJECT (font_button), "font");
983 g_object_notify (G_OBJECT (font_button), "font-name");
985 /* Emit font_set signal */
986 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
990 dialog_destroy (GtkWidget *widget,
993 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
995 /* Dialog will get destroyed so reference is not valid now */
996 font_button->priv->font_dialog = NULL;
1000 gtk_font_button_create_inside (GtkFontButton *font_button)
1004 gtk_widget_push_composite_child ();
1006 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1008 font_button->priv->font_label = gtk_label_new (_("Font"));
1010 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1011 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1013 if (font_button->priv->show_size)
1015 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1016 font_button->priv->size_label = gtk_label_new ("14");
1017 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1020 gtk_widget_show_all (widget);
1022 gtk_widget_pop_composite_child ();
1028 gtk_font_button_label_use_font (GtkFontButton *font_button)
1030 PangoFontDescription *desc;
1032 if (!font_button->priv->use_font)
1035 desc = pango_font_description_from_string (font_button->priv->fontname);
1037 if (!font_button->priv->use_size)
1038 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1040 gtk_widget_modify_font (font_button->priv->font_label, desc);
1042 pango_font_description_free (desc);
1046 font_description_style_equal (const PangoFontDescription *a,
1047 const PangoFontDescription *b)
1049 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
1050 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
1051 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
1052 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
1056 gtk_font_button_update_font_info (GtkFontButton *font_button)
1058 PangoFontDescription *desc;
1059 const gchar *family;
1061 gchar *family_style;
1063 desc = pango_font_description_from_string (font_button->priv->fontname);
1064 family = pango_font_description_get_family (desc);
1067 /* This gives the wrong names, e.g. Italic when the font chooser
1068 * dialog displayed Oblique.
1070 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
1071 style = pango_font_description_to_string (desc);
1072 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
1076 if (font_button->priv->show_style && family)
1078 PangoFontFamily **families;
1079 PangoFontFace **faces;
1080 gint n_families, n_faces, i;
1084 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
1085 &families, &n_families);
1088 for (i = 0; i < n_families; i++)
1090 const gchar *name = pango_font_family_get_name (families[i]);
1092 if (!g_ascii_strcasecmp (name, family))
1094 pango_font_family_list_faces (families[i], &faces, &n_faces);
1100 for (i = 0; i < n_faces; i++)
1102 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
1104 if (font_description_style_equal (tmp_desc, desc))
1106 style = g_strdup (pango_font_face_get_face_name (faces[i]));
1107 pango_font_description_free (tmp_desc);
1111 pango_font_description_free (tmp_desc);
1116 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
1117 family_style = g_strdup (family);
1119 family_style = g_strdup_printf ("%s %s", family, style);
1121 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1124 g_free (family_style);
1126 if (font_button->priv->show_size)
1128 gchar *size = g_strdup_printf ("%g",
1129 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
1131 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1136 gtk_font_button_label_use_font (font_button);
1138 pango_font_description_free (desc);