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 "gtkfontsel.h"
38 #include "gtkmarshalers.h"
39 #include "gtkseparator.h"
40 #include "gtkprivate.h"
48 * SECTION:gtkfontbutton
49 * @Short_description: A button to launch a font selection dialog
50 * @Title: GtkFontButton
51 * @See_also: #GtkFontSelectionDialog, #GtkColorButton.
53 * The #GtkFontButton is a button which displays the currently selected
54 * font an allows to open a font selection dialog to change the font.
55 * It is suitable widget for selecting a font in a preference dialog.
59 struct _GtkFontButtonPrivate
70 GtkWidget *font_dialog;
72 GtkWidget *font_label;
73 GtkWidget *size_label;
95 static void gtk_font_button_finalize (GObject *object);
96 static void gtk_font_button_get_property (GObject *object,
100 static void gtk_font_button_set_property (GObject *object,
105 static void gtk_font_button_clicked (GtkButton *button);
107 /* Dialog response functions */
108 static void dialog_ok_clicked (GtkWidget *widget,
110 static void dialog_cancel_clicked (GtkWidget *widget,
112 static void dialog_destroy (GtkWidget *widget,
115 /* Auxiliary functions */
116 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
117 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
118 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
120 static guint font_button_signals[LAST_SIGNAL] = { 0 };
122 G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
125 gtk_font_button_class_init (GtkFontButtonClass *klass)
127 GObjectClass *gobject_class;
128 GtkButtonClass *button_class;
130 gobject_class = (GObjectClass *) klass;
131 button_class = (GtkButtonClass *) klass;
133 gobject_class->finalize = gtk_font_button_finalize;
134 gobject_class->set_property = gtk_font_button_set_property;
135 gobject_class->get_property = gtk_font_button_get_property;
137 button_class->clicked = gtk_font_button_clicked;
139 klass->font_set = NULL;
142 * GtkFontButton:title:
144 * The title of the font selection dialog.
148 g_object_class_install_property (gobject_class,
150 g_param_spec_string ("title",
152 P_("The title of the font selection dialog"),
154 (GTK_PARAM_READABLE |
155 GTK_PARAM_WRITABLE)));
158 * GtkFontButton:font-name:
160 * The name of the currently selected font.
164 g_object_class_install_property (gobject_class,
166 g_param_spec_string ("font-name",
168 P_("The name of the selected font"),
170 (GTK_PARAM_READABLE |
171 GTK_PARAM_WRITABLE)));
174 * GtkFontButton:use-font:
176 * If this property is set to %TRUE, the label will be drawn
177 * in the selected font.
181 g_object_class_install_property (gobject_class,
183 g_param_spec_boolean ("use-font",
184 P_("Use font in label"),
185 P_("Whether the label is drawn in the selected font"),
187 GTK_PARAM_READWRITE));
190 * GtkFontButton:use-size:
192 * If this property is set to %TRUE, the label will be drawn
193 * with the selected font size.
197 g_object_class_install_property (gobject_class,
199 g_param_spec_boolean ("use-size",
200 P_("Use size in label"),
201 P_("Whether the label is drawn with the selected font size"),
203 GTK_PARAM_READWRITE));
206 * GtkFontButton:show-style:
208 * If this property is set to %TRUE, the name of the selected font style
209 * will be shown in the label. For a more WYSIWYG way to show the selected
210 * style, see the ::use-font property.
214 g_object_class_install_property (gobject_class,
216 g_param_spec_boolean ("show-style",
218 P_("Whether the selected font style is shown in the label"),
220 GTK_PARAM_READWRITE));
222 * GtkFontButton:show-size:
224 * If this property is set to %TRUE, the selected font size will be shown
225 * in the label. For a more WYSIWYG way to show the selected size, see the
226 * ::use-size property.
230 g_object_class_install_property (gobject_class,
232 g_param_spec_boolean ("show-size",
234 P_("Whether selected font size is shown in the label"),
236 GTK_PARAM_READWRITE));
239 * GtkFontButton::font-set:
240 * @widget: the object which received the signal.
242 * The ::font-set signal is emitted when the user selects a font.
243 * When handling this signal, use gtk_font_button_get_font_name()
244 * to find out which font was just selected.
246 * Note that this signal is only emitted when the <emphasis>user</emphasis>
247 * changes the font. If you need to react to programmatic font changes
248 * as well, use the notify::font-name signal.
252 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
253 G_TYPE_FROM_CLASS (gobject_class),
255 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
257 g_cclosure_marshal_VOID__VOID,
260 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
264 gtk_font_button_init (GtkFontButton *font_button)
266 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
267 GTK_TYPE_FONT_BUTTON,
268 GtkFontButtonPrivate);
270 /* Initialize fields */
271 font_button->priv->fontname = g_strdup (_("Sans 12"));
272 font_button->priv->use_font = FALSE;
273 font_button->priv->use_size = FALSE;
274 font_button->priv->show_style = TRUE;
275 font_button->priv->show_size = TRUE;
276 font_button->priv->font_dialog = NULL;
277 font_button->priv->title = g_strdup (_("Pick a Font"));
279 font_button->priv->inside = gtk_font_button_create_inside (font_button);
280 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
282 gtk_font_button_update_font_info (font_button);
287 gtk_font_button_finalize (GObject *object)
289 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
291 if (font_button->priv->font_dialog != NULL)
292 gtk_widget_destroy (font_button->priv->font_dialog);
293 font_button->priv->font_dialog = NULL;
295 g_free (font_button->priv->fontname);
296 font_button->priv->fontname = NULL;
298 g_free (font_button->priv->title);
299 font_button->priv->title = NULL;
301 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
305 gtk_font_button_set_property (GObject *object,
310 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
315 gtk_font_button_set_title (font_button, g_value_get_string (value));
318 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
321 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
324 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
326 case PROP_SHOW_STYLE:
327 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
330 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
333 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
339 gtk_font_button_get_property (GObject *object,
344 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
349 g_value_set_string (value, gtk_font_button_get_title (font_button));
352 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
355 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
358 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
360 case PROP_SHOW_STYLE:
361 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
364 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
374 * gtk_font_button_new:
376 * Creates a new font picker widget.
378 * Returns: a new font picker widget.
383 gtk_font_button_new (void)
385 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
389 * gtk_font_button_new_with_font:
390 * @fontname: Name of font to display in font selection dialog
392 * Creates a new font picker widget.
394 * Returns: a new font picker widget.
399 gtk_font_button_new_with_font (const gchar *fontname)
401 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
405 * gtk_font_button_set_title:
406 * @font_button: a #GtkFontButton
407 * @title: a string containing the font selection dialog title
409 * Sets the title for the font selection dialog.
414 gtk_font_button_set_title (GtkFontButton *font_button,
418 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
420 old_title = font_button->priv->title;
421 font_button->priv->title = g_strdup (title);
424 if (font_button->priv->font_dialog)
425 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
426 font_button->priv->title);
428 g_object_notify (G_OBJECT (font_button), "title");
432 * gtk_font_button_get_title:
433 * @font_button: a #GtkFontButton
435 * Retrieves the title of the font selection dialog.
437 * Returns: an internal copy of the title string which must not be freed.
441 G_CONST_RETURN gchar*
442 gtk_font_button_get_title (GtkFontButton *font_button)
444 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
446 return font_button->priv->title;
450 * gtk_font_button_get_use_font:
451 * @font_button: a #GtkFontButton
453 * Returns whether the selected font is used in the label.
455 * Returns: whether the selected font is used in the label.
460 gtk_font_button_get_use_font (GtkFontButton *font_button)
462 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
464 return font_button->priv->use_font;
468 * gtk_font_button_set_use_font:
469 * @font_button: a #GtkFontButton
470 * @use_font: If %TRUE, font name will be written using font chosen.
472 * If @use_font is %TRUE, the font name will be written using the selected font.
477 gtk_font_button_set_use_font (GtkFontButton *font_button,
480 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
482 use_font = (use_font != FALSE);
484 if (font_button->priv->use_font != use_font)
486 font_button->priv->use_font = use_font;
489 gtk_font_button_label_use_font (font_button);
491 gtk_widget_set_style (font_button->priv->font_label, NULL);
493 g_object_notify (G_OBJECT (font_button), "use-font");
499 * gtk_font_button_get_use_size:
500 * @font_button: a #GtkFontButton
502 * Returns whether the selected size is used in the label.
504 * Returns: whether the selected size is used in the label.
509 gtk_font_button_get_use_size (GtkFontButton *font_button)
511 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
513 return font_button->priv->use_size;
517 * gtk_font_button_set_use_size:
518 * @font_button: a #GtkFontButton
519 * @use_size: If %TRUE, font name will be written using the selected size.
521 * If @use_size is %TRUE, the font name will be written using the selected size.
526 gtk_font_button_set_use_size (GtkFontButton *font_button,
529 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
531 use_size = (use_size != FALSE);
532 if (font_button->priv->use_size != use_size)
534 font_button->priv->use_size = use_size;
536 if (font_button->priv->use_font)
537 gtk_font_button_label_use_font (font_button);
539 g_object_notify (G_OBJECT (font_button), "use-size");
544 * gtk_font_button_get_show_style:
545 * @font_button: a #GtkFontButton
547 * Returns whether the name of the font style will be shown in the label.
549 * Return value: whether the font style will be shown in the label.
554 gtk_font_button_get_show_style (GtkFontButton *font_button)
556 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
558 return font_button->priv->show_style;
562 * gtk_font_button_set_show_style:
563 * @font_button: a #GtkFontButton
564 * @show_style: %TRUE if font style should be displayed in label.
566 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
571 gtk_font_button_set_show_style (GtkFontButton *font_button,
574 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
576 show_style = (show_style != FALSE);
577 if (font_button->priv->show_style != show_style)
579 font_button->priv->show_style = show_style;
581 gtk_font_button_update_font_info (font_button);
583 g_object_notify (G_OBJECT (font_button), "show-style");
589 * gtk_font_button_get_show_size:
590 * @font_button: a #GtkFontButton
592 * Returns whether the font size will be shown in the label.
594 * Return value: whether the font size will be shown in the label.
599 gtk_font_button_get_show_size (GtkFontButton *font_button)
601 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
603 return font_button->priv->show_size;
607 * gtk_font_button_set_show_size:
608 * @font_button: a #GtkFontButton
609 * @show_size: %TRUE if font size should be displayed in dialog.
611 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
616 gtk_font_button_set_show_size (GtkFontButton *font_button,
619 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
621 show_size = (show_size != FALSE);
623 if (font_button->priv->show_size != show_size)
625 font_button->priv->show_size = show_size;
627 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
628 font_button->priv->inside = gtk_font_button_create_inside (font_button);
629 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
631 gtk_font_button_update_font_info (font_button);
633 g_object_notify (G_OBJECT (font_button), "show-size");
639 * gtk_font_button_get_font_name:
640 * @font_button: a #GtkFontButton
642 * Retrieves the name of the currently selected font. This name includes
643 * style and size information as well. If you want to render something
644 * with the font, use this string with pango_font_description_from_string() .
645 * If you're interested in peeking certain values (family name,
646 * style, size, weight) just query these properties from the
647 * #PangoFontDescription object.
649 * Returns: an internal copy of the font name which must not be freed.
653 G_CONST_RETURN gchar *
654 gtk_font_button_get_font_name (GtkFontButton *font_button)
656 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
658 return font_button->priv->fontname;
662 * gtk_font_button_set_font_name:
663 * @font_button: a #GtkFontButton
664 * @fontname: Name of font to display in font selection dialog
666 * Sets or updates the currently-displayed font in font picker dialog.
668 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
669 * font selection dialog exists, otherwise %FALSE.
674 gtk_font_button_set_font_name (GtkFontButton *font_button,
675 const gchar *fontname)
680 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
681 g_return_val_if_fail (fontname != NULL, FALSE);
683 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
685 old_fontname = font_button->priv->fontname;
686 font_button->priv->fontname = g_strdup (fontname);
687 g_free (old_fontname);
690 gtk_font_button_update_font_info (font_button);
692 if (font_button->priv->font_dialog)
693 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
694 font_button->priv->fontname);
698 g_object_notify (G_OBJECT (font_button), "font-name");
704 gtk_font_button_clicked (GtkButton *button)
706 GtkFontSelectionDialog *font_dialog;
707 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
709 if (!font_button->priv->font_dialog)
713 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
715 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
717 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
719 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
721 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
722 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
724 gtk_window_set_modal (GTK_WINDOW (font_dialog),
725 gtk_window_get_modal (GTK_WINDOW (parent)));
728 g_signal_connect (gtk_font_selection_dialog_get_ok_button (font_dialog), "clicked",
729 G_CALLBACK (dialog_ok_clicked), font_button);
730 g_signal_connect (gtk_font_selection_dialog_get_cancel_button (font_dialog), "clicked",
731 G_CALLBACK (dialog_cancel_clicked), font_button);
732 g_signal_connect (font_dialog, "destroy",
733 G_CALLBACK (dialog_destroy), font_button);
736 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
738 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
740 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
744 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
748 dialog_ok_clicked (GtkWidget *widget,
751 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
753 gtk_widget_hide (font_button->priv->font_dialog);
755 g_free (font_button->priv->fontname);
756 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
759 gtk_font_button_update_font_info (font_button);
761 g_object_notify (G_OBJECT (font_button), "font-name");
763 /* Emit font_set signal */
764 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
769 dialog_cancel_clicked (GtkWidget *widget,
772 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
774 gtk_widget_hide (font_button->priv->font_dialog);
778 dialog_destroy (GtkWidget *widget,
781 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
783 /* Dialog will get destroyed so reference is not valid now */
784 font_button->priv->font_dialog = NULL;
788 gtk_font_button_create_inside (GtkFontButton *font_button)
792 gtk_widget_push_composite_child ();
794 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
796 font_button->priv->font_label = gtk_label_new (_("Font"));
798 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
799 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
801 if (font_button->priv->show_size)
803 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
804 font_button->priv->size_label = gtk_label_new ("14");
805 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
808 gtk_widget_show_all (widget);
810 gtk_widget_pop_composite_child ();
816 gtk_font_button_label_use_font (GtkFontButton *font_button)
818 PangoFontDescription *desc;
820 if (!font_button->priv->use_font)
823 desc = pango_font_description_from_string (font_button->priv->fontname);
825 if (!font_button->priv->use_size)
826 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
828 gtk_widget_modify_font (font_button->priv->font_label, desc);
830 pango_font_description_free (desc);
834 font_description_style_equal (const PangoFontDescription *a,
835 const PangoFontDescription *b)
837 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
838 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
839 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
840 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
844 gtk_font_button_update_font_info (GtkFontButton *font_button)
846 PangoFontDescription *desc;
851 desc = pango_font_description_from_string (font_button->priv->fontname);
852 family = pango_font_description_get_family (desc);
855 /* This gives the wrong names, e.g. Italic when the font selection
856 * dialog displayed Oblique.
858 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
859 style = pango_font_description_to_string (desc);
860 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
864 if (font_button->priv->show_style && family)
866 PangoFontFamily **families;
867 PangoFontFace **faces;
868 gint n_families, n_faces, i;
872 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
873 &families, &n_families);
876 for (i = 0; i < n_families; i++)
878 const gchar *name = pango_font_family_get_name (families[i]);
880 if (!g_ascii_strcasecmp (name, family))
882 pango_font_family_list_faces (families[i], &faces, &n_faces);
888 for (i = 0; i < n_faces; i++)
890 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
892 if (font_description_style_equal (tmp_desc, desc))
894 style = g_strdup (pango_font_face_get_face_name (faces[i]));
895 pango_font_description_free (tmp_desc);
899 pango_font_description_free (tmp_desc);
904 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
905 family_style = g_strdup (family);
907 family_style = g_strdup_printf ("%s %s", family, style);
909 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
912 g_free (family_style);
914 if (font_button->priv->show_size)
916 gchar *size = g_strdup_printf ("%g",
917 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
919 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
924 gtk_font_button_label_use_font (font_button);
926 pango_font_description_free (desc);