]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooserdialog.c
GtkFontChooser: Use const instead of G_CONST_RETURN
[~andy/gtk] / gtk / gtkfontchooserdialog.c
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
4  *
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.
10  *
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.
15  *
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.
20  *
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.
25  */
26
27 #include "config.h"
28
29 #include <stdlib.h>
30 #include <glib/gprintf.h>
31 #include <string.h>
32
33 #include <atk/atk.h>
34
35 #include "gtkfontchooserdialog.h"
36 #include "gtkfontchooser.h"
37 #include "gtkstock.h"
38 #include "gtkintl.h"
39 #include "gtkaccessible.h"
40 #include "gtkbuildable.h"
41 #include "gtkprivate.h"
42 #include "gtkwidget.h"
43
44 struct _GtkFontChooserDialogPrivate
45 {
46   GtkWidget *fontchooser;
47
48   GtkWidget *select_button;
49   GtkWidget *cancel_button;
50 };
51
52 /**
53  * SECTION:gtkfontchooserdlg
54  * @Short_description: A dialog box for selecting fonts
55  * @Title: GtkFontChooserDialog
56  * @See_also: #GtkFontChooser, #GtkDialog
57  *
58  * The #GtkFontChooserDialog widget is a dialog box for selecting a font.
59  *
60  * To set the font which is initially selected, use
61  * gtk_font_chooser_dialog_set_font_name().
62  *
63  * To get the selected font use gtk_font_chooser_dialog_get_font_name().
64  *
65  * To change the text which is shown in the preview area, use
66  * gtk_font_chooser_dialog_set_preview_text().
67  *
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.
74  * </refsect2>
75  *
76  * Since: 3.2
77  */
78
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,
81                                                                       GtkBuilder   *builder,
82                                                                       const gchar  *childname);
83
84 G_DEFINE_TYPE_WITH_CODE (GtkFontChooserDialog, gtk_font_chooser_dialog,
85                          GTK_TYPE_DIALOG,
86                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
87                          gtk_font_chooser_dialog_buildable_interface_init))
88
89 static GtkBuildableIface *parent_buildable_iface;
90
91 static void
92 gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
93 {
94   g_type_class_add_private (klass, sizeof (GtkFontChooserDialogPrivate));
95 }
96
97 static void
98 gtk_font_chooser_dialog_init (GtkFontChooserDialog *fontchooserdiag)
99 {
100   GtkFontChooserDialogPrivate *priv;
101   GtkDialog *dialog = GTK_DIALOG (fontchooserdiag);
102   GtkWidget *action_area, *content_area;
103
104   fontchooserdiag->priv = G_TYPE_INSTANCE_GET_PRIVATE (fontchooserdiag,
105                                                    GTK_TYPE_FONT_CHOOSER_DIALOG,
106                                                    GtkFontChooserDialogPrivate);
107   priv = fontchooserdiag->priv;
108
109   content_area = gtk_dialog_get_content_area (dialog);
110   action_area = gtk_dialog_get_action_area (dialog);
111
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);
116
117   gtk_widget_push_composite_child ();
118
119   gtk_window_set_resizable (GTK_WINDOW (fontchooserdiag), TRUE);
120
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);
127
128   /* Create the action area */
129   priv->cancel_button = gtk_dialog_add_button (dialog,
130                                                GTK_STOCK_CANCEL,
131                                                GTK_RESPONSE_CANCEL);
132   priv->select_button = gtk_dialog_add_button (dialog,
133                                                _("Select"),
134                                                GTK_RESPONSE_OK);
135   gtk_widget_grab_default (priv->select_button);
136
137   gtk_dialog_set_alternative_button_order (GTK_DIALOG (fontchooserdiag),
138              GTK_RESPONSE_OK,
139              GTK_RESPONSE_CANCEL,
140              -1);
141
142   gtk_window_set_title (GTK_WINDOW (fontchooserdiag),
143                         _("Font Selection"));
144
145   gtk_widget_pop_composite_child ();
146 }
147
148 /**
149  * gtk_font_chooser_dialog_new:
150  * @title: (allow-none): the title of the dialog window 
151  *
152  * Creates a new #GtkFontChooserDialog.
153  *
154  * Return value: a new #GtkFontChooserDialog
155  *
156  * Since: 3.2
157  */
158 GtkWidget*
159 gtk_font_chooser_dialog_new (const gchar *title)
160 {
161   GtkFontChooserDialog *fontchooserdiag;
162   
163   fontchooserdiag = g_object_new (GTK_TYPE_FONT_CHOOSER_DIALOG, NULL);
164
165   if (title)
166     gtk_window_set_title (GTK_WINDOW (fontchooserdiag), title);
167   
168   return GTK_WIDGET (fontchooserdiag);
169 }
170
171 /**
172  * gtk_font_chooser_dialog_get_font_chooser:
173  * @fcd: a #GtkFontChooserDialog
174  *
175  * Retrieves the #GtkFontChooser widget embedded in the dialog.
176  *
177  * Returns: (transfer none): the embedded #GtkFontChooser
178  *
179  * Since: 3.2
180  **/
181 GtkWidget*
182 gtk_font_chooser_dialog_get_font_chooser (GtkFontChooserDialog *fcd)
183 {
184   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
185
186   return fcd->priv->fontchooser;
187 }
188
189 static void
190 gtk_font_chooser_dialog_buildable_interface_init (GtkBuildableIface *iface)
191 {
192   parent_buildable_iface = g_type_interface_peek_parent (iface);
193   iface->get_internal_child = gtk_font_chooser_dialog_buildable_get_internal_child;
194 }
195
196 static GObject *
197 gtk_font_chooser_dialog_buildable_get_internal_child (GtkBuildable *buildable,
198               GtkBuilder   *builder,
199               const gchar  *childname)
200 {
201   GtkFontChooserDialogPrivate *priv;
202
203   priv = GTK_FONT_CHOOSER_DIALOG (buildable)->priv;
204
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);
211
212   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
213 }
214
215 /**
216  * gtk_font_chooser_dialog_get_font_name:
217  * @fcd: a #GtkFontChooserDialog
218  * 
219  * Gets the currently-selected font name.
220  *
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.
227  * 
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().
230  *
231  * Since: 3.2
232  */
233 gchar*
234 gtk_font_chooser_dialog_get_font_name (GtkFontChooserDialog *fcd)
235 {
236   GtkFontChooserDialogPrivate *priv;
237
238   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
239
240   priv = fcd->priv;
241
242   return gtk_font_chooser_get_font_name (GTK_FONT_CHOOSER (priv->fontchooser));
243 }
244
245 /**
246  * gtk_font_chooser_dialog_set_font_name:
247  * @fcd: a #GtkFontChooserDialog
248  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
249  *
250  * Sets the currently selected font. 
251  * 
252  * Return value: %TRUE if the font selected in @fcd is now the
253  *     @fontname specified, %FALSE otherwise. 
254  *
255  * Since: 3.2
256  */
257 gboolean
258 gtk_font_chooser_dialog_set_font_name (GtkFontChooserDialog *fcd,
259            const gchar          *fontname)
260 {
261   GtkFontChooserDialogPrivate *priv;
262
263   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), FALSE);
264   g_return_val_if_fail (fontname, FALSE);
265
266   priv = fcd->priv;
267
268   return gtk_font_chooser_set_font_name (GTK_FONT_CHOOSER (priv->fontchooser), fontname);
269 }
270
271 /**
272  * gtk_font_chooser_dialog_get_preview_text:
273  * @fcd: a #GtkFontChooserDialog
274  *
275  * Gets the text displayed in the preview area.
276  * 
277  * Return value: the text displayed in the preview area. 
278  *     This string is owned by the widget and should not be 
279  *     modified or freed 
280  *
281  * Since: 3.2
282  */
283 const gchar*
284 gtk_font_chooser_dialog_get_preview_text (GtkFontChooserDialog *fcd)
285 {
286   GtkFontChooserDialogPrivate *priv;
287
288   g_return_val_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd), NULL);
289
290   priv = fcd->priv;
291
292   return gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (priv->fontchooser));
293 }
294
295 /**
296  * gtk_font_chooser_dialog_set_preview_text:
297  * @fcd: a #GtkFontChooserDialog
298  * @text: the text to display in the preview area
299  *
300  * Sets the text displayed in the preview area. 
301  *
302  * Since: 3.2
303  */
304 void
305 gtk_font_chooser_dialog_set_preview_text (GtkFontChooserDialog *fcd,
306               const gchar            *text)
307 {
308   GtkFontChooserDialogPrivate *priv;
309
310   g_return_if_fail (GTK_IS_FONT_CHOOSER_DIALOG (fcd));
311   g_return_if_fail (text != NULL);
312
313   priv = fcd->priv;
314
315   gtk_font_chooser_set_preview_text (GTK_FONT_CHOOSER (priv->fontchooser), text);
316 }