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.
79 static void gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface);
80 static GObject* gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
82 const gchar *childname);
84 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog,
86 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
87 gtk_font_chooser_dialog_buildable_interface_init))
89 static GtkBuildableIface *parent_buildable_iface;
92 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
94 g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
98 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
100 GtkFontChooserDialogPrivate *priv;
101 GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
102 GtkWidget *action_area, *content_area;
104 fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
105 GTK_TYPE_FONT_CHOOSER_DIALOG,
106 GtkFontChooserDialogPrivate);
107 priv = fontchooserdiag->priv;
109 content_area = gtk_dialog_get_content_area (dialog);
110 action_area = gtk_dialog_get_action_area (dialog);
112 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
113 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
114 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
115 gtk_box_set_spacing (GTK_BOX (action_area), 6);
117 gtk_widget_push_composite_child ();
119 gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
121 /* Create the content area */
122 priv->fontchooser = gtk_font_chooser_new ();
123 gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
124 gtk_widget_show (priv->fontchooser);
125 gtk_box_pack_start (GTK_BOX (content_area),
126 priv->fontchooser, TRUE, TRUE, 0);
128 /* Create the action area */
129 priv->cancel_button = gtk_dialog_add_button (dialog,
131 GTK_RESPONSE_CANCEL);
132 priv->select_button = gtk_dialog_add_button (dialog,
135 gtk_widget_grab_default (priv->select_button);
137 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
142 gtk_window_set_title (GTK_WINDOW (fontchooserdiag),
143 _("Font Selection"));
145 gtk_widget_pop_composite_child ();
149 * gtk_font_chooser_dialog_new:
150 * @title: (allow-none): the title of the dialog window
152 * Creates a new #GtkFontChooserDialog.
154 * Return value: a new #GtkFontChooserDialog
159 gtk_font_chooser_dialog_new (const gchar *title)
161 GtkFontChooserDialog *fontchooserdiag;
163 fontchooserdiag = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG, NULL);
166 gtk_window_set_title (GTK_WINDOW (fontchooserdiag), title);
168 return GTK_WIDGET (fontchooserdiag);
172 * gtk_font_chooser_dialog_get_font_chooser:
173 * @fcd: a #GtkFontChooserDialog
175 * Retrieves the #GtkFontChooser widget embedded in the dialog.
177 * Returns: (transfer none): the embedded #GtkFontChooser
182 gtk_font_chooser_dialog_get_font_chooser (GtkFontChooserDialog *fcd)
184 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
186 return fcd->priv->fontchooser;
190 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
192 parent_buildable_iface = g_type_interface_peek_parent (iface);
193 iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
197 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
199 const gchar *childname)
201 GtkFontChooserDialogPrivate *priv;
203 priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
205 if (g_strcmp0 (childname, "select_button") == 0)
206 return G_OBJECT (priv->select_button);
207 else if (g_strcmp0 (childname, "cancel_button") == 0)
208 return G_OBJECT (priv->cancel_button);
209 else if (g_strcmp0 (childname, "font_chooser") == 0)
210 return G_OBJECT (priv->fontchooser);
212 return parent_buildable_iface->get_internal_child (buildable, builder, childname);
216 * gtk_font_chooser_dialog_get_font_name:
217 * @fcd: a #GtkFontChooserDialog
219 * Gets the currently-selected font name.
221 * Note that this can be a different string than what you set with
222 * gtk_font_chooser_dialog_set_font_name(), as the font chooser widget
223 * may normalize font names and thus return a string with a different
224 * structure. For example, "Helvetica Italic Bold 12" could be normalized
225 * to "Helvetica Bold Italic 12". Use pango_font_description_equal()
226 * if you want to compare two font descriptions.
228 * Return value: A string with the name of the current font, or %NULL if no
229 * font is selected. You must free this string with g_free().
234 gtk_font_chooser_dialog_get_font_name (GtkFontChooserDialog *fcd)
236 GtkFontChooserDialogPrivate *priv;
238 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
242 return gtk_font_chooser_get_font_name (GTK_FONT_CHOOSER (priv->fontchooser));
246 * gtk_font_chooser_dialog_set_font_name:
247 * @fcd: a #GtkFontChooserDialog
248 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
250 * Sets the currently selected font.
252 * Return value: %TRUE if the font selected in @fcd is now the
253 * @fontname specified, %FALSE otherwise.
258 gtk_font_chooser_dialog_set_font_name (GtkFontChooserDialog *fcd,
259 const gchar *fontname)
261 GtkFontChooserDialogPrivate *priv;
263 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), FALSE);
264 g_return_val_if_fail (fontname, FALSE);
268 return gtk_font_chooser_set_font_name (GTK_FONT_CHOOSER (priv->fontchooser), fontname);
272 * gtk_font_chooser_dialog_get_preview_text:
273 * @fcd: a #GtkFontChooserDialog
275 * Gets the text displayed in the preview area.
277 * Return value: the text displayed in the preview area.
278 * This string is owned by the widget and should not be
284 gtk_font_chooser_dialog_get_preview_text (GtkFontChooserDialog *fcd)
286 GtkFontChooserDialogPrivate *priv;
288 g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
292 return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->fontchooser));
296 * gtk_font_chooser_dialog_set_preview_text:
297 * @fcd: a #GtkFontChooserDialog
298 * @text: the text to display in the preview area
300 * Sets the text displayed in the preview area.
305 gtk_font_chooser_dialog_set_preview_text (GtkFontChooserDialog *fcd,
308 GtkFontChooserDialogPrivate *priv;
310 g_return_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd));
311 g_return_if_fail (text != NULL);
315 gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->fontchooser), text);