]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserdialog.c
Cosmetic doc fixes
[~andy/gtk] / gtk / gtkfontchooserdialog.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <glib/gprintf.h>
24 #include <string.h>
25
26 #include <atk/atk.h>
27
28 #include "gtkfontchooserdialog.h"
29 #include "gtkfontchooser.h"
30 #include "gtkfontchooserwidget.h"
31 #include "gtkfontchooserutils.h"
32 #include "gtkbox.h"
33 #include "gtkstock.h"
34 #include "gtkintl.h"
35 #include "gtkaccessible.h"
36 #include "gtkbuildable.h"
37 #include "gtkprivate.h"
38 #include "gtkwidget.h"
39
40 struct _GtkFontChooserDialogPrivate
41 {
42   GtkWidget *fontchooser;
43
44   GtkWidget *select_button;
45   GtkWidget *cancel_button;
46 };
47
48 /**
49  * SECTION:gtkfontchooserdlg
50  * @Short_description: A dialog box for selecting fonts
51  * @Title: GtkFontChooserDialog
52  * @See_also: #GtkFontChooser, #GtkDialog
53  *
54  * The #GtkFontChooserDialog widget is a dialog box for selecting a font.
55  * It implements the #GtkFontChooser interface.
56  *
57  * <refsect2 id="GtkFontChooserDialog-BUILDER-UI">
58  * <title>GtkFontChooserDialog as GtkBuildable</title>
59  * The GtkFontChooserDialog implementation of the GtkBuildable interface
60  * exposes the buttons with the names
61  * "select_button" and "cancel_button.
62  * </refsect2>
63  *
64  * Since: 3.2
65  */
66
67 static void     gtk_font_chooser_dialog_buildable_interface_init     (GtkBuildableIface *iface);
68 static GObject *gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
69                                                                       GtkBuilder   *builder,
70                                                                       const gchar  *childname);
71
72 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog, GTK_TYPE_DIALOG,
73                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FONT_CHOOSER,
74                                                 _gtk_font_chooser_delegate_iface_init)
75                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
76                                                 gtk_font_chooser_dialog_buildable_interface_init))
77
78 static GtkBuildableIface *parent_buildable_iface;
79
80 static void
81 gtk_font_chooser_dialog_set_property (GObject      *object,
82                                       guint         prop_id,
83                                       const GValue *value,
84                                       GParamSpec   *pspec)
85 {
86   GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
87   GtkFontChooserDialogPrivate *priv = dialog->priv;
88
89   switch (prop_id)
90     {
91     default:
92       g_object_set_property (G_OBJECT (priv->fontchooser), pspec->name, value);
93       break;
94     }
95 }
96
97 static void
98 gtk_font_chooser_dialog_get_property (GObject      *object,
99                                       guint         prop_id,
100                                       GValue       *value,
101                                       GParamSpec   *pspec)
102 {
103   GtkFontChooserDialog *dialog = GTK_FONT_CHOOSER_DIALOG (object);
104   GtkFontChooserDialogPrivate *priv = dialog->priv;
105
106   switch (prop_id)
107     {
108     default:
109       g_object_get_property (G_OBJECT (priv->fontchooser), pspec->name, value);
110       break;
111     }
112 }
113
114 static void
115 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
116 {
117   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118
119   gobject_class->get_property = gtk_font_chooser_dialog_get_property;
120   gobject_class->set_property = gtk_font_chooser_dialog_set_property;
121
122   _gtk_font_chooser_install_properties (gobject_class);
123
124   g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
125 }
126
127 static void
128 font_activated_cb (GtkFontChooser *fontchooser,
129                    const gchar    *fontname,
130                    gpointer        user_data)
131 {
132   GtkDialog *dialog = user_data;
133
134   gtk_dialog_response (dialog, GTK_RESPONSE_OK);
135 }
136
137 static void
138 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
139 {
140   GtkFontChooserDialogPrivate *priv;
141   GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
142   GtkWidget *action_area, *content_area;
143
144   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
145                                                        GTK_TYPE_FONT_CHOOSER_DIALOG,
146                                                        GtkFontChooserDialogPrivate);
147   priv = fontchooserdiag->priv;
148
149   content_area = gtk_dialog_get_content_area (dialog);
150   action_area = gtk_dialog_get_action_area (dialog);
151
152   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
153   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
154   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
155   gtk_box_set_spacing (GTK_BOX (action_area), 6);
156
157   gtk_widget_push_composite_child ();
158
159   gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
160
161   /* Create the content area */
162   priv->fontchooser = gtk_font_chooser_widget_new ();
163   gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
164   gtk_widget_show (priv->fontchooser);
165   gtk_box_pack_start (GTK_BOX (content_area),
166                       priv->fontchooser, TRUE, TRUE, 0);
167
168   g_signal_connect (priv->fontchooser, "font-activated",
169                     G_CALLBACK (font_activated_cb), dialog);
170
171   /* Create the action area */
172   priv->cancel_button = gtk_dialog_add_button (dialog,
173                                                GTK_STOCK_CANCEL,
174                                                GTK_RESPONSE_CANCEL);
175   priv->select_button = gtk_dialog_add_button (dialog,
176                                                _("_Select"),
177                                                GTK_RESPONSE_OK);
178   gtk_widget_grab_default (priv->select_button);
179
180   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
181                                            GTK_RESPONSE_OK,
182                                            GTK_RESPONSE_CANCEL,
183                                            -1);
184
185   gtk_window_set_title (GTK_WINDOW (fontchooserdiag), _("Font Selection"));
186
187   gtk_widget_pop_composite_child ();
188
189   _gtk_font_chooser_set_delegate (GTK_FONT_CHOOSER (fontchooserdiag),
190                                   GTK_FONT_CHOOSER (priv->fontchooser));
191 }
192
193 /** gtk_font_chooser_dialog_new:
194  * @title: (allow-none): Title of the dialog, or %NULL
195  * @parent: (allow-none): Transient parent of the dialog, or %NULL
196  *
197  * Creates a new #GtkFontChooserDialog.
198  *
199  * Return value: a new #GtkFontChooserDialog
200  *
201  * Since: 3.2
202  */
203 GtkWidget*
204 gtk_font_chooser_dialog_new (const gchar *title,
205                              GtkWindow   *parent)
206 {
207   GtkFontChooserDialog *dialog;
208
209   dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
210                          "title", title,
211                          "transient-for", parent,
212                          NULL);
213
214   return GTK_WIDGET (dialog);
215 }
216
217 static void
218 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
219 {
220   parent_buildable_iface = g_type_interface_peek_parent (iface);
221   iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
222 }
223
224 static GObject *
225 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
226                                                       GtkBuilder   *builder,
227                                                       const gchar  *childname)
228 {
229   GtkFontChooserDialogPrivate *priv;
230
231   priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
232
233   if (g_strcmp0 (childname, "select_button") == 0)
234     return G_OBJECT (priv->select_button);
235   else if (g_strcmp0 (childname, "cancel_button") == 0)
236     return G_OBJECT (priv->cancel_button);
237
238   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
239 }