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"
42 #include "gtkprivate.h"
49 #define GTK_FONT_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_FONT_BUTTON, GtkFontButtonPrivate))
51 struct _GtkFontButtonPrivate
62 GtkWidget *font_dialog;
64 GtkWidget *font_label;
65 GtkWidget *size_label;
87 static void gtk_font_button_init (GtkFontButton *font_button);
88 static void gtk_font_button_class_init (GtkFontButtonClass *klass);
89 static void gtk_font_button_finalize (GObject *object);
90 static void gtk_font_button_get_property (GObject *object,
94 static void gtk_font_button_set_property (GObject *object,
99 static void gtk_font_button_clicked (GtkButton *button);
101 /* Dialog response functions */
102 static void dialog_ok_clicked (GtkWidget *widget,
104 static void dialog_cancel_clicked (GtkWidget *widget,
106 static void dialog_destroy (GtkWidget *widget,
109 /* Auxiliary functions */
110 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
111 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
112 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
114 static gpointer parent_class = NULL;
115 static guint font_button_signals[LAST_SIGNAL] = { 0 };
118 gtk_font_button_get_type (void)
120 static GType font_button_type = 0;
122 if (!font_button_type)
124 static const GTypeInfo font_button_info =
126 sizeof (GtkFontButtonClass),
127 NULL, /* base_init */
128 NULL, /* base_finalize */
129 (GClassInitFunc) gtk_font_button_class_init,
130 NULL, /* class_finalize */
131 NULL, /* class_data */
132 sizeof (GtkFontButton),
134 (GInstanceInitFunc) gtk_font_button_init,
138 g_type_register_static (GTK_TYPE_BUTTON, "GtkFontButton",
139 &font_button_info, 0);
142 return font_button_type;
147 gtk_font_button_class_init (GtkFontButtonClass *klass)
149 GtkObjectClass *object_class;
150 GObjectClass *gobject_class;
151 GtkButtonClass *button_class;
153 object_class = (GtkObjectClass *) klass;
154 gobject_class = (GObjectClass *) klass;
155 button_class = (GtkButtonClass *) klass;
157 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->finalize = gtk_font_button_finalize;
160 gobject_class->set_property = gtk_font_button_set_property;
161 gobject_class->get_property = gtk_font_button_get_property;
163 button_class->clicked = gtk_font_button_clicked;
165 klass->font_set = NULL;
168 * GtkFontButton:title:
170 * The title of the font selection dialog.
174 g_object_class_install_property (gobject_class,
176 g_param_spec_string ("title",
178 P_("The title of the font selection dialog"),
180 (GTK_PARAM_READABLE |
181 GTK_PARAM_WRITABLE)));
184 * GtkFontButton:font-name:
186 * The name of the currently selected font.
190 g_object_class_install_property (gobject_class,
192 g_param_spec_string ("font-name",
194 P_("The name of the selected font"),
196 (GTK_PARAM_READABLE |
197 GTK_PARAM_WRITABLE)));
200 * GtkFontButton:use-font:
202 * If this property is set to %TRUE, the label will be drawn in the selected font.
206 g_object_class_install_property (gobject_class,
208 g_param_spec_boolean ("use-font",
209 P_("Use font in label"),
210 P_("Whether the label is drawn in the selected font"),
212 GTK_PARAM_READWRITE));
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"),
227 GTK_PARAM_READWRITE));
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 P_("Whether the selected font style is shown in the label"),
243 GTK_PARAM_READWRITE));
245 * GtkFontButton:show-size:
247 * If this property is set to %TRUE, the selected font size will be shown in the label. For
248 * a more WYSIWIG way to show the selected size, see the ::use-size property.
252 g_object_class_install_property (gobject_class,
254 g_param_spec_boolean ("show-size",
256 P_("Whether selected font size is shown in the label"),
258 GTK_PARAM_READWRITE));
261 * GtkFontButton::font-set:
262 * @widget: the object which received the signal.
264 * The ::font-set signal is emitted when the user selects a font. When handling this signal,
265 * use gtk_font_button_get_font_name() to find out which font was just selected.
269 font_button_signals[FONT_SET] = g_signal_new ("font-set",
270 G_TYPE_FROM_CLASS (gobject_class),
272 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
274 g_cclosure_marshal_VOID__VOID,
277 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
281 gtk_font_button_init (GtkFontButton *font_button)
283 font_button->priv = GTK_FONT_BUTTON_GET_PRIVATE (font_button);
285 /* Initialize fields */
286 font_button->priv->fontname = g_strdup (_("Sans 12"));
287 font_button->priv->use_font = FALSE;
288 font_button->priv->use_size = FALSE;
289 font_button->priv->show_style = TRUE;
290 font_button->priv->show_size = TRUE;
291 font_button->priv->font_dialog = NULL;
292 font_button->priv->title = g_strdup (_("Pick a Font"));
294 font_button->priv->inside = gtk_font_button_create_inside (font_button);
295 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
297 gtk_font_button_update_font_info (font_button);
302 gtk_font_button_finalize (GObject *object)
304 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
306 if (font_button->priv->font_dialog != NULL)
307 gtk_widget_destroy (font_button->priv->font_dialog);
308 font_button->priv->font_dialog = NULL;
310 g_free (font_button->priv->fontname);
311 font_button->priv->fontname = NULL;
313 g_free (font_button->priv->title);
314 font_button->priv->title = NULL;
316 G_OBJECT_CLASS (parent_class)->finalize (object);
320 gtk_font_button_set_property (GObject *object,
325 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
330 gtk_font_button_set_title (font_button, g_value_get_string (value));
333 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
336 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
339 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
341 case PROP_SHOW_STYLE:
342 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
345 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
354 gtk_font_button_get_property (GObject *object,
359 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
364 g_value_set_string (value, gtk_font_button_get_title (font_button));
367 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
370 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
373 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
375 case PROP_SHOW_STYLE:
376 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
379 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
389 * gtk_font_button_new:
391 * Creates a new font picker widget.
393 * Returns: a new font picker widget.
398 gtk_font_button_new (void)
400 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
404 * gtk_font_button_new_with_font:
405 * @fontname: Name of font to display in font selection dialog
407 * Creates a new font picker widget.
409 * Returns: a new font picker widget.
414 gtk_font_button_new_with_font (const gchar *fontname)
416 return g_object_new (GTK_TYPE_FONT_BUTTON, "font_name", fontname, NULL);
420 * gtk_font_button_set_title:
421 * @font_button: a #GtkFontButton
422 * @title: a string containing the font selection dialog title
424 * Sets the title for the font selection dialog.
429 gtk_font_button_set_title (GtkFontButton *font_button,
433 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
435 old_title = font_button->priv->title;
436 font_button->priv->title = g_strdup (title);
439 if (font_button->priv->font_dialog)
440 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
441 font_button->priv->title);
443 g_object_notify (G_OBJECT (font_button), "title");
447 * gtk_font_button_get_title:
448 * @font_button: a #GtkFontButton
450 * Retrieves the title of the font selection dialog.
452 * Returns: an internal copy of the title string which must not be freed.
456 G_CONST_RETURN gchar*
457 gtk_font_button_get_title (GtkFontButton *font_button)
459 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
461 return font_button->priv->title;
465 * gtk_font_button_get_use_font:
466 * @font_button: a #GtkFontButton
468 * Returns whether the selected font is used in the label.
470 * Returns: whether the selected font is used in the label.
475 gtk_font_button_get_use_font (GtkFontButton *font_button)
477 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
479 return font_button->priv->use_font;
483 * gtk_font_button_set_use_font:
484 * @font_button: a #GtkFontButton
485 * @use_font: If %TRUE, font name will be written using font chosen.
487 * If @use_font is %TRUE, the font name will be written using the selected font.
492 gtk_font_button_set_use_font (GtkFontButton *font_button,
495 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
497 use_font = (use_font != FALSE);
499 if (font_button->priv->use_font != use_font)
501 font_button->priv->use_font = use_font;
504 gtk_font_button_label_use_font (font_button);
506 gtk_widget_set_style (font_button->priv->font_label, NULL);
508 g_object_notify (G_OBJECT (font_button), "use-font");
514 * gtk_font_button_get_use_size:
515 * @font_button: a #GtkFontButton
517 * Returns whether the selected size is used in the label.
519 * Returns: whether the selected size is used in the label.
524 gtk_font_button_get_use_size (GtkFontButton *font_button)
526 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
528 return font_button->priv->use_size;
532 * gtk_font_button_set_use_size:
533 * @font_button: a #GtkFontButton
534 * @use_size: If %TRUE, font name will be written using the selected size.
536 * If @use_size is %TRUE, the font name will be written using the selected size.
541 gtk_font_button_set_use_size (GtkFontButton *font_button,
544 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
546 use_size = (use_size != FALSE);
547 if (font_button->priv->use_size != use_size)
549 font_button->priv->use_size = use_size;
551 if (font_button->priv->use_font)
552 gtk_font_button_label_use_font (font_button);
554 g_object_notify (G_OBJECT (font_button), "use-size");
559 * gtk_font_button_get_show_style:
560 * @font_button: a #GtkFontButton
562 * Returns whether the name of the font style will be shown in the label.
564 * Return value: whether the font style will be shown in the label.
569 gtk_font_button_get_show_style (GtkFontButton *font_button)
571 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
573 return font_button->priv->show_style;
577 * gtk_font_button_set_show_style:
578 * @font_button: a #GtkFontButton
579 * @show_style: %TRUE if font style should be displayed in label.
581 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
586 gtk_font_button_set_show_style (GtkFontButton *font_button,
589 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
591 show_style = (show_style != FALSE);
592 if (font_button->priv->show_style != show_style)
594 font_button->priv->show_style = show_style;
596 gtk_font_button_update_font_info (font_button);
598 g_object_notify (G_OBJECT (font_button), "show-style");
604 * gtk_font_button_get_show_size:
605 * @font_button: a #GtkFontButton
607 * Returns whether the font size will be shown in the label.
609 * Return value: whether the font size will be shown in the label.
614 gtk_font_button_get_show_size (GtkFontButton *font_button)
616 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
618 return font_button->priv->show_size;
622 * gtk_font_button_set_show_size:
623 * @font_button: a #GtkFontButton
624 * @show_size: %TRUE if font size should be displayed in dialog.
626 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
631 gtk_font_button_set_show_size (GtkFontButton *font_button,
634 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
636 show_size = (show_size != FALSE);
638 if (font_button->priv->show_size != show_size)
640 font_button->priv->show_size = show_size;
642 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
643 font_button->priv->inside = gtk_font_button_create_inside (font_button);
644 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
646 gtk_font_button_update_font_info (font_button);
648 g_object_notify (G_OBJECT (font_button), "show-size");
654 * gtk_font_button_get_font_name:
655 * @font_button: a #GtkFontButton
657 * Retrieves the name of the currently selected font.
659 * Returns: an internal copy of the font name which must not be freed.
663 G_CONST_RETURN gchar *
664 gtk_font_button_get_font_name (GtkFontButton *font_button)
666 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
668 return font_button->priv->fontname;
672 * gtk_font_button_set_font_name:
673 * @font_button: a #GtkFontButton
674 * @fontname: Name of font to display in font selection dialog
676 * Sets or updates the currently-displayed font in font picker dialog.
678 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
679 * font selection dialog exists, otherwise %FALSE.
684 gtk_font_button_set_font_name (GtkFontButton *font_button,
685 const gchar *fontname)
690 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
691 g_return_val_if_fail (fontname != NULL, FALSE);
693 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
695 old_fontname = font_button->priv->fontname;
696 font_button->priv->fontname = g_strdup (fontname);
697 g_free (old_fontname);
700 gtk_font_button_update_font_info (font_button);
702 if (font_button->priv->font_dialog)
703 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
704 font_button->priv->fontname);
708 g_object_notify (G_OBJECT (font_button), "font-name");
714 gtk_font_button_clicked (GtkButton *button)
716 GtkFontSelectionDialog *font_dialog;
717 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
719 if (!font_button->priv->font_dialog)
723 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
725 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
727 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
730 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
732 /* If there is a grabbed window, set new dialog as modal */
733 if (gtk_grab_get_current ())
734 gtk_window_set_modal (GTK_WINDOW (font_dialog), TRUE);
736 g_signal_connect (font_dialog->ok_button, "clicked",
737 G_CALLBACK (dialog_ok_clicked), font_button);
738 g_signal_connect (font_dialog->cancel_button, "clicked",
739 G_CALLBACK (dialog_cancel_clicked), font_button);
740 g_signal_connect (font_dialog, "destroy",
741 G_CALLBACK (dialog_destroy), font_button);
744 if (!GTK_WIDGET_VISIBLE (font_button->priv->font_dialog))
746 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
748 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
752 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
756 dialog_ok_clicked (GtkWidget *widget,
759 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
761 gtk_widget_hide (font_button->priv->font_dialog);
763 g_free (font_button->priv->fontname);
764 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
767 gtk_font_button_update_font_info (font_button);
769 g_object_notify (G_OBJECT (font_button), "font-name");
771 /* Emit font_set signal */
772 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
777 dialog_cancel_clicked (GtkWidget *widget,
780 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
782 gtk_widget_hide (font_button->priv->font_dialog);
786 dialog_destroy (GtkWidget *widget,
789 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
791 /* Dialog will get destroyed so reference is not valid now */
792 font_button->priv->font_dialog = NULL;
796 gtk_font_button_create_inside (GtkFontButton *font_button)
800 gtk_widget_push_composite_child ();
802 widget = gtk_hbox_new (FALSE, 0);
804 font_button->priv->font_label = gtk_label_new (_("Font"));
806 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
807 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
809 if (font_button->priv->show_size)
811 gtk_box_pack_start (GTK_BOX (widget), gtk_vseparator_new (), FALSE, FALSE, 0);
812 font_button->priv->size_label = gtk_label_new ("14");
813 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
816 gtk_widget_show_all (widget);
818 gtk_widget_pop_composite_child ();
824 gtk_font_button_label_use_font (GtkFontButton *font_button)
826 PangoFontDescription *desc;
828 if (!font_button->priv->use_font)
831 desc = pango_font_description_from_string (font_button->priv->fontname);
833 if (!font_button->priv->use_size)
834 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
836 gtk_widget_modify_font (font_button->priv->font_label, desc);
838 pango_font_description_free (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;
879 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
880 &families, &n_families);
883 for (i = 0; i < n_families; i++)
885 const gchar *name = pango_font_family_get_name (families[i]);
887 if (!g_ascii_strcasecmp (name, family))
889 pango_font_family_list_faces (families[i], &faces, &n_faces);
895 for (i = 0; i < n_faces; i++)
897 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
899 if (font_description_style_equal (tmp_desc, desc))
901 style = g_strdup (pango_font_face_get_face_name (faces[i]));
902 pango_font_description_free (tmp_desc);
906 pango_font_description_free (tmp_desc);
911 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
912 family_style = g_strdup (family);
914 family_style = g_strdup_printf ("%s %s", family, style);
916 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
919 g_free (family_style);
921 if (font_button->priv->show_size)
923 gchar *size = g_strdup_printf ("%d",
924 pango_font_description_get_size (desc) / PANGO_SCALE);
926 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
931 gtk_font_button_label_use_font (font_button);
933 pango_font_description_free (desc);
936 #define __GTK_FONT_BUTTON_C__
937 #include "gtkaliasdef.c"