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/.
32 #include "gtkfontbutton.h"
34 #include "gtksignal.h"
36 #include "gtkalignment.h"
39 #include "gtkvseparator.h"
40 #include "gtkfontsel.h"
42 #include "gtkmarshalers.h"
48 #define GTK_FONT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButtonPrivate))
50 struct _GtkFontButtonPrivate
61 GtkWidget *font_dialog;
63 GtkWidget *font_label;
64 GtkWidget *size_label;
86 static void gtk_font_button_init (GtkFontButton *font_button);
87 static void gtk_font_button_class_init (GtkFontButtonClass *klass);
88 static void gtk_font_button_finalize (GObject *object);
89 static void gtk_font_button_get_property (GObject *object,
93 static void gtk_font_button_set_property (GObject *object,
98 static void gtk_font_button_clicked (GtkButton *button);
100 /* Dialog response functions */
101 static void dialog_ok_clicked (GtkWidget *widget,
103 static void dialog_cancel_clicked (GtkWidget *widget,
105 static void dialog_destroy (GtkWidget *widget,
108 /* Auxiliary functions */
109 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
110 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
111 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
113 static gpointer parent_class = NULL;
114 static guint font_button_signals[LAST_SIGNAL] = { 0 };
117 gtk_font_button_get_type (void)
119 static GType font_button_type = 0;
121 if (!font_button_type)
123 static const GTypeInfo font_button_info =
125 sizeof (GtkFontButtonClass),
126 NULL, /* base_init */
127 NULL, /* base_finalize */
128 (GClassInitFunc) gtk_font_button_class_init,
129 NULL, /* class_finalize */
130 NULL, /* class_data */
131 sizeof (GtkFontButton),
133 (GInstanceInitFunc) gtk_font_button_init,
137 g_type_register_static (GTK_TYPE_BUTTON, "GtkFontButton",
138 &font_button_info, 0);
141 return font_button_type;
146 gtk_font_button_class_init (GtkFontButtonClass *klass)
148 GtkObjectClass *object_class;
149 GObjectClass *gobject_class;
150 GtkButtonClass *button_class;
152 object_class = (GtkObjectClass *) klass;
153 gobject_class = (GObjectClass *) klass;
154 button_class = (GtkButtonClass *) klass;
156 parent_class = g_type_class_peek_parent (klass);
158 gobject_class->finalize = gtk_font_button_finalize;
159 gobject_class->set_property = gtk_font_button_set_property;
160 gobject_class->get_property = gtk_font_button_get_property;
162 button_class->clicked = gtk_font_button_clicked;
164 klass->font_set = NULL;
167 * GtkFontButton:title:
169 * The title of the font selection dialog.
173 g_object_class_install_property (gobject_class,
175 g_param_spec_string ("title",
177 P_("The title of the font selection dialog"),
183 * GtkFontButton:font-name:
185 * The name of the currently selected font.
189 g_object_class_install_property (gobject_class,
191 g_param_spec_string ("font_name",
193 P_("The name of the selected font"),
199 * GtkFontButton:use-font:
201 * If this property is set to %TRUE, the label will be drawn in the selected font.
205 g_object_class_install_property (gobject_class,
207 g_param_spec_boolean ("use_font",
208 P_("Use font in label"),
209 P_("Whether the label is drawn in the selected font"),
215 * GtkFontButton:use-size:
217 * If this property is set to %TRUE, the label will be drawn with the selected font size.
221 g_object_class_install_property (gobject_class,
223 g_param_spec_boolean ("use_size",
224 P_("Use size in label"),
225 P_("Whether the label is drawn with the selected font size"),
231 * GtkFontButton:show-style:
233 * If this property is set to %TRUE, the name of the selected font style will be shown in the label. For
234 * a more WYSIWIG way to show the selected style, see the ::use-font property.
238 g_object_class_install_property (gobject_class,
240 g_param_spec_boolean ("show_style",
242 P_("Whether the selected font style is shown in the label"),
247 * GtkFontButton:show-size:
249 * If this property is set to %TRUE, the selected font size will be shown in the label. For
250 * a more WYSIWIG way to show the selected size, see the ::use-size property.
254 g_object_class_install_property (gobject_class,
256 g_param_spec_boolean ("show_size",
258 P_("Whether selected font size is shown in the label"),
264 * GtkFontButton::font-set:
265 * @widget: the object which received the signal.
267 * The ::font-set signal is emitted when the user selects a font. When handling this signal,
268 * use gtk_font_button_get_font_name() to find out which font was just selected.
272 font_button_signals[FONT_SET] = g_signal_new ("font_set",
273 G_TYPE_FROM_CLASS (gobject_class),
275 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
277 g_cclosure_marshal_VOID__VOID,
280 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
284 gtk_font_button_init (GtkFontButton *font_button)
286 font_button->priv = GTK_FONT_BUTTON_GET_PRIVATE (font_button);
288 /* Initialize fields */
289 font_button->priv->fontname = g_strdup (_("Sans 12"));
290 font_button->priv->use_font = FALSE;
291 font_button->priv->use_size = FALSE;
292 font_button->priv->show_style = TRUE;
293 font_button->priv->show_size = TRUE;
294 font_button->priv->font_dialog = NULL;
295 font_button->priv->title = g_strdup (_("Pick a Font"));
297 font_button->priv->inside = gtk_font_button_create_inside (font_button);
298 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
300 gtk_font_button_update_font_info (font_button);
305 gtk_font_button_finalize (GObject *object)
307 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
309 if (font_button->priv->font_dialog != NULL)
310 gtk_widget_destroy (font_button->priv->font_dialog);
311 font_button->priv->font_dialog = NULL;
313 g_free (font_button->priv->fontname);
314 font_button->priv->fontname = NULL;
316 g_free (font_button->priv->title);
317 font_button->priv->title = NULL;
319 G_OBJECT_CLASS (parent_class)->finalize (object);
323 gtk_font_button_set_property (GObject *object,
328 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
333 gtk_font_button_set_title (font_button, g_value_get_string (value));
336 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
339 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
342 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
344 case PROP_SHOW_STYLE:
345 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
348 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
357 gtk_font_button_get_property (GObject *object,
362 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
367 g_value_set_string (value, gtk_font_button_get_title (font_button));
370 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
373 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
376 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
378 case PROP_SHOW_STYLE:
379 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
382 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
392 * gtk_font_button_new:
394 * Creates a new font picker widget.
396 * Returns: a new font picker widget.
401 gtk_font_button_new (void)
403 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
407 * gtk_font_button_new_with_font:
408 * @fontname: Name of font to display in font selection dialog
410 * Creates a new font picker widget.
412 * Returns: a new font picker widget.
417 gtk_font_button_new_with_font (const gchar *fontname)
419 return g_object_new (GTK_TYPE_FONT_BUTTON, "font_name", fontname, NULL);
423 * gtk_font_button_set_title:
424 * @font_button: a #GtkFontButton
425 * @title: a string containing the font selection dialog title
427 * Sets the title for the font selection dialog.
432 gtk_font_button_set_title (GtkFontButton *font_button,
436 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
438 old_title = font_button->priv->title;
439 font_button->priv->title = g_strdup (title);
442 if (font_button->priv->font_dialog)
443 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
444 font_button->priv->title);
446 g_object_notify (G_OBJECT (font_button), "title");
450 * gtk_font_button_get_title:
451 * @font_button: a #GtkFontButton
453 * Retrieves the title of the font selection dialog.
455 * Returns: an internal copy of the title string which must not be freed.
459 G_CONST_RETURN gchar*
460 gtk_font_button_get_title (GtkFontButton *font_button)
462 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
464 return font_button->priv->title;
468 * gtk_font_button_get_use_font:
469 * @font_button: a #GtkFontButton
471 * Returns whether the selected font is used in the label.
473 * Returns: whether the selected font is used in the label.
478 gtk_font_button_get_use_font (GtkFontButton *font_button)
480 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
482 return font_button->priv->use_font;
486 * gtk_font_button_set_use_font:
487 * @font_button: a #GtkFontButton
488 * @use_font: If %TRUE, font name will be written using font chosen.
490 * If @use_font is %TRUE, the font name will be written using the selected font.
495 gtk_font_button_set_use_font (GtkFontButton *font_button,
498 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
500 use_font = (use_font != FALSE);
502 if (font_button->priv->use_font != use_font)
504 font_button->priv->use_font = use_font;
507 gtk_font_button_label_use_font (font_button);
509 gtk_widget_set_style (font_button->priv->font_label, NULL);
511 g_object_notify (G_OBJECT (font_button), "use_font");
517 * gtk_font_button_get_use_size:
518 * @font_button: a #GtkFontButton
520 * Returns whether the selected size is used in the label.
522 * Returns: whether the selected size is used in the label.
527 gtk_font_button_get_use_size (GtkFontButton *font_button)
529 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
531 return font_button->priv->use_size;
535 * gtk_font_button_set_use_size:
536 * @font_button: a #GtkFontButton
537 * @use_size: If %TRUE, font name will be written using the selected size.
539 * If @use_size is %TRUE, the font name will be written using the selected size.
544 gtk_font_button_set_use_size (GtkFontButton *font_button,
547 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
549 use_size = (use_size != FALSE);
550 if (font_button->priv->use_size != use_size)
552 font_button->priv->use_size = use_size;
554 if (font_button->priv->use_font)
555 gtk_font_button_label_use_font (font_button);
557 g_object_notify (G_OBJECT (font_button), "use_size");
562 * gtk_font_button_get_show_style:
563 * @font_button: a #GtkFontButton
565 * Returns whether the name of the font style will be shown in the label.
567 * Return value: whether the font style will be shown in the label.
572 gtk_font_button_get_show_style (GtkFontButton *font_button)
574 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
576 return font_button->priv->show_style;
580 * gtk_font_button_set_show_style:
581 * @font_button: a #GtkFontButton
582 * @show_style: %TRUE if font style should be displayed in label.
584 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
589 gtk_font_button_set_show_style (GtkFontButton *font_button,
592 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
594 show_style = (show_style != FALSE);
595 if (font_button->priv->show_style != show_style)
597 font_button->priv->show_style = show_style;
599 gtk_font_button_update_font_info (font_button);
601 g_object_notify (G_OBJECT (font_button), "show_style");
607 * gtk_font_button_get_show_size:
608 * @font_button: a #GtkFontButton
610 * Returns whether the font size will be shown in the label.
612 * Return value: whether the font size will be shown in the label.
617 gtk_font_button_get_show_size (GtkFontButton *font_button)
619 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
621 return font_button->priv->show_size;
625 * gtk_font_button_set_show_size:
626 * @font_button: a #GtkFontButton
627 * @show_size: %TRUE if font size should be displayed in dialog.
629 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
634 gtk_font_button_set_show_size (GtkFontButton *font_button,
637 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
639 show_size = (show_size != FALSE);
641 if (font_button->priv->show_size != show_size)
643 font_button->priv->show_size = show_size;
645 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
646 font_button->priv->inside = gtk_font_button_create_inside (font_button);
647 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
649 gtk_font_button_update_font_info (font_button);
651 g_object_notify (G_OBJECT (font_button), "show_size");
657 * gtk_font_button_get_font_name:
658 * @font_button: a #GtkFontButton
660 * Retrieves the name of the currently selected font.
662 * Returns: an internal copy of the font name which must not be freed.
666 G_CONST_RETURN gchar *
667 gtk_font_button_get_font_name (GtkFontButton *font_button)
669 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
671 return font_button->priv->fontname;
675 * gtk_font_button_set_font_name:
676 * @font_button: a #GtkFontButton
677 * @fontname: Name of font to display in font selection dialog
679 * Sets or updates the currently-displayed font in font picker dialog.
681 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
682 * font selection dialog exists, otherwise %FALSE.
687 gtk_font_button_set_font_name (GtkFontButton *font_button,
688 const gchar *fontname)
693 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
694 g_return_val_if_fail (fontname != NULL, FALSE);
696 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
698 old_fontname = font_button->priv->fontname;
699 font_button->priv->fontname = g_strdup (fontname);
700 g_free (old_fontname);
703 gtk_font_button_update_font_info (font_button);
705 if (font_button->priv->font_dialog)
706 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
707 font_button->priv->fontname);
711 g_object_notify (G_OBJECT (font_button), "font_name");
717 gtk_font_button_clicked (GtkButton *button)
719 GtkFontSelectionDialog *font_dialog;
720 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
722 if (!font_button->priv->font_dialog)
726 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
728 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
730 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
733 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
735 /* If there is a grabbed window, set new dialog as modal */
736 if (gtk_grab_get_current ())
737 gtk_window_set_modal (GTK_WINDOW (font_dialog), TRUE);
739 g_signal_connect (font_dialog->ok_button, "clicked",
740 G_CALLBACK (dialog_ok_clicked), font_button);
741 g_signal_connect (font_dialog->cancel_button, "clicked",
742 G_CALLBACK (dialog_cancel_clicked), font_button);
743 g_signal_connect (font_dialog, "destroy",
744 G_CALLBACK (dialog_destroy), font_button);
747 if (!GTK_WIDGET_VISIBLE (font_button->priv->font_dialog))
749 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
751 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
755 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
759 dialog_ok_clicked (GtkWidget *widget,
762 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
764 gtk_widget_hide (font_button->priv->font_dialog);
766 g_free (font_button->priv->fontname);
767 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
770 gtk_font_button_update_font_info (font_button);
772 g_object_notify (G_OBJECT (font_button), "font_name");
774 /* Emit font_set signal */
775 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
780 dialog_cancel_clicked (GtkWidget *widget,
783 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
785 gtk_widget_hide (font_button->priv->font_dialog);
789 dialog_destroy (GtkWidget *widget,
792 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
794 /* Dialog will get destroyed so reference is not valid now */
795 font_button->priv->font_dialog = NULL;
799 gtk_font_button_create_inside (GtkFontButton *font_button)
803 gtk_widget_push_composite_child ();
805 widget = gtk_hbox_new (FALSE, 0);
807 font_button->priv->font_label = gtk_label_new (_("Font"));
809 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
810 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
812 if (font_button->priv->show_size)
814 gtk_box_pack_start (GTK_BOX (widget), gtk_vseparator_new (), FALSE, FALSE, 0);
815 font_button->priv->size_label = gtk_label_new ("14");
816 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
819 gtk_widget_show_all (widget);
821 gtk_widget_pop_composite_child ();
827 gtk_font_button_label_use_font (GtkFontButton *font_button)
829 PangoFontDescription *desc;
831 if (!font_button->priv->use_font)
834 desc = pango_font_description_from_string (font_button->priv->fontname);
836 if (!font_button->priv->use_size)
837 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
839 gtk_widget_modify_font (font_button->priv->font_label, desc);
841 pango_font_description_free (desc);
845 font_description_style_equal (const PangoFontDescription *a,
846 const PangoFontDescription *b)
848 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
849 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
850 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
851 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
855 gtk_font_button_update_font_info (GtkFontButton *font_button)
857 PangoFontDescription *desc;
862 desc = pango_font_description_from_string (font_button->priv->fontname);
863 family = pango_font_description_get_family (desc);
866 /* This gives the wrong names, e.g. Italic when the font selection
867 * dialog displayed Oblique.
869 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
870 style = pango_font_description_to_string (desc);
871 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
875 if (font_button->priv->show_style)
877 PangoFontFamily **families;
878 PangoFontFace **faces;
879 gint n_families, n_faces, i;
882 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
883 &families, &n_families);
886 for (i = 0; i < n_families; i++)
888 const gchar *name = pango_font_family_get_name (families[i]);
890 if (!g_ascii_strcasecmp (name, family))
892 pango_font_family_list_faces (families[i], &faces, &n_faces);
898 for (i = 0; i < n_faces; i++)
900 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
902 if (font_description_style_equal (tmp_desc, desc))
904 style = g_strdup (pango_font_face_get_face_name (faces[i]));
905 pango_font_description_free (tmp_desc);
909 pango_font_description_free (tmp_desc);
914 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
915 family_style = g_strdup (family);
917 family_style = g_strdup_printf ("%s %s", family, style);
919 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
922 g_free (family_style);
924 if (font_button->priv->show_size)
926 gchar *size = g_strdup_printf ("%d",
927 pango_font_description_get_size (desc) / PANGO_SCALE);
929 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
934 gtk_font_button_label_use_font (font_button);
936 pango_font_description_free (desc);