1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <glib/gprintf.h>
28 #include "gtkfontchooserdialog.h"
29 #include "gtkfontchooser.h"
32 #include "gtkaccessible.h"
33 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
35 #include "gtkwidget.h"
37 struct _GtkFontChooserDialogPrivate
39 GtkWidget *fontchooser;
41 GtkWidget *select_button;
42 GtkWidget *cancel_button;
46 * SECTION:gtkfontchooserdlg
47 * @Short_description: A dialog box for selecting fonts
48 * @Title: GtkFontChooserDialog
49 * @See_also: #GtkFontChooser, #GtkDialog
51 * The #GtkFontChooserDialog widget is a dialog box for selecting a font.
53 * To set the font which is initially selected, use
54 * gtk_font_chooser_dialog_set_font_name().
56 * To get the selected font use gtk_font_chooser_dialog_get_font_name().
58 * To change the text which is shown in the preview area, use
59 * gtk_font_chooser_dialog_set_preview_text().
61 * <refsect2 id="GtkFontChooserDialog-BUILDER-UI">
62 * <title>GtkFontChooserDialog as GtkBuildable</title>
63 * The GtkFontChooserDialog implementation of the GtkBuildable interface
64 * exposes the embedded #GtkFontChooser as internal child with the
65 * name "font_chooser". It also exposes the buttons with the names
66 * "select_button" and "cancel_button.
72 static void gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface);
73 static GObject *gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
75 const gchar *childname);
77 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog, GTK_TYPE_DIALOG,
78 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
79 gtk_font_chooser_dialog_buildable_interface_init))
81 static GtkBuildableIface *parent_buildable_iface;
84 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
86 g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
90 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
92 GtkFontChooserDialogPrivate *priv;
93 GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
94 GtkWidget *action_area, *content_area;
96 fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
97 GTK_TYPE_FONT_CHOOSER_DIALOG,
98 GtkFontChooserDialogPrivate);
99 priv = fontchooserdiag->priv;
101 content_area = gtk_dialog_get_content_area (dialog);
102 action_area = gtk_dialog_get_action_area (dialog);
104 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
105 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
106 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
107 gtk_box_set_spacing (GTK_BOX (action_area), 6);
109 gtk_widget_push_composite_child ();
111 gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
113 /* Create the content area */
114 priv->fontchooser = gtk_font_chooser_new ();
115 gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
116 gtk_widget_show (priv->fontchooser);
117 gtk_box_pack_start (GTK_BOX (content_area),
118 priv->fontchooser, TRUE, TRUE, 0);
120 /* Create the action area */
121 priv->cancel_button = gtk_dialog_add_button (dialog,
123 GTK_RESPONSE_CANCEL);
124 priv->select_button = gtk_dialog_add_button (dialog,
127 gtk_widget_grab_default (priv->select_button);
129 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
134 gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
136 gtk_widget_pop_composite_child ();
140 * gtk_font_chooser_dialog_new:
141 * @title: (allow-none): Title of the dialog, or %NULL
142 * @parent: (allow-none): Trasient parent of the dialog, or %NULL
144 * Creates a new #GtkFontChooserDialog.
146 * Return value: a new #GtkFontChooserDialog
151 gtk_font_chooser_dialog_new (const gchar *title,
154 GtkFontChooserDialog *dialog;
156 dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
158 "transient-for", parent,
161 return GTK_WIDGET (dialog);
165 * gtk_font_chooser_dialog_get_font_chooser:
166 * @fcd: a #GtkFontChooserDialog
168 * Retrieves the #GtkFontChooser widget embedded in the dialog.
170 * Returns: (transfer none): the embedded #GtkFontChooser
175 gtk_font_chooser_dialog_get_font_chooser (GtkFontChooserDialog *fcd)
177 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
179 return fcd->priv->fontchooser;
183 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
185 parent_buildable_iface = g_type_interface_peek_parent (iface);
186 iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
190 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
192 const gchar *childname)
194 GtkFontChooserDialogPrivate *priv;
196 priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
198 if (g_strcmp0 (childname, "select_button") == 0)
199 return G_OBJECT (priv->select_button);
200 else if (g_strcmp0 (childname, "cancel_button") == 0)
201 return G_OBJECT (priv->cancel_button);
202 else if (g_strcmp0 (childname, "font_chooser") == 0)
203 return G_OBJECT (priv->fontchooser);
205 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
209 * gtk_font_chooser_dialog_get_font_name:
210 * @fcd: a #GtkFontChooserDialog
212 * Gets the currently-selected font name.
214 * Note that this can be a different string than what you set with
215 * gtk_font_chooser_dialog_set_font_name(), as the font chooser widget
216 * may normalize font names and thus return a string with a different
217 * structure. For example, "Helvetica Italic Bold 12" could be normalized
218 * to "Helvetica Bold Italic 12".
220 * Use pango_font_description_equal() if you want to compare two
223 * Return value: A string with the name of the current font, or %NULL
224 * if no font is selected. You must free this string with g_free().
229 gtk_font_chooser_dialog_get_font_name (GtkFontChooserDialog *fcd)
231 GtkFontChooserDialogPrivate *priv;
233 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
237 return gtk_font_chooser_get_font_name (GTK_FONT_CHOOSER (priv->fontchooser));
241 * gtk_font_chooser_dialog_set_font_name:
242 * @fcd: a #GtkFontChooserDialog
243 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
245 * Sets the currently selected font.
247 * Return value: %TRUE if the font selected in @fcd is now the
248 * @fontname specified, %FALSE otherwise.
253 gtk_font_chooser_dialog_set_font_name (GtkFontChooserDialog *fcd,
254 const gchar *fontname)
256 GtkFontChooserDialogPrivate *priv;
258 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), FALSE);
259 g_return_val_if_fail (fontname, FALSE);
263 return gtk_font_chooser_set_font_name (GTK_FONT_CHOOSER (priv->fontchooser), fontname);
267 * gtk_font_chooser_dialog_get_preview_text:
268 * @fcd: a #GtkFontChooserDialog
270 * Gets the text displayed in the preview area.
272 * Return value: the text displayed in the preview area.
273 * This string is owned by the widget and should not be
279 gtk_font_chooser_dialog_get_preview_text (GtkFontChooserDialog *fcd)
281 GtkFontChooserDialogPrivate *priv;
283 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
287 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->fontchooser));
291 * gtk_font_chooser_dialog_set_preview_text:
292 * @fcd: a #GtkFontChooserDialog
293 * @text: the text to display in the preview area
295 * Sets the text displayed in the preview area.
300 gtk_font_chooser_dialog_set_preview_text (GtkFontChooserDialog *fcd,
303 GtkFontChooserDialogPrivate *priv;
305 g_return_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd));
306 g_return_if_fail (text != NULL);
310 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->fontchooser), text);