1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * Massively updated to rework the user interface by Alberto Ruiz, 2011
6 * Massively updated for Pango by Owen Taylor, May 2000
7 * GtkFontChooser widget for Gtk+, by Damon Chaplin, May 1998.
8 * Based on the GnomeFontSelector widget, by Elliot Lee, but major changes.
9 * The GnomeFontSelector was derived from app/text_tool.c in the GIMP.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
30 #include <glib/gprintf.h>
35 #include "gtkfontchooserdialog.h"
36 #include "gtkfontchooser.h"
39 #include "gtkaccessible.h"
40 #include "gtkbuildable.h"
41 #include "gtkprivate.h"
42 #include "gtkwidget.h"
44 struct _GtkFontChooserDialogPrivate
46 GtkWidget *fontchooser;
48 GtkWidget *select_button;
49 GtkWidget *cancel_button;
53 * SECTION:gtkfontchooserdlg
54 * @Short_description: A dialog box for selecting fonts
55 * @Title: GtkFontChooserDialog
56 * @See_also: #GtkFontChooser, #GtkDialog
58 * The #GtkFontChooserDialog widget is a dialog box for selecting a font.
60 * To set the font which is initially selected, use
61 * gtk_font_chooser_dialog_set_font_name().
63 * To get the selected font use gtk_font_chooser_dialog_get_font_name().
65 * To change the text which is shown in the preview area, use
66 * gtk_font_chooser_dialog_set_preview_text().
68 * <refsect2 id="GtkFontChooserDialog-BUILDER-UI">
69 * <title>GtkFontChooserDialog as GtkBuildable</title>
70 * The GtkFontChooserDialog implementation of the GtkBuildable interface
71 * exposes the embedded #GtkFontChooser as internal child with the
72 * name "font_chooser". It also exposes the buttons with the names
73 * "select_button" and "cancel_button.
77 static void gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface);
78 static GObject* gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
80 const gchar *childname);
82 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog,
84 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
85 gtk_font_chooser_dialog_buildable_interface_init))
87 static GtkBuildableIface *parent_buildable_iface;
90 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
92 g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
96 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
98 GtkFontChooserDialogPrivate *priv;
99 GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
100 GtkWidget *action_area, *content_area;
102 fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
103 GTK_TYPE_FONT_CHOOSER_DIALOG,
104 GtkFontChooserDialogPrivate);
105 priv = fontchooserdiag->priv;
107 content_area = gtk_dialog_get_content_area (dialog);
108 action_area = gtk_dialog_get_action_area (dialog);
110 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
111 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
112 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
113 gtk_box_set_spacing (GTK_BOX (action_area), 6);
115 gtk_widget_push_composite_child ();
117 gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
119 /* Create the content area */
120 priv->fontchooser = gtk_font_chooser_new ();
121 gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
122 gtk_widget_show (priv->fontchooser);
123 gtk_box_pack_start (GTK_BOX (content_area),
124 priv->fontchooser, TRUE, TRUE, 0);
126 /* Create the action area */
127 priv->cancel_button = gtk_dialog_add_button (dialog,
129 GTK_RESPONSE_CANCEL);
130 priv->select_button = gtk_dialog_add_button (dialog,
133 gtk_widget_grab_default (priv->select_button);
135 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
140 gtk_window_set_title (GTK_WINDOW (fontchooserdiag),
141 _("Font Selection"));
143 gtk_widget_pop_composite_child ();
147 * gtk_font_chooser_dialog_new:
148 * @title: (allow-none): the title of the dialog window
150 * Creates a new #GtkFontChooserDialog.
152 * Return value: a new #GtkFontChooserDialog
155 gtk_font_chooser_dialog_new (const gchar *title)
157 GtkFontChooserDialog *fontchooserdiag;
159 fontchooserdiag = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG, NULL);
162 gtk_window_set_title (GTK_WINDOW (fontchooserdiag), title);
164 return GTK_WIDGET (fontchooserdiag);
168 * gtk_font_chooser_dialog_get_font_chooser:
169 * @fcd: a #GtkFontChooserDialog
171 * Retrieves the #GtkFontChooser widget embedded in the dialog.
173 * Returns: (transfer none): the embedded #GtkFontChooser
178 gtk_font_chooser_dialog_get_font_chooser (GtkFontChooserDialog *fcd)
180 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
182 return fcd->priv->fontchooser;
186 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
188 parent_buildable_iface = g_type_interface_peek_parent (iface);
189 iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
193 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
195 const gchar *childname)
197 GtkFontChooserDialogPrivate *priv;
199 priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
201 if (g_strcmp0 (childname, "select_button") == 0)
202 return G_OBJECT (priv->select_button);
203 else if (g_strcmp0 (childname, "cancel_button") == 0)
204 return G_OBJECT (priv->cancel_button);
205 else if (g_strcmp0 (childname, "font_chooser") == 0)
206 return G_OBJECT (priv->fontchooser);
208 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
212 * gtk_font_chooser_dialog_get_font_name:
213 * @fcd: a #GtkFontChooserDialog
215 * Gets the currently-selected font name.
217 * Note that this can be a different string than what you set with
218 * gtk_font_chooser_dialog_set_font_name(), as the font chooser widget
219 * may normalize font names and thus return a string with a different
220 * structure. For example, "Helvetica Italic Bold 12" could be normalized
221 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
222 * if you want to compare two font descriptions.
224 * Return value: A string with the name of the current font, or %NULL if no
225 * font is selected. You must free this string with g_free().
230 gtk_font_chooser_dialog_get_font_name (GtkFontChooserDialog *fcd)
232 GtkFontChooserDialogPrivate *priv;
234 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
238 return gtk_font_chooser_get_font_name (GTK_FONT_CHOOSER (priv->fontchooser));
242 * gtk_font_chooser_dialog_set_font_name:
243 * @fcd: a #GtkFontChooserDialog
244 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
246 * Sets the currently selected font.
248 * Return value: %TRUE if the font selected in @fcd is now the
249 * @fontname specified, %FALSE otherwise.
254 gtk_font_chooser_dialog_set_font_name (GtkFontChooserDialog *fcd,
255 const gchar *fontname)
257 GtkFontChooserDialogPrivate *priv;
259 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), FALSE);
260 g_return_val_if_fail (fontname, FALSE);
264 return gtk_font_chooser_set_font_name (GTK_FONT_CHOOSER (priv->fontchooser), fontname);
268 * gtk_font_chooser_dialog_get_preview_text:
269 * @fcd: a #GtkFontChooserDialog
271 * Gets the text displayed in the preview area.
273 * Return value: the text displayed in the preview area.
274 * This string is owned by the widget and should not be
279 G_CONST_RETURN gchar*
280 gtk_font_chooser_dialog_get_preview_text (GtkFontChooserDialog *fcd)
282 GtkFontChooserDialogPrivate *priv;
284 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
288 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->fontchooser));
292 * gtk_font_chooser_dialog_set_preview_text:
293 * @fcd: a #GtkFontChooserDialog
294 * @text: the text to display in the preview area
296 * Sets the text displayed in the preview area.
301 gtk_font_chooser_dialog_set_preview_text (GtkFontChooserDialog *fcd,
304 GtkFontChooserDialogPrivate *priv;
306 g_return_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd));
307 g_return_if_fail (text != NULL);
311 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->fontchooser), text);