]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserdialog.c
GtkFontChooserDialog: Add parent window as a parameter in the constructor
[~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 "gtkstock.h"
31 #include "gtkintl.h"
32 #include "gtkaccessible.h"
33 #include "gtkbuildable.h"
34 #include "gtkprivate.h"
35 #include "gtkwidget.h"
36
37 struct _GtkFontChooserDialogPrivate
38 {
39   GtkWidget *fontchooser;
40
41   GtkWidget *select_button;
42   GtkWidget *cancel_button;
43 };
44
45 /**
46  * SECTION:gtkfontchooserdlg
47  * @Short_description: A dialog box for selecting fonts
48  * @Title: GtkFontChooserDialog
49  * @See_also: #GtkFontChooser, #GtkDialog
50  *
51  * The #GtkFontChooserDialog widget is a dialog box for selecting a font.
52  *
53  * To set the font which is initially selected, use
54  * gtk_font_chooser_dialog_set_font_name().
55  *
56  * To get the selected font use gtk_font_chooser_dialog_get_font_name().
57  *
58  * To change the text which is shown in the preview area, use
59  * gtk_font_chooser_dialog_set_preview_text().
60  *
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.
67  * </refsect2>
68  *
69  * Since: 3.2
70  */
71
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,
74                                                                       GtkBuilder   *builder,
75                                                                       const gchar  *childname);
76
77 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog,
78                          GTK_TYPE_DIALOG,
79                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
80                          gtk_font_chooser_dialog_buildable_interface_init))
81
82 static GtkBuildableIface *parent_buildable_iface;
83
84 static void
85 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
86 {
87   g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
88 }
89
90 static void
91 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
92 {
93   GtkFontChooserDialogPrivate *priv;
94   GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
95   GtkWidget *action_area, *content_area;
96
97   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
98                                                    GTK_TYPE_FONT_CHOOSER_DIALOG,
99                                                    GtkFontChooserDialogPrivate);
100   priv = fontchooserdiag->priv;
101
102   content_area = gtk_dialog_get_content_area (dialog);
103   action_area = gtk_dialog_get_action_area (dialog);
104
105   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
106   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
107   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
108   gtk_box_set_spacing (GTK_BOX (action_area), 6);
109
110   gtk_widget_push_composite_child ();
111
112   gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
113
114   /* Create the content area */
115   priv->fontchooser = gtk_font_chooser_new ();
116   gtk_container_set_border_width (GTK_CONTAINER (priv->fontchooser), 5);
117   gtk_widget_show (priv->fontchooser);
118   gtk_box_pack_start (GTK_BOX (content_area),
119                       priv->fontchooser, TRUE, TRUE, 0);
120
121   /* Create the action area */
122   priv->cancel_button = gtk_dialog_add_button (dialog,
123                                                GTK_STOCK_CANCEL,
124                                                GTK_RESPONSE_CANCEL);
125   priv->select_button = gtk_dialog_add_button (dialog,
126                                                _("Select"),
127                                                GTK_RESPONSE_OK);
128   gtk_widget_grab_default (priv->select_button);
129
130   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
131              GTK_RESPONSE_OK,
132              GTK_RESPONSE_CANCEL,
133              -1);
134
135   gtk_window_set_title (GTK_WINDOW (fontchooserdiag),
136                         _("Font Selection"));
137
138   gtk_widget_pop_composite_child ();
139 }
140
141 /**
142  * gtk_font_chooser_dialog_new:
143  * @title: (allow-none): Title of the dialog, or %NULL
144  * @parent: (allow-none): Trasient parent of the dialog, or %NULL
145  *
146  * Creates a new #GtkFontChooserDialog.
147  *
148  * Return value: a new #GtkFontChooserDialog
149  *
150  * Since: 3.2
151  */
152 GtkWidget*
153 gtk_font_chooser_dialog_new (const gchar *title,
154                              GtkWindow   *parent)
155 {
156   GtkFontChooserDialog *dialog;
157
158   dialog = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG,
159                          "title", title,
160                          "transient-for", parent,
161                          NULL);
162
163   return GTK_WIDGET (dialog);
164 }
165
166 /**
167  * gtk_font_chooser_dialog_get_font_chooser:
168  * @fcd: a #GtkFontChooserDialog
169  *
170  * Retrieves the #GtkFontChooser widget embedded in the dialog.
171  *
172  * Returns: (transfer none): the embedded #GtkFontChooser
173  *
174  * Since: 3.2
175  **/
176 GtkWidget*
177 gtk_font_chooser_dialog_get_font_chooser (GtkFontChooserDialog *fcd)
178 {
179   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
180
181   return fcd->priv->fontchooser;
182 }
183
184 static void
185 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
186 {
187   parent_buildable_iface = g_type_interface_peek_parent (iface);
188   iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
189 }
190
191 static GObject *
192 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
193               GtkBuilder   *builder,
194               const gchar  *childname)
195 {
196   GtkFontChooserDialogPrivate *priv;
197
198   priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
199
200   if (g_strcmp0 (childname, "select_button") == 0)
201     return G_OBJECT (priv->select_button);
202   else if (g_strcmp0 (childname, "cancel_button") == 0)
203     return G_OBJECT (priv->cancel_button);
204   else if (g_strcmp0 (childname, "font_chooser") == 0)
205     return G_OBJECT (priv->fontchooser);
206
207   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
208 }
209
210 /**
211  * gtk_font_chooser_dialog_get_font_name:
212  * @fcd: a #GtkFontChooserDialog
213  * 
214  * Gets the currently-selected font name.
215  *
216  * Note that this can be a different string than what you set with 
217  * gtk_font_chooser_dialog_set_font_name(), as the font chooser widget
218  * may normalize font names and thus return a string with a different 
219  * structure. For example, "Helvetica Italic Bold 12" could be normalized 
220  * to "Helvetica Bold Italic 12".  Use pango_font_description_equal()
221  * if you want to compare two font descriptions.
222  * 
223  * Return value: A string with the name of the current font, or %NULL if no 
224  *     font is selected. You must free this string with g_free().
225  *
226  * Since: 3.2
227  */
228 gchar*
229 gtk_font_chooser_dialog_get_font_name (GtkFontChooserDialog *fcd)
230 {
231   GtkFontChooserDialogPrivate *priv;
232
233   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
234
235   priv = fcd->priv;
236
237   return gtk_font_chooser_get_font_name (GTK_FONT_CHOOSER (priv->fontchooser));
238 }
239
240 /**
241  * gtk_font_chooser_dialog_set_font_name:
242  * @fcd: a #GtkFontChooserDialog
243  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
244  *
245  * Sets the currently selected font. 
246  * 
247  * Return value: %TRUE if the font selected in @fcd is now the
248  *     @fontname specified, %FALSE otherwise. 
249  *
250  * Since: 3.2
251  */
252 gboolean
253 gtk_font_chooser_dialog_set_font_name (GtkFontChooserDialog *fcd,
254            const gchar          *fontname)
255 {
256   GtkFontChooserDialogPrivate *priv;
257
258   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), FALSE);
259   g_return_val_if_fail (fontname, FALSE);
260
261   priv = fcd->priv;
262
263   return gtk_font_chooser_set_font_name (GTK_FONT_CHOOSER (priv->fontchooser), fontname);
264 }
265
266 /**
267  * gtk_font_chooser_dialog_get_preview_text:
268  * @fcd: a #GtkFontChooserDialog
269  *
270  * Gets the text displayed in the preview area.
271  * 
272  * Return value: the text displayed in the preview area. 
273  *     This string is owned by the widget and should not be 
274  *     modified or freed 
275  *
276  * Since: 3.2
277  */
278 const gchar*
279 gtk_font_chooser_dialog_get_preview_text (GtkFontChooserDialog *fcd)
280 {
281   GtkFontChooserDialogPrivate *priv;
282
283   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
284
285   priv = fcd->priv;
286
287   return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->fontchooser));
288 }
289
290 /**
291  * gtk_font_chooser_dialog_set_preview_text:
292  * @fcd: a #GtkFontChooserDialog
293  * @text: the text to display in the preview area
294  *
295  * Sets the text displayed in the preview area. 
296  *
297  * Since: 3.2
298  */
299 void
300 gtk_font_chooser_dialog_set_preview_text (GtkFontChooserDialog *fcd,
301               const gchar            *text)
302 {
303   GtkFontChooserDialogPrivate *priv;
304
305   g_return_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd));
306   g_return_if_fail (text != NULL);
307
308   priv = fcd->priv;
309
310   gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->fontchooser), text);
311 }