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"
34 #include "gtkalignment.h"
37 #include "gtkvseparator.h"
38 #include "gtkfontsel.h"
40 #include "gtkmarshalers.h"
41 #include "gtkprivate.h"
49 * SECTION:gtkfontbutton
50 * @Short_description: A button to launch a font selection dialog
51 * @Title: GtkFontButton
52 * @See_also: #GtkFontSelectionDialog, #GtkColorButton.
54 * The #GtkFontButton is a button which displays the currently selected
55 * font an allows to open a font selection dialog to change the font.
56 * It is suitable widget for selecting a font in a preference dialog.
60 struct _GtkFontButtonPrivate
71 GtkWidget *font_dialog;
73 GtkWidget *font_label;
74 GtkWidget *size_label;
96 static void gtk_font_button_finalize (GObject *object);
97 static void gtk_font_button_get_property (GObject *object,
101 static void gtk_font_button_set_property (GObject *object,
106 static void gtk_font_button_clicked (GtkButton *button);
108 /* Dialog response functions */
109 static void dialog_ok_clicked (GtkWidget *widget,
111 static void dialog_cancel_clicked (GtkWidget *widget,
113 static void dialog_destroy (GtkWidget *widget,
116 /* Auxiliary functions */
117 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
118 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
119 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
121 static guint font_button_signals[LAST_SIGNAL] = { 0 };
123 G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
126 gtk_font_button_class_init (GtkFontButtonClass *klass)
128 GObjectClass *gobject_class;
129 GtkButtonClass *button_class;
131 gobject_class = (GObjectClass *) klass;
132 button_class = (GtkButtonClass *) klass;
134 gobject_class->finalize = gtk_font_button_finalize;
135 gobject_class->set_property = gtk_font_button_set_property;
136 gobject_class->get_property = gtk_font_button_get_property;
138 button_class->clicked = gtk_font_button_clicked;
140 klass->font_set = NULL;
143 * GtkFontButton:title:
145 * The title of the font selection dialog.
149 g_object_class_install_property (gobject_class,
151 g_param_spec_string ("title",
153 P_("The title of the font selection dialog"),
155 (GTK_PARAM_READABLE |
156 GTK_PARAM_WRITABLE)));
159 * GtkFontButton:font-name:
161 * The name of the currently selected font.
165 g_object_class_install_property (gobject_class,
167 g_param_spec_string ("font-name",
169 P_("The name of the selected font"),
171 (GTK_PARAM_READABLE |
172 GTK_PARAM_WRITABLE)));
175 * GtkFontButton:use-font:
177 * If this property is set to %TRUE, the label will be drawn
178 * in the selected font.
182 g_object_class_install_property (gobject_class,
184 g_param_spec_boolean ("use-font",
185 P_("Use font in label"),
186 P_("Whether the label is drawn in the selected font"),
188 GTK_PARAM_READWRITE));
191 * GtkFontButton:use-size:
193 * If this property is set to %TRUE, the label will be drawn
194 * with the selected font size.
198 g_object_class_install_property (gobject_class,
200 g_param_spec_boolean ("use-size",
201 P_("Use size in label"),
202 P_("Whether the label is drawn with the selected font size"),
204 GTK_PARAM_READWRITE));
207 * GtkFontButton:show-style:
209 * If this property is set to %TRUE, the name of the selected font style
210 * will be shown in the label. For a more WYSIWYG way to show the selected
211 * style, see the ::use-font property.
215 g_object_class_install_property (gobject_class,
217 g_param_spec_boolean ("show-style",
219 P_("Whether the selected font style is shown in the label"),
221 GTK_PARAM_READWRITE));
223 * GtkFontButton:show-size:
225 * If this property is set to %TRUE, the selected font size will be shown
226 * in the label. For a more WYSIWYG way to show the selected size, see the
227 * ::use-size property.
231 g_object_class_install_property (gobject_class,
233 g_param_spec_boolean ("show-size",
235 P_("Whether selected font size is shown in the label"),
237 GTK_PARAM_READWRITE));
240 * GtkFontButton::font-set:
241 * @widget: the object which received the signal.
243 * The ::font-set signal is emitted when the user selects a font.
244 * When handling this signal, use gtk_font_button_get_font_name()
245 * to find out which font was just selected.
247 * Note that this signal is only emitted when the <emphasis>user</emphasis>
248 * changes the font. If you need to react to programmatic font changes
249 * as well, use the notify::font-name signal.
253 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
254 G_TYPE_FROM_CLASS (gobject_class),
256 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
258 g_cclosure_marshal_VOID__VOID,
261 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
265 gtk_font_button_init (GtkFontButton *font_button)
267 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
268 GTK_TYPE_FONT_BUTTON,
269 GtkFontButtonPrivate);
271 /* Initialize fields */
272 font_button->priv->fontname = g_strdup (_("Sans 12"));
273 font_button->priv->use_font = FALSE;
274 font_button->priv->use_size = FALSE;
275 font_button->priv->show_style = TRUE;
276 font_button->priv->show_size = TRUE;
277 font_button->priv->font_dialog = NULL;
278 font_button->priv->title = g_strdup (_("Pick a Font"));
280 font_button->priv->inside = gtk_font_button_create_inside (font_button);
281 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
283 gtk_font_button_update_font_info (font_button);
288 gtk_font_button_finalize (GObject *object)
290 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
292 if (font_button->priv->font_dialog != NULL)
293 gtk_widget_destroy (font_button->priv->font_dialog);
294 font_button->priv->font_dialog = NULL;
296 g_free (font_button->priv->fontname);
297 font_button->priv->fontname = NULL;
299 g_free (font_button->priv->title);
300 font_button->priv->title = NULL;
302 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
306 gtk_font_button_set_property (GObject *object,
311 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
316 gtk_font_button_set_title (font_button, g_value_get_string (value));
319 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
322 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
325 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
327 case PROP_SHOW_STYLE:
328 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
331 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
340 gtk_font_button_get_property (GObject *object,
345 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
350 g_value_set_string (value, gtk_font_button_get_title (font_button));
353 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
356 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
359 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
361 case PROP_SHOW_STYLE:
362 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
365 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
375 * gtk_font_button_new:
377 * Creates a new font picker widget.
379 * Returns: a new font picker widget.
384 gtk_font_button_new (void)
386 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
390 * gtk_font_button_new_with_font:
391 * @fontname: Name of font to display in font selection dialog
393 * Creates a new font picker widget.
395 * Returns: a new font picker widget.
400 gtk_font_button_new_with_font (const gchar *fontname)
402 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
406 * gtk_font_button_set_title:
407 * @font_button: a #GtkFontButton
408 * @title: a string containing the font selection dialog title
410 * Sets the title for the font selection dialog.
415 gtk_font_button_set_title (GtkFontButton *font_button,
419 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
421 old_title = font_button->priv->title;
422 font_button->priv->title = g_strdup (title);
425 if (font_button->priv->font_dialog)
426 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
427 font_button->priv->title);
429 g_object_notify (G_OBJECT (font_button), "title");
433 * gtk_font_button_get_title:
434 * @font_button: a #GtkFontButton
436 * Retrieves the title of the font selection dialog.
438 * Returns: an internal copy of the title string which must not be freed.
442 G_CONST_RETURN gchar*
443 gtk_font_button_get_title (GtkFontButton *font_button)
445 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
447 return font_button->priv->title;
451 * gtk_font_button_get_use_font:
452 * @font_button: a #GtkFontButton
454 * Returns whether the selected font is used in the label.
456 * Returns: whether the selected font is used in the label.
461 gtk_font_button_get_use_font (GtkFontButton *font_button)
463 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
465 return font_button->priv->use_font;
469 * gtk_font_button_set_use_font:
470 * @font_button: a #GtkFontButton
471 * @use_font: If %TRUE, font name will be written using font chosen.
473 * If @use_font is %TRUE, the font name will be written using the selected font.
478 gtk_font_button_set_use_font (GtkFontButton *font_button,
481 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
483 use_font = (use_font != FALSE);
485 if (font_button->priv->use_font != use_font)
487 font_button->priv->use_font = use_font;
490 gtk_font_button_label_use_font (font_button);
492 gtk_widget_set_style (font_button->priv->font_label, NULL);
494 g_object_notify (G_OBJECT (font_button), "use-font");
500 * gtk_font_button_get_use_size:
501 * @font_button: a #GtkFontButton
503 * Returns whether the selected size is used in the label.
505 * Returns: whether the selected size is used in the label.
510 gtk_font_button_get_use_size (GtkFontButton *font_button)
512 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
514 return font_button->priv->use_size;
518 * gtk_font_button_set_use_size:
519 * @font_button: a #GtkFontButton
520 * @use_size: If %TRUE, font name will be written using the selected size.
522 * If @use_size is %TRUE, the font name will be written using the selected size.
527 gtk_font_button_set_use_size (GtkFontButton *font_button,
530 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
532 use_size = (use_size != FALSE);
533 if (font_button->priv->use_size != use_size)
535 font_button->priv->use_size = use_size;
537 if (font_button->priv->use_font)
538 gtk_font_button_label_use_font (font_button);
540 g_object_notify (G_OBJECT (font_button), "use-size");
545 * gtk_font_button_get_show_style:
546 * @font_button: a #GtkFontButton
548 * Returns whether the name of the font style will be shown in the label.
550 * Return value: whether the font style will be shown in the label.
555 gtk_font_button_get_show_style (GtkFontButton *font_button)
557 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
559 return font_button->priv->show_style;
563 * gtk_font_button_set_show_style:
564 * @font_button: a #GtkFontButton
565 * @show_style: %TRUE if font style should be displayed in label.
567 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
572 gtk_font_button_set_show_style (GtkFontButton *font_button,
575 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
577 show_style = (show_style != FALSE);
578 if (font_button->priv->show_style != show_style)
580 font_button->priv->show_style = show_style;
582 gtk_font_button_update_font_info (font_button);
584 g_object_notify (G_OBJECT (font_button), "show-style");
590 * gtk_font_button_get_show_size:
591 * @font_button: a #GtkFontButton
593 * Returns whether the font size will be shown in the label.
595 * Return value: whether the font size will be shown in the label.
600 gtk_font_button_get_show_size (GtkFontButton *font_button)
602 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
604 return font_button->priv->show_size;
608 * gtk_font_button_set_show_size:
609 * @font_button: a #GtkFontButton
610 * @show_size: %TRUE if font size should be displayed in dialog.
612 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
617 gtk_font_button_set_show_size (GtkFontButton *font_button,
620 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
622 show_size = (show_size != FALSE);
624 if (font_button->priv->show_size != show_size)
626 font_button->priv->show_size = show_size;
628 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
629 font_button->priv->inside = gtk_font_button_create_inside (font_button);
630 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
632 gtk_font_button_update_font_info (font_button);
634 g_object_notify (G_OBJECT (font_button), "show-size");
640 * gtk_font_button_get_font_name:
641 * @font_button: a #GtkFontButton
643 * Retrieves the name of the currently selected font. This name includes
644 * style and size information as well. If you want to render something
645 * with the font, use this string with pango_font_description_from_string() .
646 * If you're interested in peeking certain values (family name,
647 * style, size, weight) just query these properties from the
648 * #PangoFontDescription object.
650 * Returns: an internal copy of the font name which must not be freed.
654 G_CONST_RETURN gchar *
655 gtk_font_button_get_font_name (GtkFontButton *font_button)
657 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
659 return font_button->priv->fontname;
663 * gtk_font_button_set_font_name:
664 * @font_button: a #GtkFontButton
665 * @fontname: Name of font to display in font selection dialog
667 * Sets or updates the currently-displayed font in font picker dialog.
669 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
670 * font selection dialog exists, otherwise %FALSE.
675 gtk_font_button_set_font_name (GtkFontButton *font_button,
676 const gchar *fontname)
681 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
682 g_return_val_if_fail (fontname != NULL, FALSE);
684 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
686 old_fontname = font_button->priv->fontname;
687 font_button->priv->fontname = g_strdup (fontname);
688 g_free (old_fontname);
691 gtk_font_button_update_font_info (font_button);
693 if (font_button->priv->font_dialog)
694 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
695 font_button->priv->fontname);
699 g_object_notify (G_OBJECT (font_button), "font-name");
705 gtk_font_button_clicked (GtkButton *button)
707 GtkFontSelectionDialog *font_dialog;
708 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
710 if (!font_button->priv->font_dialog)
714 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
716 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
718 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
720 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
722 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
723 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
725 gtk_window_set_modal (GTK_WINDOW (font_dialog),
726 gtk_window_get_modal (GTK_WINDOW (parent)));
729 g_signal_connect (gtk_font_selection_dialog_get_ok_button (font_dialog), "clicked",
730 G_CALLBACK (dialog_ok_clicked), font_button);
731 g_signal_connect (gtk_font_selection_dialog_get_cancel_button (font_dialog), "clicked",
732 G_CALLBACK (dialog_cancel_clicked), font_button);
733 g_signal_connect (font_dialog, "destroy",
734 G_CALLBACK (dialog_destroy), font_button);
737 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
739 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
741 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
745 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
749 dialog_ok_clicked (GtkWidget *widget,
752 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
754 gtk_widget_hide (font_button->priv->font_dialog);
756 g_free (font_button->priv->fontname);
757 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_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);
770 dialog_cancel_clicked (GtkWidget *widget,
773 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
775 gtk_widget_hide (font_button->priv->font_dialog);
779 dialog_destroy (GtkWidget *widget,
782 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
784 /* Dialog will get destroyed so reference is not valid now */
785 font_button->priv->font_dialog = NULL;
789 gtk_font_button_create_inside (GtkFontButton *font_button)
793 gtk_widget_push_composite_child ();
795 widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
797 font_button->priv->font_label = gtk_label_new (_("Font"));
799 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
800 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
802 if (font_button->priv->show_size)
804 gtk_box_pack_start (GTK_BOX (widget), gtk_separator_new (GTK_ORIENTATION_VERTICAL), FALSE, FALSE, 0);
805 font_button->priv->size_label = gtk_label_new ("14");
806 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
809 gtk_widget_show_all (widget);
811 gtk_widget_pop_composite_child ();
817 gtk_font_button_label_use_font (GtkFontButton *font_button)
819 PangoFontDescription *desc;
821 if (!font_button->priv->use_font)
824 desc = pango_font_description_from_string (font_button->priv->fontname);
826 if (!font_button->priv->use_size)
827 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
829 gtk_widget_modify_font (font_button->priv->font_label, desc);
831 pango_font_description_free (desc);
835 font_description_style_equal (const PangoFontDescription *a,
836 const PangoFontDescription *b)
838 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
839 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
840 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
841 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
845 gtk_font_button_update_font_info (GtkFontButton *font_button)
847 PangoFontDescription *desc;
852 desc = pango_font_description_from_string (font_button->priv->fontname);
853 family = pango_font_description_get_family (desc);
856 /* This gives the wrong names, e.g. Italic when the font selection
857 * dialog displayed Oblique.
859 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
860 style = pango_font_description_to_string (desc);
861 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
865 if (font_button->priv->show_style && family)
867 PangoFontFamily **families;
868 PangoFontFace **faces;
869 gint n_families, n_faces, i;
873 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
874 &families, &n_families);
877 for (i = 0; i < n_families; i++)
879 const gchar *name = pango_font_family_get_name (families[i]);
881 if (!g_ascii_strcasecmp (name, family))
883 pango_font_family_list_faces (families[i], &faces, &n_faces);
889 for (i = 0; i < n_faces; i++)
891 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
893 if (font_description_style_equal (tmp_desc, desc))
895 style = g_strdup (pango_font_face_get_face_name (faces[i]));
896 pango_font_description_free (tmp_desc);
900 pango_font_description_free (tmp_desc);
905 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
906 family_style = g_strdup (family);
908 family_style = g_strdup_printf ("%s %s", family, style);
910 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
913 g_free (family_style);
915 if (font_button->priv->show_size)
917 gchar *size = g_strdup_printf ("%g",
918 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
920 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
925 gtk_font_button_label_use_font (font_button);
927 pango_font_description_free (desc);