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"
33 #include "gtksignal.h"
35 #include "gtkalignment.h"
38 #include "gtkvseparator.h"
39 #include "gtkfontsel.h"
41 #include "gtkmarshalers.h"
47 #define GTK_FONT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButtonPrivate))
49 struct _GtkFontButtonPrivate
60 GtkWidget *font_dialog;
62 GtkWidget *font_label;
63 GtkWidget *size_label;
85 static void gtk_font_button_init (GtkFontButton *font_button);
86 static void gtk_font_button_class_init (GtkFontButtonClass *klass);
87 static void gtk_font_button_finalize (GObject *object);
88 static void gtk_font_button_get_property (GObject *object,
92 static void gtk_font_button_set_property (GObject *object,
97 static void gtk_font_button_clicked (GtkButton *button);
99 /* Dialog response functions */
100 static void dialog_ok_clicked (GtkWidget *widget,
102 static void dialog_cancel_clicked (GtkWidget *widget,
104 static void dialog_destroy (GtkWidget *widget,
107 /* Auxiliary functions */
108 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
109 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
110 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
112 static gpointer parent_class = NULL;
113 static guint font_button_signals[LAST_SIGNAL] = { 0 };
116 gtk_font_button_get_type (void)
118 static GType font_button_type = 0;
120 if (!font_button_type)
122 static const GTypeInfo font_button_info =
124 sizeof (GtkFontButtonClass),
125 NULL, /* base_init */
126 NULL, /* base_finalize */
127 (GClassInitFunc) gtk_font_button_class_init,
128 NULL, /* class_finalize */
129 NULL, /* class_data */
130 sizeof (GtkFontButton),
132 (GInstanceInitFunc) gtk_font_button_init,
136 g_type_register_static (GTK_TYPE_BUTTON, "GtkFontButton",
137 &font_button_info, 0);
140 return font_button_type;
145 gtk_font_button_class_init (GtkFontButtonClass *klass)
147 GtkObjectClass *object_class;
148 GObjectClass *gobject_class;
149 GtkButtonClass *button_class;
151 object_class = (GtkObjectClass *) klass;
152 gobject_class = (GObjectClass *) klass;
153 button_class = (GtkButtonClass *) klass;
155 parent_class = g_type_class_peek_parent (klass);
157 gobject_class->finalize = gtk_font_button_finalize;
158 gobject_class->set_property = gtk_font_button_set_property;
159 gobject_class->get_property = gtk_font_button_get_property;
161 button_class->clicked = gtk_font_button_clicked;
163 klass->font_set = NULL;
166 * GtkFontButton:title:
168 * The title of the font selection dialog.
172 g_object_class_install_property (gobject_class,
174 g_param_spec_string ("title",
176 _("The title of the font selection dialog"),
182 * GtkFontButton:font-name:
184 * The name of the currently selected font.
188 g_object_class_install_property (gobject_class,
190 g_param_spec_string ("font_name",
192 _("The name of the selected font"),
198 * GtkFontButton:use-font:
200 * If this property is set to %TRUE, the label will be drawn in the selected font.
204 g_object_class_install_property (gobject_class,
206 g_param_spec_boolean ("use_font",
207 _("Use font in label"),
208 _("Whether the label is drawn in the selected font"),
214 * GtkFontButton:use-size:
216 * If this property is set to %TRUE, the label will be drawn with the selected font size.
220 g_object_class_install_property (gobject_class,
222 g_param_spec_boolean ("use_size",
223 _("Use size in label"),
224 _("Whether the label is drawn with the selected font size"),
230 * GtkFontButton:show-style:
232 * If this property is set to %TRUE, the name of the selected font style will be shown in the label. For
233 * a more WYSIWIG way to show the selected style, see the ::use-font property.
237 g_object_class_install_property (gobject_class,
239 g_param_spec_boolean ("show_style",
241 _("Whether the selected font style is shown in the label"),
246 * GtkFontButton:show-size:
248 * If this property is set to %TRUE, the selected font size will be shown in the label. For
249 * a more WYSIWIG way to show the selected size, see the ::use-size property.
253 g_object_class_install_property (gobject_class,
255 g_param_spec_boolean ("show_size",
257 _("Whether selected font size is shown in the label"),
263 * GtkFontButton::font-set:
264 * @widget: the object which received the signal.
266 * The ::font-set signal is emitted when the user selects a font. When handling this signal,
267 * use gtk_font_button_get_font_name() to find out which font was just selected.
271 font_button_signals[FONT_SET] = g_signal_new ("font_set",
272 G_TYPE_FROM_CLASS (gobject_class),
274 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
276 g_cclosure_marshal_VOID__VOID,
279 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
283 gtk_font_button_init (GtkFontButton *font_button)
285 font_button->priv = GTK_FONT_BUTTON_GET_PRIVATE (font_button);
287 /* Initialize fields */
288 font_button->priv->fontname = g_strdup (_("Sans 12"));
289 font_button->priv->use_font = FALSE;
290 font_button->priv->use_size = FALSE;
291 font_button->priv->show_style = TRUE;
292 font_button->priv->show_size = TRUE;
293 font_button->priv->font_dialog = NULL;
294 font_button->priv->title = g_strdup (_("Pick a Font"));
296 font_button->priv->inside = gtk_font_button_create_inside (font_button);
297 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
299 gtk_font_button_update_font_info (font_button);
304 gtk_font_button_finalize (GObject *object)
306 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
308 if (font_button->priv->font_dialog != NULL)
309 gtk_widget_destroy (font_button->priv->font_dialog);
310 font_button->priv->font_dialog = NULL;
312 g_free (font_button->priv->fontname);
313 font_button->priv->fontname = NULL;
315 g_free (font_button->priv->title);
316 font_button->priv->title = NULL;
318 G_OBJECT_CLASS (parent_class)->finalize (object);
322 gtk_font_button_set_property (GObject *object,
327 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
332 gtk_font_button_set_title (font_button, g_value_get_string (value));
335 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
338 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
341 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
343 case PROP_SHOW_STYLE:
344 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
347 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
356 gtk_font_button_get_property (GObject *object,
361 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
366 g_value_set_string (value, gtk_font_button_get_title (font_button));
369 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
372 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
375 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
377 case PROP_SHOW_STYLE:
378 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
381 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
391 * gtk_font_button_new:
393 * Creates a new font picker widget.
395 * Returns: a new font picker widget.
400 gtk_font_button_new (void)
402 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
406 * gtk_font_button_new_with_font:
407 * @fontname: Name of font to display in font selection dialog
409 * Creates a new font picker widget.
411 * Returns: a new font picker widget.
416 gtk_font_button_new_with_font (const gchar *fontname)
418 return g_object_new (GTK_TYPE_FONT_BUTTON, "font_name", fontname, NULL);
422 * gtk_font_button_set_title:
423 * @font_button: a #GtkFontButton
424 * @title: a string containing the font selection dialog title
426 * Sets the title for the font selection dialog.
431 gtk_font_button_set_title (GtkFontButton *font_button,
435 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
437 old_title = font_button->priv->title;
438 font_button->priv->title = g_strdup (title);
441 if (font_button->priv->font_dialog)
442 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
443 font_button->priv->title);
445 g_object_notify (G_OBJECT (font_button), "title");
449 * gtk_font_button_get_title:
450 * @font_button: a #GtkFontButton
452 * Retrieves the title of the font selection dialog.
454 * Returns: an internal copy of the title string which must not be freed.
458 G_CONST_RETURN gchar*
459 gtk_font_button_get_title (GtkFontButton *font_button)
461 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
463 return font_button->priv->title;
467 * gtk_font_button_get_use_font:
468 * @font_button: a #GtkFontButton
470 * Returns whether the selected font is used in the label.
472 * Returns: whether the selected font is used in the label.
477 gtk_font_button_get_use_font (GtkFontButton *font_button)
479 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
481 return font_button->priv->use_font;
485 * gtk_font_button_set_use_font:
486 * @font_button: a #GtkFontButton
487 * @use_font: If %TRUE, font name will be written using font chosen.
489 * If @use_font is %TRUE, the font name will be written using the selected font.
494 gtk_font_button_set_use_font (GtkFontButton *font_button,
497 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
499 use_font = (use_font != FALSE);
501 if (font_button->priv->use_font != use_font)
503 font_button->priv->use_font = use_font;
506 gtk_font_button_label_use_font (font_button);
508 gtk_widget_set_style (font_button->priv->font_label, NULL);
510 g_object_notify (G_OBJECT (font_button), "use_font");
516 * gtk_font_button_get_use_size:
517 * @font_button: a #GtkFontButton
519 * Returns whether the selected size is used in the label.
521 * Returns: whether the selected size is used in the label.
526 gtk_font_button_get_use_size (GtkFontButton *font_button)
528 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
530 return font_button->priv->use_size;
534 * gtk_font_button_set_use_size:
535 * @font_button: a #GtkFontButton
536 * @use_size: If %TRUE, font name will be written using the selected size.
538 * If @use_size is %TRUE, the font name will be written using the selected size.
543 gtk_font_button_set_use_size (GtkFontButton *font_button,
546 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
548 use_size = (use_size != FALSE);
549 if (font_button->priv->use_size != use_size)
551 font_button->priv->use_size = use_size;
553 if (font_button->priv->use_font)
554 gtk_font_button_label_use_font (font_button);
556 g_object_notify (G_OBJECT (font_button), "use_size");
561 * gtk_font_button_get_show_style:
562 * @font_button: a #GtkFontButton
564 * Returns whether the name of the font style will be shown in the label.
566 * Return value: whether the font style will be shown in the label.
571 gtk_font_button_get_show_style (GtkFontButton *font_button)
573 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
575 return font_button->priv->show_style;
579 * gtk_font_button_set_show_style:
580 * @font_button: a #GtkFontButton
581 * @show_style: %TRUE if font style should be displayed in label.
583 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
588 gtk_font_button_set_show_style (GtkFontButton *font_button,
591 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
593 show_style = (show_style != FALSE);
594 if (font_button->priv->show_style != show_style)
596 font_button->priv->show_style = show_style;
598 gtk_font_button_update_font_info (font_button);
600 g_object_notify (G_OBJECT (font_button), "show_style");
606 * gtk_font_button_get_show_size:
607 * @font_button: a #GtkFontButton
609 * Returns whether the font size will be shown in the label.
611 * Return value: whether the font size will be shown in the label.
616 gtk_font_button_get_show_size (GtkFontButton *font_button)
618 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
620 return font_button->priv->show_size;
624 * gtk_font_button_set_show_size:
625 * @font_button: a #GtkFontButton
626 * @show_size: %TRUE if font size should be displayed in dialog.
628 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
633 gtk_font_button_set_show_size (GtkFontButton *font_button,
636 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
638 show_size = (show_size != FALSE);
640 if (font_button->priv->show_size != show_size)
642 font_button->priv->show_size = show_size;
644 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
645 font_button->priv->inside = gtk_font_button_create_inside (font_button);
646 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
648 gtk_font_button_update_font_info (font_button);
650 g_object_notify (G_OBJECT (font_button), "show_size");
656 * gtk_font_button_get_font_name:
657 * @font_button: a #GtkFontButton
659 * Retrieves the name of the currently selected font.
661 * Returns: an internal copy of the font name which must not be freed.
665 G_CONST_RETURN gchar *
666 gtk_font_button_get_font_name (GtkFontButton *font_button)
668 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
670 return font_button->priv->fontname;
674 * gtk_font_button_set_font_name:
675 * @font_button: a #GtkFontButton
676 * @fontname: Name of font to display in font selection dialog
678 * Sets or updates the currently-displayed font in font picker dialog.
680 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
681 * font selection dialog exists, otherwise %FALSE.
686 gtk_font_button_set_font_name (GtkFontButton *font_button,
687 const gchar *fontname)
692 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
693 g_return_val_if_fail (fontname != NULL, FALSE);
695 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
697 old_fontname = font_button->priv->fontname;
698 font_button->priv->fontname = g_strdup (fontname);
699 g_free (old_fontname);
702 gtk_font_button_update_font_info (font_button);
704 if (font_button->priv->font_dialog)
705 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
706 font_button->priv->fontname);
710 g_object_notify (G_OBJECT (font_button), "font_name");
716 gtk_font_button_clicked (GtkButton *button)
718 GtkFontSelectionDialog *font_dialog;
719 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
721 if (!font_button->priv->font_dialog)
725 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
727 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
729 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
732 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
734 /* If there is a grabbed window, set new dialog as modal */
735 if (gtk_grab_get_current ())
736 gtk_window_set_modal (GTK_WINDOW (font_dialog), TRUE);
738 g_signal_connect (font_dialog->ok_button, "clicked",
739 G_CALLBACK (dialog_ok_clicked), font_button);
740 g_signal_connect (font_dialog->cancel_button, "clicked",
741 G_CALLBACK (dialog_cancel_clicked), font_button);
742 g_signal_connect (font_dialog, "destroy",
743 G_CALLBACK (dialog_destroy), font_button);
746 if (!GTK_WIDGET_VISIBLE (font_button->priv->font_dialog))
748 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
750 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
754 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
758 dialog_ok_clicked (GtkWidget *widget,
761 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
763 gtk_widget_hide (font_button->priv->font_dialog);
765 g_free (font_button->priv->fontname);
766 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
769 gtk_font_button_update_font_info (font_button);
771 g_object_notify (G_OBJECT (font_button), "font_name");
773 /* Emit font_set signal */
774 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
779 dialog_cancel_clicked (GtkWidget *widget,
782 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
784 gtk_widget_hide (font_button->priv->font_dialog);
788 dialog_destroy (GtkWidget *widget,
791 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
793 /* Dialog will get destroyed so reference is not valid now */
794 font_button->priv->font_dialog = NULL;
798 gtk_font_button_create_inside (GtkFontButton *font_button)
802 gtk_widget_push_composite_child ();
804 widget = gtk_hbox_new (FALSE, 0);
806 font_button->priv->font_label = gtk_label_new (_("Font"));
808 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
809 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
811 if (font_button->priv->show_size)
813 gtk_box_pack_start (GTK_BOX (widget), gtk_vseparator_new (), FALSE, FALSE, 0);
814 font_button->priv->size_label = gtk_label_new ("14");
815 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
818 gtk_widget_show_all (widget);
820 gtk_widget_pop_composite_child ();
826 gtk_font_button_label_use_font (GtkFontButton *font_button)
828 PangoFontDescription *desc;
830 if (!font_button->priv->use_font)
833 desc = pango_font_description_from_string (font_button->priv->fontname);
835 if (!font_button->priv->use_size)
836 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
838 gtk_widget_modify_font (font_button->priv->font_label, desc);
842 font_description_style_equal (const PangoFontDescription *a,
843 const PangoFontDescription *b)
845 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
846 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
847 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
848 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
852 gtk_font_button_update_font_info (GtkFontButton *font_button)
854 PangoFontDescription *desc;
859 desc = pango_font_description_from_string (font_button->priv->fontname);
860 family = pango_font_description_get_family (desc);
863 /* This gives the wrong names, e.g. Italic when the font selection
864 * dialog displayed Oblique.
866 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
867 style = pango_font_description_to_string (desc);
868 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
872 if (font_button->priv->show_style)
874 PangoFontFamily **families;
875 PangoFontFace **faces;
876 gint n_families, n_faces, i;
880 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
881 &families, &n_families);
882 for (i = 0; i < n_families; i++)
884 const gchar *name = pango_font_family_get_name (families[i]);
886 if (!g_ascii_strcasecmp (name, family))
888 pango_font_family_list_faces (families[i], &faces, &n_faces);
894 for (i = 0; i < n_faces; i++)
896 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
898 if (font_description_style_equal (tmp_desc, desc))
900 style = g_strdup (pango_font_face_get_face_name (faces[i]));
901 pango_font_description_free (tmp_desc);
905 pango_font_description_free (tmp_desc);
910 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
911 family_style = g_strdup (family);
913 family_style = g_strdup_printf ("%s %s", family, style);
915 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
918 g_free (family_style);
920 if (font_button->priv->show_size)
922 gchar *size = g_strdup_printf ("%d",
923 pango_font_description_get_size (desc) / PANGO_SCALE);
925 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
930 gtk_font_button_label_use_font (font_button);
932 pango_font_description_free (desc);