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"
48 struct _GtkFontButtonPrivate
59 GtkWidget *font_dialog;
61 GtkWidget *font_label;
62 GtkWidget *size_label;
84 static void gtk_font_button_finalize (GObject *object);
85 static void gtk_font_button_get_property (GObject *object,
89 static void gtk_font_button_set_property (GObject *object,
94 static void gtk_font_button_clicked (GtkButton *button);
96 /* Dialog response functions */
97 static void dialog_ok_clicked (GtkWidget *widget,
99 static void dialog_cancel_clicked (GtkWidget *widget,
101 static void dialog_destroy (GtkWidget *widget,
104 /* Auxiliary functions */
105 static GtkWidget *gtk_font_button_create_inside (GtkFontButton *gfs);
106 static void gtk_font_button_label_use_font (GtkFontButton *gfs);
107 static void gtk_font_button_update_font_info (GtkFontButton *gfs);
109 static guint font_button_signals[LAST_SIGNAL] = { 0 };
111 G_DEFINE_TYPE (GtkFontButton, gtk_font_button, GTK_TYPE_BUTTON)
114 gtk_font_button_class_init (GtkFontButtonClass *klass)
116 GObjectClass *gobject_class;
117 GtkButtonClass *button_class;
119 gobject_class = (GObjectClass *) klass;
120 button_class = (GtkButtonClass *) klass;
122 gobject_class->finalize = gtk_font_button_finalize;
123 gobject_class->set_property = gtk_font_button_set_property;
124 gobject_class->get_property = gtk_font_button_get_property;
126 button_class->clicked = gtk_font_button_clicked;
128 klass->font_set = NULL;
131 * GtkFontButton:title:
133 * The title of the font selection dialog.
137 g_object_class_install_property (gobject_class,
139 g_param_spec_string ("title",
141 P_("The title of the font selection dialog"),
143 (GTK_PARAM_READABLE |
144 GTK_PARAM_WRITABLE)));
147 * GtkFontButton:font-name:
149 * The name of the currently selected font.
153 g_object_class_install_property (gobject_class,
155 g_param_spec_string ("font-name",
157 P_("The name of the selected font"),
159 (GTK_PARAM_READABLE |
160 GTK_PARAM_WRITABLE)));
163 * GtkFontButton:use-font:
165 * If this property is set to %TRUE, the label will be drawn
166 * in the selected font.
170 g_object_class_install_property (gobject_class,
172 g_param_spec_boolean ("use-font",
173 P_("Use font in label"),
174 P_("Whether the label is drawn in the selected font"),
176 GTK_PARAM_READWRITE));
179 * GtkFontButton:use-size:
181 * If this property is set to %TRUE, the label will be drawn
182 * with the selected font size.
186 g_object_class_install_property (gobject_class,
188 g_param_spec_boolean ("use-size",
189 P_("Use size in label"),
190 P_("Whether the label is drawn with the selected font size"),
192 GTK_PARAM_READWRITE));
195 * GtkFontButton:show-style:
197 * If this property is set to %TRUE, the name of the selected font style
198 * will be shown in the label. For a more WYSIWYG way to show the selected
199 * style, see the ::use-font property.
203 g_object_class_install_property (gobject_class,
205 g_param_spec_boolean ("show-style",
207 P_("Whether the selected font style is shown in the label"),
209 GTK_PARAM_READWRITE));
211 * GtkFontButton:show-size:
213 * If this property is set to %TRUE, the selected font size will be shown
214 * in the label. For a more WYSIWYG way to show the selected size, see the
215 * ::use-size property.
219 g_object_class_install_property (gobject_class,
221 g_param_spec_boolean ("show-size",
223 P_("Whether selected font size is shown in the label"),
225 GTK_PARAM_READWRITE));
228 * GtkFontButton::font-set:
229 * @widget: the object which received the signal.
231 * The ::font-set signal is emitted when the user selects a font.
232 * When handling this signal, use gtk_font_button_get_font_name()
233 * to find out which font was just selected.
235 * Note that this signal is only emitted when the <emphasis>user</emphasis>
236 * changes the font. If you need to react to programmatic font changes
237 * as well, use the notify::font-name signal.
241 font_button_signals[FONT_SET] = g_signal_new (I_("font-set"),
242 G_TYPE_FROM_CLASS (gobject_class),
244 G_STRUCT_OFFSET (GtkFontButtonClass, font_set),
246 g_cclosure_marshal_VOID__VOID,
249 g_type_class_add_private (gobject_class, sizeof (GtkFontButtonPrivate));
253 gtk_font_button_init (GtkFontButton *font_button)
255 font_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (font_button,
256 GTK_TYPE_FONT_BUTTON,
257 GtkFontButtonPrivate);
259 /* Initialize fields */
260 font_button->priv->fontname = g_strdup (_("Sans 12"));
261 font_button->priv->use_font = FALSE;
262 font_button->priv->use_size = FALSE;
263 font_button->priv->show_style = TRUE;
264 font_button->priv->show_size = TRUE;
265 font_button->priv->font_dialog = NULL;
266 font_button->priv->title = g_strdup (_("Pick a Font"));
268 font_button->priv->inside = gtk_font_button_create_inside (font_button);
269 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
271 gtk_font_button_update_font_info (font_button);
276 gtk_font_button_finalize (GObject *object)
278 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
280 if (font_button->priv->font_dialog != NULL)
281 gtk_widget_destroy (font_button->priv->font_dialog);
282 font_button->priv->font_dialog = NULL;
284 g_free (font_button->priv->fontname);
285 font_button->priv->fontname = NULL;
287 g_free (font_button->priv->title);
288 font_button->priv->title = NULL;
290 G_OBJECT_CLASS (gtk_font_button_parent_class)->finalize (object);
294 gtk_font_button_set_property (GObject *object,
299 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
304 gtk_font_button_set_title (font_button, g_value_get_string (value));
307 gtk_font_button_set_font_name (font_button, g_value_get_string (value));
310 gtk_font_button_set_use_font (font_button, g_value_get_boolean (value));
313 gtk_font_button_set_use_size (font_button, g_value_get_boolean (value));
315 case PROP_SHOW_STYLE:
316 gtk_font_button_set_show_style (font_button, g_value_get_boolean (value));
319 gtk_font_button_set_show_size (font_button, g_value_get_boolean (value));
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
328 gtk_font_button_get_property (GObject *object,
333 GtkFontButton *font_button = GTK_FONT_BUTTON (object);
338 g_value_set_string (value, gtk_font_button_get_title (font_button));
341 g_value_set_string (value, gtk_font_button_get_font_name (font_button));
344 g_value_set_boolean (value, gtk_font_button_get_use_font (font_button));
347 g_value_set_boolean (value, gtk_font_button_get_use_size (font_button));
349 case PROP_SHOW_STYLE:
350 g_value_set_boolean (value, gtk_font_button_get_show_style (font_button));
353 g_value_set_boolean (value, gtk_font_button_get_show_size (font_button));
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
363 * gtk_font_button_new:
365 * Creates a new font picker widget.
367 * Returns: a new font picker widget.
372 gtk_font_button_new (void)
374 return g_object_new (GTK_TYPE_FONT_BUTTON, NULL);
378 * gtk_font_button_new_with_font:
379 * @fontname: Name of font to display in font selection dialog
381 * Creates a new font picker widget.
383 * Returns: a new font picker widget.
388 gtk_font_button_new_with_font (const gchar *fontname)
390 return g_object_new (GTK_TYPE_FONT_BUTTON, "font-name", fontname, NULL);
394 * gtk_font_button_set_title:
395 * @font_button: a #GtkFontButton
396 * @title: a string containing the font selection dialog title
398 * Sets the title for the font selection dialog.
403 gtk_font_button_set_title (GtkFontButton *font_button,
407 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
409 old_title = font_button->priv->title;
410 font_button->priv->title = g_strdup (title);
413 if (font_button->priv->font_dialog)
414 gtk_window_set_title (GTK_WINDOW (font_button->priv->font_dialog),
415 font_button->priv->title);
417 g_object_notify (G_OBJECT (font_button), "title");
421 * gtk_font_button_get_title:
422 * @font_button: a #GtkFontButton
424 * Retrieves the title of the font selection dialog.
426 * Returns: an internal copy of the title string which must not be freed.
430 G_CONST_RETURN gchar*
431 gtk_font_button_get_title (GtkFontButton *font_button)
433 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
435 return font_button->priv->title;
439 * gtk_font_button_get_use_font:
440 * @font_button: a #GtkFontButton
442 * Returns whether the selected font is used in the label.
444 * Returns: whether the selected font is used in the label.
449 gtk_font_button_get_use_font (GtkFontButton *font_button)
451 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
453 return font_button->priv->use_font;
457 * gtk_font_button_set_use_font:
458 * @font_button: a #GtkFontButton
459 * @use_font: If %TRUE, font name will be written using font chosen.
461 * If @use_font is %TRUE, the font name will be written using the selected font.
466 gtk_font_button_set_use_font (GtkFontButton *font_button,
469 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
471 use_font = (use_font != FALSE);
473 if (font_button->priv->use_font != use_font)
475 font_button->priv->use_font = use_font;
478 gtk_font_button_label_use_font (font_button);
480 gtk_widget_set_style (font_button->priv->font_label, NULL);
482 g_object_notify (G_OBJECT (font_button), "use-font");
488 * gtk_font_button_get_use_size:
489 * @font_button: a #GtkFontButton
491 * Returns whether the selected size is used in the label.
493 * Returns: whether the selected size is used in the label.
498 gtk_font_button_get_use_size (GtkFontButton *font_button)
500 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
502 return font_button->priv->use_size;
506 * gtk_font_button_set_use_size:
507 * @font_button: a #GtkFontButton
508 * @use_size: If %TRUE, font name will be written using the selected size.
510 * If @use_size is %TRUE, the font name will be written using the selected size.
515 gtk_font_button_set_use_size (GtkFontButton *font_button,
518 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
520 use_size = (use_size != FALSE);
521 if (font_button->priv->use_size != use_size)
523 font_button->priv->use_size = use_size;
525 if (font_button->priv->use_font)
526 gtk_font_button_label_use_font (font_button);
528 g_object_notify (G_OBJECT (font_button), "use-size");
533 * gtk_font_button_get_show_style:
534 * @font_button: a #GtkFontButton
536 * Returns whether the name of the font style will be shown in the label.
538 * Return value: whether the font style will be shown in the label.
543 gtk_font_button_get_show_style (GtkFontButton *font_button)
545 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
547 return font_button->priv->show_style;
551 * gtk_font_button_set_show_style:
552 * @font_button: a #GtkFontButton
553 * @show_style: %TRUE if font style should be displayed in label.
555 * If @show_style is %TRUE, the font style will be displayed along with name of the selected font.
560 gtk_font_button_set_show_style (GtkFontButton *font_button,
563 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
565 show_style = (show_style != FALSE);
566 if (font_button->priv->show_style != show_style)
568 font_button->priv->show_style = show_style;
570 gtk_font_button_update_font_info (font_button);
572 g_object_notify (G_OBJECT (font_button), "show-style");
578 * gtk_font_button_get_show_size:
579 * @font_button: a #GtkFontButton
581 * Returns whether the font size will be shown in the label.
583 * Return value: whether the font size will be shown in the label.
588 gtk_font_button_get_show_size (GtkFontButton *font_button)
590 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
592 return font_button->priv->show_size;
596 * gtk_font_button_set_show_size:
597 * @font_button: a #GtkFontButton
598 * @show_size: %TRUE if font size should be displayed in dialog.
600 * If @show_size is %TRUE, the font size will be displayed along with the name of the selected font.
605 gtk_font_button_set_show_size (GtkFontButton *font_button,
608 g_return_if_fail (GTK_IS_FONT_BUTTON (font_button));
610 show_size = (show_size != FALSE);
612 if (font_button->priv->show_size != show_size)
614 font_button->priv->show_size = show_size;
616 gtk_container_remove (GTK_CONTAINER (font_button), font_button->priv->inside);
617 font_button->priv->inside = gtk_font_button_create_inside (font_button);
618 gtk_container_add (GTK_CONTAINER (font_button), font_button->priv->inside);
620 gtk_font_button_update_font_info (font_button);
622 g_object_notify (G_OBJECT (font_button), "show-size");
628 * gtk_font_button_get_font_name:
629 * @font_button: a #GtkFontButton
631 * Retrieves the name of the currently selected font. This name includes
632 * style and size information as well. If you want to render something
633 * with the font, use this string with pango_font_description_from_string() .
634 * If you're interested in peeking certain values (family name,
635 * style, size, weight) just query these properties from the
636 * #PangoFontDescription object.
638 * Returns: an internal copy of the font name which must not be freed.
642 G_CONST_RETURN gchar *
643 gtk_font_button_get_font_name (GtkFontButton *font_button)
645 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), NULL);
647 return font_button->priv->fontname;
651 * gtk_font_button_set_font_name:
652 * @font_button: a #GtkFontButton
653 * @fontname: Name of font to display in font selection dialog
655 * Sets or updates the currently-displayed font in font picker dialog.
657 * Returns: Return value of gtk_font_selection_dialog_set_font_name() if the
658 * font selection dialog exists, otherwise %FALSE.
663 gtk_font_button_set_font_name (GtkFontButton *font_button,
664 const gchar *fontname)
669 g_return_val_if_fail (GTK_IS_FONT_BUTTON (font_button), FALSE);
670 g_return_val_if_fail (fontname != NULL, FALSE);
672 if (g_ascii_strcasecmp (font_button->priv->fontname, fontname))
674 old_fontname = font_button->priv->fontname;
675 font_button->priv->fontname = g_strdup (fontname);
676 g_free (old_fontname);
679 gtk_font_button_update_font_info (font_button);
681 if (font_button->priv->font_dialog)
682 result = gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog),
683 font_button->priv->fontname);
687 g_object_notify (G_OBJECT (font_button), "font-name");
693 gtk_font_button_clicked (GtkButton *button)
695 GtkFontSelectionDialog *font_dialog;
696 GtkFontButton *font_button = GTK_FONT_BUTTON (button);
698 if (!font_button->priv->font_dialog)
702 parent = gtk_widget_get_toplevel (GTK_WIDGET (font_button));
704 font_button->priv->font_dialog = gtk_font_selection_dialog_new (font_button->priv->title);
706 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
708 if (gtk_widget_is_toplevel (parent) && GTK_IS_WINDOW (parent))
710 if (GTK_WINDOW (parent) != gtk_window_get_transient_for (GTK_WINDOW (font_dialog)))
711 gtk_window_set_transient_for (GTK_WINDOW (font_dialog), GTK_WINDOW (parent));
713 gtk_window_set_modal (GTK_WINDOW (font_dialog),
714 gtk_window_get_modal (GTK_WINDOW (parent)));
717 g_signal_connect (gtk_font_selection_dialog_get_ok_button (font_dialog), "clicked",
718 G_CALLBACK (dialog_ok_clicked), font_button);
719 g_signal_connect (gtk_font_selection_dialog_get_cancel_button (font_dialog), "clicked",
720 G_CALLBACK (dialog_cancel_clicked), font_button);
721 g_signal_connect (font_dialog, "destroy",
722 G_CALLBACK (dialog_destroy), font_button);
725 if (!gtk_widget_get_visible (font_button->priv->font_dialog))
727 font_dialog = GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog);
729 gtk_font_selection_dialog_set_font_name (font_dialog, font_button->priv->fontname);
733 gtk_window_present (GTK_WINDOW (font_button->priv->font_dialog));
737 dialog_ok_clicked (GtkWidget *widget,
740 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
742 gtk_widget_hide (font_button->priv->font_dialog);
744 g_free (font_button->priv->fontname);
745 font_button->priv->fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_button->priv->font_dialog));
748 gtk_font_button_update_font_info (font_button);
750 g_object_notify (G_OBJECT (font_button), "font-name");
752 /* Emit font_set signal */
753 g_signal_emit (font_button, font_button_signals[FONT_SET], 0);
758 dialog_cancel_clicked (GtkWidget *widget,
761 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
763 gtk_widget_hide (font_button->priv->font_dialog);
767 dialog_destroy (GtkWidget *widget,
770 GtkFontButton *font_button = GTK_FONT_BUTTON (data);
772 /* Dialog will get destroyed so reference is not valid now */
773 font_button->priv->font_dialog = NULL;
777 gtk_font_button_create_inside (GtkFontButton *font_button)
781 gtk_widget_push_composite_child ();
783 widget = gtk_hbox_new (FALSE, 0);
785 font_button->priv->font_label = gtk_label_new (_("Font"));
787 gtk_label_set_justify (GTK_LABEL (font_button->priv->font_label), GTK_JUSTIFY_LEFT);
788 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->font_label, TRUE, TRUE, 5);
790 if (font_button->priv->show_size)
792 gtk_box_pack_start (GTK_BOX (widget), gtk_vseparator_new (), FALSE, FALSE, 0);
793 font_button->priv->size_label = gtk_label_new ("14");
794 gtk_box_pack_start (GTK_BOX (widget), font_button->priv->size_label, FALSE, FALSE, 5);
797 gtk_widget_show_all (widget);
799 gtk_widget_pop_composite_child ();
805 gtk_font_button_label_use_font (GtkFontButton *font_button)
807 PangoFontDescription *desc;
809 if (!font_button->priv->use_font)
812 desc = pango_font_description_from_string (font_button->priv->fontname);
814 if (!font_button->priv->use_size)
815 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
817 gtk_widget_modify_font (font_button->priv->font_label, desc);
819 pango_font_description_free (desc);
823 font_description_style_equal (const PangoFontDescription *a,
824 const PangoFontDescription *b)
826 return (pango_font_description_get_weight (a) == pango_font_description_get_weight (b) &&
827 pango_font_description_get_style (a) == pango_font_description_get_style (b) &&
828 pango_font_description_get_stretch (a) == pango_font_description_get_stretch (b) &&
829 pango_font_description_get_variant (a) == pango_font_description_get_variant (b));
833 gtk_font_button_update_font_info (GtkFontButton *font_button)
835 PangoFontDescription *desc;
840 desc = pango_font_description_from_string (font_button->priv->fontname);
841 family = pango_font_description_get_family (desc);
844 /* This gives the wrong names, e.g. Italic when the font selection
845 * dialog displayed Oblique.
847 pango_font_description_unset_fields (desc, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE);
848 style = pango_font_description_to_string (desc);
849 gtk_label_set_text (GTK_LABEL (font_button->priv->style_label), style);
853 if (font_button->priv->show_style && family)
855 PangoFontFamily **families;
856 PangoFontFace **faces;
857 gint n_families, n_faces, i;
861 pango_context_list_families (gtk_widget_get_pango_context (GTK_WIDGET (font_button)),
862 &families, &n_families);
865 for (i = 0; i < n_families; i++)
867 const gchar *name = pango_font_family_get_name (families[i]);
869 if (!g_ascii_strcasecmp (name, family))
871 pango_font_family_list_faces (families[i], &faces, &n_faces);
877 for (i = 0; i < n_faces; i++)
879 PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
881 if (font_description_style_equal (tmp_desc, desc))
883 style = g_strdup (pango_font_face_get_face_name (faces[i]));
884 pango_font_description_free (tmp_desc);
888 pango_font_description_free (tmp_desc);
893 if (style == NULL || !g_ascii_strcasecmp (style, "Regular"))
894 family_style = g_strdup (family);
896 family_style = g_strdup_printf ("%s %s", family, style);
898 gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
901 g_free (family_style);
903 if (font_button->priv->show_size)
905 gchar *size = g_strdup_printf ("%g",
906 pango_font_description_get_size (desc) / (double)PANGO_SCALE);
908 gtk_label_set_text (GTK_LABEL (font_button->priv->size_label), size);
913 gtk_font_button_label_use_font (font_button);
915 pango_font_description_free (desc);