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 "gtkfontchooserdialog.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 chooser dialog
50 * @Title: GtkFontButton
51 * @See_also: #GtkFontChooserDialog, #GtkColorButton.
53 * The #GtkFontButton is a button which displays the currently selected
54 * font an allows to open a font chooser 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 response_cb (GtkDialog *dialog,
111 static void dialog_destroy (GtkWidget *widget,
114 /* Auxiliary functions */
115 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
116 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
117 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
119 static guint font_button_signals[LAST_SIGNAL] = { 0 };
121 G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
124 gtk_font_button_class_init (GtkFontButtonClass *klass)
126 GObjectClass *gobject_class;
127 GtkButtonClass *button_class;
129 gobject_class = (GObjectClass *) klass;
130 button_class = (GtkButtonClass *) klass;
132 gobject_class->finalize = gtk_font_button_finalize;
133 gobject_class->set_property = gtk_font_button_set_property;
134 gobject_class->get_property = gtk_font_button_get_property;
136 button_class->clicked = gtk_font_button_clicked;
138 klass->font_set = NULL;
141 * GtkFontButton:title:
143 * The title of the font chooser dialog.
147 g_object_class_install_property (gobject_class,
149 g_param_spec_string ("title",
151 P_("The title of the font chooser dialog"),
153 (GTK_PARAM_READABLE |
154 GTK_PARAM_WRITABLE)));
157 * GtkFontButton:font-name:
159 * The name of the currently selected font.
163 g_object_class_install_property (gobject_class,
165 g_param_spec_string ("font-name",
167 P_("The name of the selected font"),
169 (GTK_PARAM_READABLE |
170 GTK_PARAM_WRITABLE)));
173 * GtkFontButton:use-font:
175 * If this property is set to %TRUE, the label will be drawn
176 * in the selected font.
180 g_object_class_install_property (gobject_class,
182 g_param_spec_boolean ("use-font",
183 P_("Use font in label"),
184 P_("Whether the label is drawn in the selected font"),
186 GTK_PARAM_READWRITE));
189 * GtkFontButton:use-size:
191 * If this property is set to %TRUE, the label will be drawn
192 * with the selected font size.
196 g_object_class_install_property (gobject_class,
198 g_param_spec_boolean ("use-size",
199 P_("Use size in label"),
200 P_("Whether the label is drawn with the selected font size"),
202 GTK_PARAM_READWRITE));
205 * GtkFontButton:show-style:
207 * If this property is set to %TRUE, the name of the selected font style
208 * will be shown in the label. For a more WYSIWYG way to show the selected
209 * style, see the ::use-font property.
213 g_object_class_install_property (gobject_class,
215 g_param_spec_boolean ("show-style",
217 P_("Whether the selected font style is shown in the label"),
219 GTK_PARAM_READWRITE));
221 * GtkFontButton:show-size:
223 * If this property is set to %TRUE, the selected font size will be shown
224 * in the label. For a more WYSIWYG way to show the selected size, see the
225 * ::use-size property.
229 g_object_class_install_property (gobject_class,
231 g_param_spec_boolean ("show-size",
233 P_("Whether selected font size is shown in the label"),
235 GTK_PARAM_READWRITE));
238 * GtkFontButton::font-set:
239 * @widget: the object which received the signal.
241 * The ::font-set signal is emitted when the user selects a font.
242 * When handling this signal, use gtk_font_button_get_font_name()
243 * to find out which font was just selected.
245 * Note that this signal is only emitted when the <emphasis>user</emphasis>
246 * changes the font. If you need to react to programmatic font changes
247 * as well, use the notify::font-name signal.
251 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
252 G_TYPE_FROM_CLASS (gobject_class),
254 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
256 g_cclosure_marshal_VOID__VOID,
259 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
263 gtk_font_button_init (GtkFontButton *font_button)
265 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
266 GTK_TYPE_FONT_BUTTON,
267 GtkFontButtonPrivate);
269 /* Initialize fields */
270 font_button->priv->fontname = g_strdup (_("Sans 12"));
271 font_button->priv->use_font = FALSE;
272 font_button->priv->use_size = FALSE;
273 font_button->priv->show_style = TRUE;
274 font_button->priv->show_size = TRUE;
275 font_button->priv->font_dialog = NULL;
276 font_button->priv->title = g_strdup (_("Pick a Font"));
278 font_button->priv->inside = gtk_font_button_create_inside (font_button);
279 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
281 gtk_font_button_update_font_info (font_button);
286 gtk_font_button_finalize (GObject *object)
288 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
290 if (font_button->priv->font_dialog != NULL)
291 gtk_widget_destroy (font_button->priv->font_dialog);
292 font_button->priv->font_dialog = NULL;
294 g_free (font_button->priv->fontname);
295 font_button->priv->fontname = NULL;
297 g_free (font_button->priv->title);
298 font_button->priv->title = NULL;
300 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
304 gtk_font_button_set_property (GObject *object,
309 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
314 gtk_font_button_set_title (font_button, g_value_get_string (value));
317 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
320 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
323 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
325 case PROP_SHOW_STYLE:
326 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
329 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
338 gtk_font_button_get_property (GObject *object,
343 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
348 g_value_set_string (value, gtk_font_button_get_title (font_button));
351 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
354 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
357 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
359 case PROP_SHOW_STYLE:
360 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
363 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
373 * gtk_font_button_new:
375 * Creates a new font picker widget.
377 * Returns: a new font picker widget.
382 gtk_font_button_new (void)
384 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
388 * gtk_font_button_new_with_font:
389 * @fontname: Name of font to display in font chooser dialog
391 * Creates a new font picker widget.
393 * Returns: a new font picker widget.
398 gtk_font_button_new_with_font (const gchar *fontname)
400 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
404 * gtk_font_button_set_title:
405 * @font_button: a #GtkFontButton
406 * @title: a string containing the font chooser dialog title
408 * Sets the title for the font chooser dialog.
413 gtk_font_button_set_title (GtkFontButton *font_button,
417 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
419 old_title = font_button->priv->title;
420 font_button->priv->title = g_strdup (title);
423 if (font_button->priv->font_dialog)
424 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
425 font_button->priv->title);
427 g_object_notify (G_OBJECT (font_button), "title");
431 * gtk_font_button_get_title:
432 * @font_button: a #GtkFontButton
434 * Retrieves the title of the font chooser dialog.
436 * Returns: an internal copy of the title string which must not be freed.
441 gtk_font_button_get_title (GtkFontButton *font_button)
443 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
445 return font_button->priv->title;
449 * gtk_font_button_get_use_font:
450 * @font_button: a #GtkFontButton
452 * Returns whether the selected font is used in the label.
454 * Returns: whether the selected font is used in the label.
459 gtk_font_button_get_use_font (GtkFontButton *font_button)
461 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
463 return font_button->priv->use_font;
467 * gtk_font_button_set_use_font:
468 * @font_button: a #GtkFontButton
469 * @use_font: If %TRUE, font name will be written using font chosen.
471 * If @use_font is %TRUE, the font name will be written using the selected font.
476 gtk_font_button_set_use_font (GtkFontButton *font_button,
479 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
481 use_font = (use_font != FALSE);
483 if (font_button->priv->use_font != use_font)
485 font_button->priv->use_font = use_font;
488 gtk_font_button_label_use_font (font_button);
490 gtk_widget_set_style (font_button->priv->font_label, NULL);
492 g_object_notify (G_OBJECT (font_button), "use-font");
498 * gtk_font_button_get_use_size:
499 * @font_button: a #GtkFontButton
501 * Returns whether the selected size is used in the label.
503 * Returns: whether the selected size is used in the label.
508 gtk_font_button_get_use_size (GtkFontButton *font_button)
510 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
512 return font_button->priv->use_size;
516 * gtk_font_button_set_use_size:
517 * @font_button: a #GtkFontButton
518 * @use_size: If %TRUE, font name will be written using the selected size.
520 * If @use_size is %TRUE, the font name will be written using the selected size.
525 gtk_font_button_set_use_size (GtkFontButton *font_button,
528 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
530 use_size = (use_size != FALSE);
531 if (font_button->priv->use_size != use_size)
533 font_button->priv->use_size = use_size;
535 if (font_button->priv->use_font)
536 gtk_font_button_label_use_font (font_button);
538 g_object_notify (G_OBJECT (font_button), "use-size");
543 * gtk_font_button_get_show_style:
544 * @font_button: a #GtkFontButton
546 * Returns whether the name of the font style will be shown in the label.
548 * Return value: whether the font style will be shown in the label.
553 gtk_font_button_get_show_style (GtkFontButton *font_button)
555 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
557 return font_button->priv->show_style;
561 * gtk_font_button_set_show_style:
562 * @font_button: a #GtkFontButton
563 * @show_style: %TRUE if font style should be displayed in label.
565 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
570 gtk_font_button_set_show_style (GtkFontButton *font_button,
573 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
575 show_style = (show_style != FALSE);
576 if (font_button->priv->show_style != show_style)
578 font_button->priv->show_style = show_style;
580 gtk_font_button_update_font_info (font_button);
582 g_object_notify (G_OBJECT (font_button), "show-style");
588 * gtk_font_button_get_show_size:
589 * @font_button: a #GtkFontButton
591 * Returns whether the font size will be shown in the label.
593 * Return value: whether the font size will be shown in the label.
598 gtk_font_button_get_show_size (GtkFontButton *font_button)
600 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
602 return font_button->priv->show_size;
606 * gtk_font_button_set_show_size:
607 * @font_button: a #GtkFontButton
608 * @show_size: %TRUE if font size should be displayed in dialog.
610 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
615 gtk_font_button_set_show_size (GtkFontButton *font_button,
618 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
620 show_size = (show_size != FALSE);
622 if (font_button->priv->show_size != show_size)
624 font_button->priv->show_size = show_size;
626 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
627 font_button->priv->inside = gtk_font_button_create_inside (font_button);
628 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
630 gtk_font_button_update_font_info (font_button);
632 g_object_notify (G_OBJECT (font_button), "show-size");
638 * gtk_font_button_get_font_name:
639 * @font_button: a #GtkFontButton
641 * Retrieves the name of the currently selected font. This name includes
642 * style and size information as well. If you want to render something
643 * with the font, use this string with pango_font_description_from_string() .
644 * If you're interested in peeking certain values (family name,
645 * style, size, weight) just query these properties from the
646 * #PangoFontDescription object.
648 * Returns: an internal copy of the font name which must not be freed.
653 gtk_font_button_get_font_name (GtkFontButton *font_button)
655 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
657 return font_button->priv->fontname;
661 * gtk_font_button_set_font_name:
662 * @font_button: a #GtkFontButton
663 * @fontname: Name of font to display in font chooser dialog
665 * Sets or updates the currently-displayed font in font picker dialog.
667 * Returns: Return value of gtk_font_chooser_dialog_set_font_name() if the
668 * font chooser dialog exists, otherwise %FALSE.
673 gtk_font_button_set_font_name (GtkFontButton *font_button,
674 const gchar *fontname)
679 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
680 g_return_val_if_fail (fontname != NULL, FALSE);
682 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
684 old_fontname = font_button->priv->fontname;
685 font_button->priv->fontname = g_strdup (fontname);
686 g_free (old_fontname);
689 gtk_font_button_update_font_info (font_button);
691 if (font_button->priv->font_dialog)
692 result = gtk_font_chooser_dialog_set_font_name (GTK_FONT_CHOOSER_DIALOG (font_button->priv->font_dialog),
693 font_button->priv->fontname);
697 g_object_notify (G_OBJECT (font_button), "font-name");
703 gtk_font_button_clicked (GtkButton *button)
705 GtkFontChooserDialog *font_dialog;
706 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
708 if (!font_button->priv->font_dialog)
712 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
714 font_button->priv->font_dialog = gtk_font_chooser_dialog_new (font_button->priv->title,
717 font_dialog = GTK_FONT_CHOOSER_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 (font_dialog, "response",
729 G_CALLBACK (response_cb), font_button);
731 g_signal_connect (font_dialog, "destroy",
732 G_CALLBACK (dialog_destroy), font_button);
735 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
737 font_dialog = GTK_FONT_CHOOSER_DIALOG (font_button->priv->font_dialog);
738 gtk_font_chooser_dialog_set_font_name (font_dialog, font_button->priv->fontname);
741 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
746 response_cb (GtkDialog *dialog,
750 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
751 gtk_widget_hide (font_button->priv->font_dialog);
753 if (response_id != GTK_RESPONSE_OK)
756 g_free (font_button->priv->fontname);
757 font_button->priv->fontname = gtk_font_chooser_dialog_get_font_name (GTK_FONT_CHOOSER_DIALOG (font_button->priv->font_dialog));
760 gtk_font_button_update_font_info (font_button);
762 g_object_notify (G_OBJECT (font_button), "font-name");
764 /* Emit font_set signal */
765 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
769 dialog_destroy (GtkWidget *widget,
772 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
774 /* Dialog will get destroyed so reference is not valid now */
775 font_button->priv->font_dialog = NULL;
779 gtk_font_button_create_inside (GtkFontButton *font_button)
783 gtk_widget_push_composite_child ();
785 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
787 font_button->priv->font_label = gtk_label_new (_("Font"));
789 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
790 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
792 if (font_button->priv->show_size)
794 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
795 font_button->priv->size_label = gtk_label_new ("14");
796 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
799 gtk_widget_show_all (widget);
801 gtk_widget_pop_composite_child ();
807 gtk_font_button_label_use_font (GtkFontButton *font_button)
809 PangoFontDescription *desc;
811 if (!font_button->priv->use_font)
814 desc = pango_font_description_from_string (font_button->priv->fontname);
816 if (!font_button->priv->use_size)
817 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
819 gtk_widget_modify_font (font_button->priv->font_label, desc);
821 pango_font_description_free (desc);
825 font_description_style_equal (const PangoFontDescription *a,
826 const PangoFontDescription *b)
828 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
829 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
830 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
831 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
835 gtk_font_button_update_font_info (GtkFontButton *font_button)
837 PangoFontDescription *desc;
842 desc = pango_font_description_from_string (font_button->priv->fontname);
843 family = pango_font_description_get_family (desc);
846 /* This gives the wrong names, e.g. Italic when the font chooser
847 * dialog displayed Oblique.
849 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
850 style = pango_font_description_to_string (desc);
851 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
855 if (font_button->priv->show_style && family)
857 PangoFontFamily **families;
858 PangoFontFace **faces;
859 gint n_families, n_faces, i;
863 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
864 &families, &n_families);
867 for (i = 0; i < n_families; i++)
869 const gchar *name = pango_font_family_get_name (families[i]);
871 if (!g_ascii_strcasecmp (name, family))
873 pango_font_family_list_faces (families[i], &faces, &n_faces);
879 for (i = 0; i < n_faces; i++)
881 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
883 if (font_description_style_equal (tmp_desc, desc))
885 style = g_strdup (pango_font_face_get_face_name (faces[i]));
886 pango_font_description_free (tmp_desc);
890 pango_font_description_free (tmp_desc);
895 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
896 family_style = g_strdup (family);
898 family_style = g_strdup_printf ("%s %s", family, style);
900 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
903 g_free (family_style);
905 if (font_button->priv->show_size)
907 gchar *size = g_strdup_printf ("%g",
908 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
910 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
915 gtk_font_button_label_use_font (font_button);
917 pango_font_description_free (desc);