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