]> Pileus Git - ~andy/gtk/blob - gtk/gtkfontchooser.c
gtkwindow: Clean up gtk_window_map()
[~andy/gtk] / gtk / gtkfontchooser.c
1 /* GTK - The GIMP Toolkit
2  * gtkfontchooser.c - Abstract interface for font file selectors GUIs
3  *
4  * Copyright (C) 2006, Emmanuele Bassi
5  * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include "gtkfontchooser.h"
26 #include "gtkfontchooserprivate.h"
27 #include "gtkintl.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
30
31 /**
32  * SECTION:gtkfontchooser
33  * @Short_description: Interface implemented by widgets displaying fonts
34  * @Title: GtkFontChooser
35  * @See_also: #GtkFontChooserDialog, #GtkFontChooserWidget, #GtkFontButton
36  *
37  * #GtkFontChooser is an interface that can be implemented by widgets
38  * displaying the list of fonts.  In GTK+, the main objects
39  * that implement this interface are #GtkFontChooserWidget,
40  * #GtkFontChooserDialog and #GtkFontButton.
41  *
42  * Since: 3.2
43  */
44
45 enum
46 {
47   SIGNAL_FONT_ACTIVATED,
48   LAST_SIGNAL
49 };
50
51 static guint chooser_signals[LAST_SIGNAL];
52
53 typedef GtkFontChooserIface GtkFontChooserInterface;
54 G_DEFINE_INTERFACE (GtkFontChooser, gtk_font_chooser, G_TYPE_OBJECT);
55
56 static void
57 gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
58 {
59   /**
60    * GtkFontChooser:font:
61    *
62    * The font description as a string, e.g. "Sans Italic 12".
63    */
64   g_object_interface_install_property
65      (iface,
66       g_param_spec_string ("font",
67                           P_("Font"),
68                            P_("Font description as a string, e.g. \"Sans Italic 12\""),
69                            GTK_FONT_CHOOSER_DEFAULT_FONT_NAME,
70                            GTK_PARAM_READWRITE));
71
72   /**
73    * GtkFontChooser:font-desc:
74    *
75    * The font description as a #PangoFontDescription.
76    */
77   g_object_interface_install_property
78      (iface,
79       g_param_spec_boxed ("font-desc",
80                           P_("Font description"),
81                           P_("Font description as a PangoFontDescription struct"),
82                           PANGO_TYPE_FONT_DESCRIPTION,
83                           GTK_PARAM_READWRITE));
84
85   /**
86    * GtkFontChooser:preview-text:
87    *
88    * The string with which to preview the font.
89    */
90   g_object_interface_install_property
91      (iface,
92       g_param_spec_string ("preview-text",
93                           P_("Preview text"),
94                           P_("The text to display in order to demonstrate the selected font"),
95                           pango_language_get_sample_string (NULL),
96                           GTK_PARAM_READWRITE));
97
98   /**
99    * GtkFontChooser:show-preview-entry:
100    *
101    * Whether to show an entry to change the preview text.
102    */
103   g_object_interface_install_property
104      (iface,
105       g_param_spec_boolean ("show-preview-entry",
106                           P_("Show preview text entry"),
107                           P_("Whether the preview text entry is shown or not"),
108                           TRUE,
109                           GTK_PARAM_READWRITE));
110
111   /**
112    * GtkFontChooserWidget::font-activated:
113    * @self: the object which received the signal
114    * @fontname: the font name
115    *
116    * Emitted when a font is activated from the widget's list.
117    * This usually happens when the user double clicks an item,
118    * or an item is selected and the user presses one of the keys
119    * Space, Shift+Space, Return or Enter.
120     */
121   chooser_signals[SIGNAL_FONT_ACTIVATED] =
122     g_signal_new ("font-activated",
123                   GTK_TYPE_FONT_CHOOSER,
124                   G_SIGNAL_RUN_FIRST,
125                   G_STRUCT_OFFSET (GtkFontChooserIface, font_activated),
126                   NULL, NULL,
127                   NULL,
128                   G_TYPE_NONE,
129                   1, G_TYPE_STRING);
130 }
131
132 /**
133  * gtk_font_chooser_get_font_family:
134  * @fontchooser: a #GtkFontChooser
135  *
136  * Gets the #PangoFontFamily representing the selected font family.
137  * Font families are a collection of font faces.
138  *
139  * If the selected font is not installed, returns %NULL.
140  *
141  * Return value: (transfer none): A #PangoFontFamily representing the
142  *     selected font family, or %NULL. The returned object is owned by @fontchooser
143  *     and must not be modified or freed.
144  *
145  * Since: 3.2
146  */
147 PangoFontFamily *
148 gtk_font_chooser_get_font_family (GtkFontChooser *fontchooser)
149 {
150   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
151
152   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_family (fontchooser);
153 }
154
155 /**
156  * gtk_font_chooser_get_font_face:
157  * @fontchooser: a #GtkFontChooser
158  *
159  * Gets the #PangoFontFace representing the selected font group
160  * details (i.e. family, slant, weight, width, etc).
161  *
162  * If the selected font is not installed, returns %NULL.
163  *
164  * Return value: (transfer none): A #PangoFontFace representing the
165  *     selected font group details, or %NULL. The returned object is owned by
166  *     @fontchooser and must not be modified or freed.
167  *
168  * Since: 3.2
169  */
170 PangoFontFace *
171 gtk_font_chooser_get_font_face (GtkFontChooser *fontchooser)
172 {
173   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
174
175   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_face (fontchooser);
176 }
177
178 /**
179  * gtk_font_chooser_get_font_size:
180  * @fontchooser: a #GtkFontChooser
181  *
182  * The selected font size.
183  *
184  * Return value: A n integer representing the selected font size,
185  *     or -1 if no font size is selected.
186  *
187  * Since: 3.2
188  */
189 gint
190 gtk_font_chooser_get_font_size (GtkFontChooser *fontchooser)
191 {
192   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
193
194   return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_size (fontchooser);
195 }
196
197 /**
198  * gtk_font_chooser_get_font:
199  * @fontchooser: a #GtkFontChooser
200  *
201  * Gets the currently-selected font name.
202  *
203  * Note that this can be a different string than what you set with
204  * gtk_font_chooser_set_font(), as the font chooser widget may
205  * normalize font names and thus return a string with a different
206  * structure. For example, "Helvetica Italic Bold 12" could be
207  * normalized to "Helvetica Bold Italic 12".
208  *
209  * Use pango_font_description_equal() if you want to compare two
210  * font descriptions.
211  *
212  * Return value: (transfer full) (allow-none): A string with the name
213  *     of the current font, or %NULL if  no font is selected. You must
214  *     free this string with g_free().
215  *
216  * Since: 3.2
217  */
218 gchar *
219 gtk_font_chooser_get_font (GtkFontChooser *fontchooser)
220 {
221   gchar *fontname;
222
223   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
224
225   g_object_get (fontchooser, "font", &fontname, NULL);
226
227
228   return fontname;
229 }
230
231 /**
232  * gtk_font_chooser_set_font:
233  * @fontchooser: a #GtkFontChooser
234  * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
235  *
236  * Sets the currently-selected font.
237  *
238  * Since: 3.2
239  */
240 void
241 gtk_font_chooser_set_font (GtkFontChooser *fontchooser,
242                            const gchar    *fontname)
243 {
244   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
245   g_return_if_fail (fontname != NULL);
246
247   g_object_set (fontchooser, "font", fontname, NULL);
248 }
249
250 /**
251  * gtk_font_chooser_get_font_desc:
252  * @fontchooser: a #GtkFontChooser
253  *
254  * Gets the currently-selected font.
255  *
256  * Note that this can be a different string than what you set with
257  * gtk_font_chooser_set_font(), as the font chooser widget may
258  * normalize font names and thus return a string with a different
259  * structure. For example, "Helvetica Italic Bold 12" could be
260  * normalized to "Helvetica Bold Italic 12".
261  *
262  * Use pango_font_description_equal() if you want to compare two
263  * font descriptions.
264  *
265  * Return value: (transfer full) (allow-none): A #PangoFontDescription for the
266  *     current font, or %NULL if  no font is selected.
267  *
268  * Since: 3.2
269  */
270 PangoFontDescription *
271 gtk_font_chooser_get_font_desc (GtkFontChooser *fontchooser)
272 {
273   PangoFontDescription *font_desc;
274
275   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
276
277   g_object_get (fontchooser, "font-desc", &font_desc, NULL);
278
279   return font_desc;
280 }
281
282 /**
283  * gtk_font_chooser_set_font_desc:
284  * @fontchooser: a #GtkFontChooser
285  * @font_desc: a #PangoFontDescription
286  *
287  * Sets the currently-selected font from @font_desc.
288  *
289  * Since: 3.2
290  */
291 void
292 gtk_font_chooser_set_font_desc (GtkFontChooser             *fontchooser,
293                                 const PangoFontDescription *font_desc)
294 {
295   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
296   g_return_if_fail (font_desc != NULL);
297
298   g_object_set (fontchooser, "font-desc", font_desc, NULL);
299 }
300
301 /**
302  * gtk_font_chooser_get_preview_text:
303  * @fontchooser: a #GtkFontChooser
304  *
305  * Gets the text displayed in the preview area.
306  *
307  * Return value: (transfer full): the text displayed in the
308  *     preview area
309  *
310  * Since: 3.2
311  */
312 gchar *
313 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
314 {
315   char *text;
316
317   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
318
319   g_object_get (fontchooser, "preview-text", &text, NULL);
320
321   return text;
322 }
323
324 /**
325  * gtk_font_chooser_set_preview_text:
326  * @fontchooser: a #GtkFontChooser
327  * @text: (transfer none): the text to display in the preview area
328  *
329  * Sets the text displayed in the preview area.
330  * The @text is used to show how the selected font looks.
331  *
332  * Since: 3.2
333  */
334 void
335 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
336                                    const gchar    *text)
337 {
338   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
339   g_return_if_fail (text != NULL);
340
341   g_object_set (fontchooser, "preview-text", text, NULL);
342 }
343
344 /**
345  * gtk_font_chooser_get_show_preview_entry:
346  * @fontchooser: a #GtkFontChooser
347  *
348  * Returns whether the preview entry is shown or not.
349  *
350  * Return value: %TRUE if the preview entry is shown
351  *     or %FALSE if it is hidden.
352  *
353  * Since: 3.2
354  */
355 gboolean
356 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
357 {
358   gboolean show;
359
360   g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
361
362   g_object_get (fontchooser, "show-preview-entry", &show, NULL);
363
364   return show;
365 }
366
367 /**
368  * gtk_font_chooser_set_show_preview_entry:
369  * @fontchooser: a #GtkFontChooser
370  * @show_preview_entry: whether to show the editable preview entry or not
371  *
372  * Shows or hides the editable preview entry.
373  *
374  * Since: 3.2
375  */
376 void
377 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
378                                          gboolean        show_preview_entry)
379 {
380   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
381
382   show_preview_entry = show_preview_entry != FALSE;
383   g_object_set (fontchooser, "show-preview-entry", show_preview_entry, NULL);
384 }
385
386 /**
387  * gtk_font_chooser_set_filter_func:
388  * @fontchooser: a #GtkFontChooser
389  * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
390  * @user_data: data to pass to @filter
391  * @destroy: function to call to free @data when it is no longer needed
392  *
393  * Adds a filter function that decides which fonts to display
394  * in the font chooser.
395  *
396  * Since: 3.2
397  */
398 void
399 gtk_font_chooser_set_filter_func (GtkFontChooser   *fontchooser,
400                                   GtkFontFilterFunc filter,
401                                   gpointer          user_data,
402                                   GDestroyNotify    destroy)
403 {
404   g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
405
406   GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_filter_func (fontchooser,
407                                                              filter,
408                                                              user_data,
409                                                              destroy);
410 }
411
412 void
413 _gtk_font_chooser_font_activated (GtkFontChooser *chooser,
414                                   const gchar    *fontname)
415 {
416   g_return_if_fail (GTK_IS_FONT_CHOOSER (chooser));
417
418   g_signal_emit (chooser, chooser_signals[SIGNAL_FONT_ACTIVATED], 0, fontname);
419 }