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_filter_data (GtkFontButton *font_button)
136 GtkFontButtonPrivate *priv = font_button->priv;
138 if (priv->font_filter_data_destroy)
139 priv->font_filter_data_destroy (priv->font_filter_data);
140 priv->font_filter = NULL;
141 priv->font_filter_data = NULL;
142 priv->font_filter_data_destroy = NULL;
146 gtk_font_button_get_preview_text (GtkFontButton *font_button)
148 GtkFontButtonPrivate *priv = font_button->priv;
150 if (priv->font_dialog)
151 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->font_dialog));
153 return g_strdup (priv->preview_text);
157 gtk_font_button_set_preview_text (GtkFontButton *font_button,
158 const gchar *preview_text)
160 GtkFontButtonPrivate *priv = font_button->priv;
162 if (priv->font_dialog)
164 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->font_dialog),
169 g_free (priv->preview_text);
170 priv->preview_text = g_strdup (preview_text);
175 gtk_font_button_get_show_preview_entry (GtkFontButton *font_button)
177 GtkFontButtonPrivate *priv = font_button->priv;
179 if (priv->font_dialog)
180 return gtk_font_chooser_get_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog));
182 return priv->show_preview_entry;
186 gtk_font_button_set_show_preview_entry (GtkFontButton *font_button,
189 GtkFontButtonPrivate *priv = font_button->priv;
191 if (priv->font_dialog)
192 return gtk_font_chooser_set_show_preview_entry (GTK_FONT_CHOOSER (priv->font_dialog), show);
194 priv->show_preview_entry = show != FALSE;
197 static PangoFontFamily *
198 gtk_font_button_font_chooser_get_font_family (GtkFontChooser *chooser)
200 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
201 GtkFontButtonPrivate *priv = font_button->priv;
203 return priv->font_family;
206 static PangoFontFace *
207 gtk_font_button_font_chooser_get_font_face (GtkFontChooser *chooser)
209 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
210 GtkFontButtonPrivate *priv = font_button->priv;
212 return priv->font_face;
216 gtk_font_button_font_chooser_get_font_size (GtkFontChooser *chooser)
218 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
219 GtkFontButtonPrivate *priv = font_button->priv;
221 return priv->font_size;
225 gtk_font_button_font_chooser_set_filter_func (GtkFontChooser *chooser,
226 GtkFontFilterFunc filter_func,
227 gpointer filter_data,
228 GDestroyNotify data_destroy)
230 GtkFontButton *font_button = GTK_FONT_BUTTON (chooser);
231 GtkFontButtonPrivate *priv = font_button->priv;
233 if (priv->font_dialog)
235 gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (priv->font_dialog),
242 clear_font_filter_data (font_button);
243 priv->font_filter = filter_func;
244 priv->font_filter_data = filter_data;
245 priv->font_filter_data_destroy = data_destroy;
249 gtk_font_button_take_font_desc (GtkFontButton *font_button,
250 PangoFontDescription *font_desc)
252 GtkFontButtonPrivate *priv = font_button->priv;
253 GObject *object = G_OBJECT (font_button);
255 if (priv->font_desc && font_desc &&
256 pango_font_description_equal (priv->font_desc, font_desc))
258 pango_font_description_free (font_desc);
262 g_object_freeze_notify (object);
265 pango_font_description_free (priv->font_desc);
267 priv->font_desc = font_desc; /* adopted */
269 priv->font_desc = pango_font_description_from_string (_("Sans 12"));
271 g_free (priv->fontname);
272 priv->fontname = pango_font_description_to_string (priv->font_desc);
274 gtk_font_button_update_font_info (font_button);
276 if (priv->font_dialog)
277 gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (priv->font_dialog),
280 g_object_notify (G_OBJECT (font_button), "font");
281 g_object_notify (G_OBJECT (font_button), "font-desc");
282 g_object_notify (G_OBJECT (font_button), "font-name");
284 g_object_thaw_notify (object);
287 static const PangoFontDescription *
288 gtk_font_button_get_font_desc (GtkFontButton *font_button)
290 return font_button->priv->font_desc;
294 gtk_font_button_font_chooser_notify (GObject *object,
298 /* We do not forward the notification of the "font" property to the dialog! */
299 if (pspec->name == I_("preview-text") ||
300 pspec->name == I_("show-preview-entry"))
301 g_object_notify_by_pspec (user_data, pspec);
305 gtk_font_button_font_chooser_iface_init (GtkFontChooserIface *iface)
307 iface->get_font_family = gtk_font_button_font_chooser_get_font_family;
308 iface->get_font_face = gtk_font_button_font_chooser_get_font_face;
309 iface->get_font_size = gtk_font_button_font_chooser_get_font_size;
310 iface->set_filter_func = gtk_font_button_font_chooser_set_filter_func;
313 G_DEFINE_TYPE_WITH_CODE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON,
314 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
315 gtk_font_button_font_chooser_iface_init))
318 gtk_font_button_class_init (GtkFontButtonClass *klass)
320 GObjectClass *gobject_class;
321 GtkButtonClass *button_class;
323 gobject_class = (GObjectClass *) klass;
324 button_class = (GtkButtonClass *) klass;
326 gobject_class->finalize = gtk_font_button_finalize;
327 gobject_class->set_property = gtk_font_button_set_property;
328 gobject_class->get_property = gtk_font_button_get_property;
330 button_class->clicked = gtk_font_button_clicked;
332 klass->font_set = NULL;
334 _gtk_font_chooser_install_properties (gobject_class);
337 * GtkFontButton:title:
339 * The title of the font chooser dialog.
343 g_object_class_install_property (gobject_class,
345 g_param_spec_string ("title",
347 P_("The title of the font chooser dialog"),
349 (GTK_PARAM_READABLE |
350 GTK_PARAM_WRITABLE)));
353 * GtkFontButton:font-name:
355 * The name of the currently selected font.
359 g_object_class_install_property (gobject_class,
361 g_param_spec_string ("font-name",
363 P_("The name of the selected font"),
365 (GTK_PARAM_READABLE |
366 GTK_PARAM_WRITABLE)));
369 * GtkFontButton:use-font:
371 * If this property is set to %TRUE, the label will be drawn
372 * in the selected font.
376 g_object_class_install_property (gobject_class,
378 g_param_spec_boolean ("use-font",
379 P_("Use font in label"),
380 P_("Whether the label is drawn in the selected font"),
382 GTK_PARAM_READWRITE));
385 * GtkFontButton:use-size:
387 * If this property is set to %TRUE, the label will be drawn
388 * with the selected font size.
392 g_object_class_install_property (gobject_class,
394 g_param_spec_boolean ("use-size",
395 P_("Use size in label"),
396 P_("Whether the label is drawn with the selected font size"),
398 GTK_PARAM_READWRITE));
401 * GtkFontButton:show-style:
403 * If this property is set to %TRUE, the name of the selected font style
404 * will be shown in the label. For a more WYSIWYG way to show the selected
405 * style, see the ::use-font property.
409 g_object_class_install_property (gobject_class,
411 g_param_spec_boolean ("show-style",
413 P_("Whether the selected font style is shown in the label"),
415 GTK_PARAM_READWRITE));
417 * GtkFontButton:show-size:
419 * If this property is set to %TRUE, the selected font size will be shown
420 * in the label. For a more WYSIWYG way to show the selected size, see the
421 * ::use-size property.
425 g_object_class_install_property (gobject_class,
427 g_param_spec_boolean ("show-size",
429 P_("Whether selected font size is shown in the label"),
431 GTK_PARAM_READWRITE));
434 * GtkFontButton::font-set:
435 * @widget: the object which received the signal.
437 * The ::font-set signal is emitted when the user selects a font.
438 * When handling this signal, use gtk_font_button_get_font_name()
439 * to find out which font was just selected.
441 * Note that this signal is only emitted when the <emphasis>user</emphasis>
442 * changes the font. If you need to react to programmatic font changes
443 * as well, use the notify::font-name signal.
447 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
448 G_TYPE_FROM_CLASS (gobject_class),
450 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
452 g_cclosure_marshal_VOID__VOID,
455 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
459 gtk_font_button_init (GtkFontButton *font_button)
461 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
462 GTK_TYPE_FONT_BUTTON,
463 GtkFontButtonPrivate);
465 /* Initialize fields */
466 font_button->priv->use_font = FALSE;
467 font_button->priv->use_size = FALSE;
468 font_button->priv->show_style = TRUE;
469 font_button->priv->show_size = TRUE;
470 font_button->priv->show_preview_entry = FALSE;
471 font_button->priv->font_dialog = NULL;
472 font_button->priv->font_family = NULL;
473 font_button->priv->font_face = NULL;
474 font_button->priv->font_size = -1;
475 font_button->priv->title = g_strdup (_("Pick a Font"));
477 font_button->priv->inside = gtk_font_button_create_inside (font_button);
478 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
480 gtk_font_button_take_font_desc (font_button, NULL);
484 gtk_font_button_finalize (GObject *object)
486 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
488 if (font_button->priv->font_dialog != NULL)
489 gtk_widget_destroy (font_button->priv->font_dialog);
490 font_button->priv->font_dialog = NULL;
492 g_free (font_button->priv->fontname);
493 font_button->priv->fontname = NULL;
495 g_free (font_button->priv->title);
496 font_button->priv->title = NULL;
498 clear_font_filter_data (font_button);
500 g_free (font_button->priv->preview_text);
501 font_button->priv->preview_text = NULL;
503 if (font_button->priv->font_family)
504 g_object_unref (font_button->priv->font_family);
505 font_button->priv->font_family = NULL;
507 if (font_button->priv->font_face)
508 g_object_unref (font_button->priv->font_face);
509 font_button->priv->font_face = NULL;
511 if (font_button->priv->font_desc)
512 pango_font_description_free (font_button->priv->font_desc);
513 font_button->priv->font_desc = NULL;
515 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
519 gtk_font_button_set_property (GObject *object,
524 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
528 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
529 gtk_font_button_set_preview_text (font_button, g_value_get_string (value));
531 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
532 gtk_font_button_set_show_preview_entry (font_button, g_value_get_boolean (value));
535 gtk_font_button_set_title (font_button, g_value_get_string (value));
537 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
538 gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
540 case GTK_FONT_CHOOSER_PROP_FONT:
542 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
545 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
548 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
550 case PROP_SHOW_STYLE:
551 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
554 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
563 gtk_font_button_get_property (GObject *object,
568 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
572 case GTK_FONT_CHOOSER_PROP_PREVIEW_TEXT:
573 g_value_set_string (value, gtk_font_button_get_preview_text (font_button));
575 case GTK_FONT_CHOOSER_PROP_SHOW_PREVIEW_ENTRY:
576 g_value_set_boolean (value, gtk_font_button_get_show_preview_entry (font_button));
579 g_value_set_string (value, gtk_font_button_get_title (font_button));
581 case GTK_FONT_CHOOSER_PROP_FONT_DESC:
582 g_value_set_boxed (value, gtk_font_button_get_font_desc (font_button));
584 case GTK_FONT_CHOOSER_PROP_FONT:
586 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
589 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
592 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
594 case PROP_SHOW_STYLE:
595 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
598 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
608 * gtk_font_button_new:
610 * Creates a new font picker widget.
612 * Returns: a new font picker widget.
617 gtk_font_button_new (void)
619 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
623 * gtk_font_button_new_with_font:
624 * @fontname: Name of font to display in font chooser dialog
626 * Creates a new font picker widget.
628 * Returns: a new font picker widget.
633 gtk_font_button_new_with_font (const gchar *fontname)
635 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
639 * gtk_font_button_set_title:
640 * @font_button: a #GtkFontButton
641 * @title: a string containing the font chooser dialog title
643 * Sets the title for the font chooser dialog.
648 gtk_font_button_set_title (GtkFontButton *font_button,
652 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
654 old_title = font_button->priv->title;
655 font_button->priv->title = g_strdup (title);
658 if (font_button->priv->font_dialog)
659 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
660 font_button->priv->title);
662 g_object_notify (G_OBJECT (font_button), "title");
666 * gtk_font_button_get_title:
667 * @font_button: a #GtkFontButton
669 * Retrieves the title of the font chooser dialog.
671 * Returns: an internal copy of the title string which must not be freed.
676 gtk_font_button_get_title (GtkFontButton *font_button)
678 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
680 return font_button->priv->title;
684 * gtk_font_button_get_use_font:
685 * @font_button: a #GtkFontButton
687 * Returns whether the selected font is used in the label.
689 * Returns: whether the selected font is used in the label.
694 gtk_font_button_get_use_font (GtkFontButton *font_button)
696 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
698 return font_button->priv->use_font;
702 * gtk_font_button_set_use_font:
703 * @font_button: a #GtkFontButton
704 * @use_font: If %TRUE, font name will be written using font chosen.
706 * If @use_font is %TRUE, the font name will be written using the selected font.
711 gtk_font_button_set_use_font (GtkFontButton *font_button,
714 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
716 use_font = (use_font != FALSE);
718 if (font_button->priv->use_font != use_font)
720 font_button->priv->use_font = use_font;
723 gtk_font_button_label_use_font (font_button);
725 gtk_widget_set_style (font_button->priv->font_label, NULL);
727 g_object_notify (G_OBJECT (font_button), "use-font");
733 * gtk_font_button_get_use_size:
734 * @font_button: a #GtkFontButton
736 * Returns whether the selected size is used in the label.
738 * Returns: whether the selected size is used in the label.
743 gtk_font_button_get_use_size (GtkFontButton *font_button)
745 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
747 return font_button->priv->use_size;
751 * gtk_font_button_set_use_size:
752 * @font_button: a #GtkFontButton
753 * @use_size: If %TRUE, font name will be written using the selected size.
755 * If @use_size is %TRUE, the font name will be written using the selected size.
760 gtk_font_button_set_use_size (GtkFontButton *font_button,
763 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
765 use_size = (use_size != FALSE);
766 if (font_button->priv->use_size != use_size)
768 font_button->priv->use_size = use_size;
770 if (font_button->priv->use_font)
771 gtk_font_button_label_use_font (font_button);
773 g_object_notify (G_OBJECT (font_button), "use-size");
778 * gtk_font_button_get_show_style:
779 * @font_button: a #GtkFontButton
781 * Returns whether the name of the font style will be shown in the label.
783 * Return value: whether the font style will be shown in the label.
788 gtk_font_button_get_show_style (GtkFontButton *font_button)
790 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
792 return font_button->priv->show_style;
796 * gtk_font_button_set_show_style:
797 * @font_button: a #GtkFontButton
798 * @show_style: %TRUE if font style should be displayed in label.
800 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
805 gtk_font_button_set_show_style (GtkFontButton *font_button,
808 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
810 show_style = (show_style != FALSE);
811 if (font_button->priv->show_style != show_style)
813 font_button->priv->show_style = show_style;
815 gtk_font_button_update_font_info (font_button);
817 g_object_notify (G_OBJECT (font_button), "show-style");
823 * gtk_font_button_get_show_size:
824 * @font_button: a #GtkFontButton
826 * Returns whether the font size will be shown in the label.
828 * Return value: whether the font size will be shown in the label.
833 gtk_font_button_get_show_size (GtkFontButton *font_button)
835 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
837 return font_button->priv->show_size;
841 * gtk_font_button_set_show_size:
842 * @font_button: a #GtkFontButton
843 * @show_size: %TRUE if font size should be displayed in dialog.
845 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
850 gtk_font_button_set_show_size (GtkFontButton *font_button,
853 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
855 show_size = (show_size != FALSE);
857 if (font_button->priv->show_size != show_size)
859 font_button->priv->show_size = show_size;
861 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
862 font_button->priv->inside = gtk_font_button_create_inside (font_button);
863 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
865 gtk_font_button_update_font_info (font_button);
867 g_object_notify (G_OBJECT (font_button), "show-size");
873 * gtk_font_button_get_font_name:
874 * @font_button: a #GtkFontButton
876 * Retrieves the name of the currently selected font. This name includes
877 * style and size information as well. If you want to render something
878 * with the font, use this string with pango_font_description_from_string() .
879 * If you're interested in peeking certain values (family name,
880 * style, size, weight) just query these properties from the
881 * #PangoFontDescription object.
883 * Returns: an internal copy of the font name which must not be freed.
888 gtk_font_button_get_font_name (GtkFontButton *font_button)
890 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
892 return font_button->priv->fontname;
896 * gtk_font_button_set_font_name:
897 * @font_button: a #GtkFontButton
898 * @fontname: Name of font to display in font chooser dialog
900 * Sets or updates the currently-displayed font in font picker dialog.
907 gtk_font_button_set_font_name (GtkFontButton *font_button,
908 const gchar *fontname)
910 PangoFontDescription *font_desc;
912 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
913 g_return_val_if_fail (fontname != NULL, FALSE);
915 font_desc = pango_font_description_from_string (fontname);
916 gtk_font_button_take_font_desc (font_button, font_desc);
922 gtk_font_button_clicked (GtkButton *button)
924 GtkFontChooser *font_dialog;
925 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
926 GtkFontButtonPrivate *priv = font_button->priv;
928 if (!font_button->priv->font_dialog)
932 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
934 priv->font_dialog = gtk_font_chooser_dialog_new (priv->title, NULL);
935 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
937 gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
939 if (priv->preview_text)
941 gtk_font_chooser_set_preview_text (font_dialog, priv->preview_text);
942 g_free (priv->preview_text);
943 priv->preview_text = NULL;
946 if (priv->font_filter)
948 gtk_font_chooser_set_filter_func (font_dialog,
950 priv->font_filter_data,
951 priv->font_filter_data_destroy);
952 priv->font_filter = NULL;
953 priv->font_filter_data = NULL;
954 priv->font_filter_data_destroy = NULL;
957 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
959 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
960 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
962 gtk_window_set_modal (GTK_WINDOW (font_dialog),
963 gtk_window_get_modal (GTK_WINDOW (parent)));
966 g_signal_connect (font_dialog, "notify",
967 G_CALLBACK (gtk_font_button_font_chooser_notify), button);
969 g_signal_connect (font_dialog, "response",
970 G_CALLBACK (response_cb), font_button);
972 g_signal_connect (font_dialog, "destroy",
973 G_CALLBACK (dialog_destroy), font_button);
976 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
978 font_dialog = GTK_FONT_CHOOSER (font_button->priv->font_dialog);
979 gtk_font_chooser_set_font_desc (font_dialog, font_button->priv->font_desc);
982 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
987 response_cb (GtkDialog *dialog,
991 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
992 GtkFontButtonPrivate *priv = font_button->priv;
993 GtkFontChooser *font_chooser;
996 gtk_widget_hide (font_button->priv->font_dialog);
998 if (response_id != GTK_RESPONSE_OK)
1001 font_chooser = GTK_FONT_CHOOSER (priv->font_dialog);
1002 object = G_OBJECT (font_chooser);
1004 g_object_freeze_notify (object);
1006 if (priv->font_desc)
1007 pango_font_description_free (priv->font_desc);
1008 priv->font_desc = gtk_font_chooser_get_font_desc (font_chooser);
1010 g_free (priv->fontname);
1011 priv->fontname = pango_font_description_to_string (priv->font_desc);
1013 if (priv->font_family)
1014 g_object_unref (priv->font_family);
1015 priv->font_family = gtk_font_chooser_get_family (font_chooser);
1016 if (priv->font_family)
1017 g_object_ref (priv->font_family);
1019 if (priv->font_face)
1020 g_object_unref (priv->font_face);
1021 priv->font_face = gtk_font_chooser_get_face (font_chooser);
1022 if (priv->font_face)
1023 g_object_ref (priv->font_face);
1025 priv->font_size = gtk_font_chooser_get_size (font_chooser);
1027 /* Set label font */
1028 gtk_font_button_update_font_info (font_button);
1030 g_object_notify (G_OBJECT (font_button), "font");
1031 g_object_notify (G_OBJECT (font_button), "font-desc");
1032 g_object_notify (G_OBJECT (font_button), "font-name");
1034 g_object_thaw_notify (object);
1036 /* Emit font_set signal */
1037 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
1041 dialog_destroy (GtkWidget *widget,
1044 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
1046 /* Dialog will get destroyed so reference is not valid now */
1047 font_button->priv->font_dialog = NULL;
1051 gtk_font_button_create_inside (GtkFontButton *font_button)
1055 gtk_widget_push_composite_child ();
1057 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
1059 font_button->priv->font_label = gtk_label_new (_("Font"));
1061 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
1062 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
1064 if (font_button->priv->show_size)
1066 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
1067 font_button->priv->size_label = gtk_label_new ("14");
1068 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
1071 gtk_widget_show_all (widget);
1073 gtk_widget_pop_composite_child ();
1079 gtk_font_button_label_use_font (GtkFontButton *font_button)
1081 PangoFontDescription *desc;
1083 if (!font_button->priv->use_font)
1086 desc = pango_font_description_copy (font_button->priv->font_desc);
1088 if (!font_button->priv->use_size)
1089 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
1091 gtk_widget_modify_font (font_button->priv->font_label, desc);
1093 pango_font_description_free (desc);
1097 font_description_style_equal (const PangoFontDescription *a,
1098 const PangoFontDescription *b)
1100 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
1101 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
1102 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
1103 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
1107 gtk_font_button_update_font_info (GtkFontButton *font_button)
1109 const PangoFontDescription *desc;
1110 const gchar *family;
1112 gchar *family_style;
1114 desc = font_button->priv->font_desc;
1115 g_assert (desc != NULL);
1117 family = pango_font_description_get_family (desc);
1120 /* This gives the wrong names, e.g. Italic when the font chooser
1121 * dialog displayed Oblique.
1123 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
1124 style = pango_font_description_to_string (desc);
1125 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
1129 if (font_button->priv->show_style && family)
1131 PangoFontFamily **families;
1132 PangoFontFace **faces;
1133 gint n_families, n_faces, i;
1137 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
1138 &families, &n_families);
1141 for (i = 0; i < n_families; i++)
1143 const gchar *name = pango_font_family_get_name (families[i]);
1145 if (!g_ascii_strcasecmp (name, family))
1147 pango_font_family_list_faces (families[i], &faces, &n_faces);
1153 for (i = 0; i < n_faces; i++)
1155 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
1157 if (font_description_style_equal (tmp_desc, desc))
1159 style = g_strdup (pango_font_face_get_face_name (faces[i]));
1160 pango_font_description_free (tmp_desc);
1164 pango_font_description_free (tmp_desc);
1169 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
1170 family_style = g_strdup (family);
1172 family_style = g_strdup_printf ("%s %s", family, style);
1174 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
1177 g_free (family_style);
1179 if (font_button->priv->show_size)
1181 gchar *size = g_strdup_printf ("%g",
1182 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
1184 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
1189 gtk_font_button_label_use_font (font_button);