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, see <http://www.gnu.org/licenses/>.
21 #include <glib/gprintf.h>
26 #include "gtkfontchooserdialog.h"
27 #include "gtkfontchooser.h"
28 #include "gtkfontchooserwidget.h"
29 #include "gtkfontchooserutils.h"
33 #include "gtkaccessible.h"
34 #include "gtkbuildable.h"
35 #include "gtkprivate.h"
36 #include "gtkwidget.h"
38 struct _GtkFontChooserDialogPrivate
40 GtkWidget *fontchooser;
42 GtkWidget *select_button;
43 GtkWidget *cancel_button;
47 * SECTION:gtkfontchooserdialog
48 * @Short_description: A dialog for selecting fonts
49 * @Title: GtkFontChooserDialog
50 * @See_also: #GtkFontChooser, #GtkDialog
52 * The #GtkFontChooserDialog widget is a dialog for selecting a font.
53 * It implements the #GtkFontChooser interface.
55 * <refsect2 id="GtkFontChooserDialog-BUILDER-UI">
56 * <title>GtkFontChooserDialog as GtkBuildable</title>
57 * The GtkFontChooserDialog implementation of the GtkBuildable interface
58 * exposes the buttons with the names
59 * "select_button" and "cancel_button.
65 static void gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface);
66 static GObject *gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
68 const gchar *childname);
70 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog, GTK_TYPE_DIALOG,
71 G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
72 _gtk_font_chooser_delegate_iface_init)
73 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
74 gtk_font_chooser_dialog_buildable_interface_init))
76 static GtkBuildableIface *parent_buildable_iface;
79 gtk_font_chooser_dialog_set_property (GObject *object,
84 GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
85 GtkFontChooserDialogPrivate *priv = dialog->priv;
90 g_object_set_property (G_OBJECT (priv->fontchooser), pspec->name, value);
96 gtk_font_chooser_dialog_get_property (GObject *object,
101 GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
102 GtkFontChooserDialogPrivate *priv = dialog->priv;
107 g_object_get_property (G_OBJECT (priv->fontchooser), pspec->name, value);
113 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 gobject_class->get_property = gtk_font_chooser_dialog_get_property;
118 gobject_class->set_property = gtk_font_chooser_dialog_set_property;
120 _gtk_font_chooser_install_properties (gobject_class);
122 g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
126 font_activated_cb (GtkFontChooser *fontchooser,
127 const gchar *fontname,
130 GtkDialog *dialog = user_data;
132 gtk_dialog_response (dialog, GTK_RESPONSE_OK);
136 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
138 GtkFontChooserDialogPrivate *priv;
139 GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
140 GtkWidget *action_area, *content_area;
142 fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
143 GTK_TYPE_FONT_CHOOSER_DIALOG,
144 GtkFontChooserDialogPrivate);
145 priv = fontchooserdiag->priv;
147 content_area = gtk_dialog_get_content_area (dialog);
148 action_area = gtk_dialog_get_action_area (dialog);
150 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
151 gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
152 gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
153 gtk_box_set_spacing (GTK_BOX (action_area), 6);
155 gtk_widget_push_composite_child ();
157 gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
159 /* Create the content area */
160 priv->fontchooser = gtk_font_chooser_widget_new ();
161 gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
162 gtk_widget_show (priv->fontchooser);
163 gtk_box_pack_start (GTK_BOX (content_area),
164 priv->fontchooser, TRUE, TRUE, 0);
166 g_signal_connect (priv->fontchooser, "font-activated",
167 G_CALLBACK (font_activated_cb), dialog);
169 /* Create the action area */
170 priv->cancel_button = gtk_dialog_add_button (dialog,
172 GTK_RESPONSE_CANCEL);
173 priv->select_button = gtk_dialog_add_button (dialog,
176 gtk_widget_grab_default (priv->select_button);
178 gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
183 gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
185 gtk_widget_pop_composite_child ();
187 _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
188 GTK_FONT_CHOOSER (priv->fontchooser));
192 * gtk_font_chooser_dialog_new:
193 * @title: (allow-none): Title of the dialog, or %NULL
194 * @parent: (allow-none): Transient parent of the dialog, or %NULL
196 * Creates a new #GtkFontChooserDialog.
198 * Return value: a new #GtkFontChooserDialog
203 gtk_font_chooser_dialog_new (const gchar *title,
206 GtkFontChooserDialog *dialog;
208 dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
210 "transient-for", parent,
213 return GTK_WIDGET (dialog);
217 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
219 parent_buildable_iface = g_type_interface_peek_parent (iface);
220 iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
224 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
226 const gchar *childname)
228 GtkFontChooserDialogPrivate *priv;
230 priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
232 if (g_strcmp0 (childname, "select_button") == 0)
233 return G_OBJECT (priv->select_button);
234 else if (g_strcmp0 (childname, "cancel_button") == 0)
235 return G_OBJECT (priv->cancel_button);
237 return parent_buildable_iface->get_internal_child (buildable, builder, childname);