1 /* GTK - The GIMP Toolkit
2 * gtkfontchooser.c - Abstract interface for font file selectors GUIs
4 * Copyright (C) 2006, Emmanuele Bassi
5 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
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.
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.
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.
25 #include "gtkfontchooser.h"
26 #include "gtkfontchooserprivate.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
32 * SECTION:gtkfontchooser
33 * @Short_description: Interface implemented by widgets displaying fonts
34 * @Title: GtkFontChooser
35 * @See_also: #GtkFontChooserDialog, #GtkFontChooserWidget, #GtkFontButton
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.
47 SIGNAL_FONT_ACTIVATED,
51 static guint chooser_signals[LAST_SIGNAL];
53 typedef GtkFontChooserIface GtkFontChooserInterface;
54 G_DEFINE_INTERFACE (GtkFontChooser, gtk_font_chooser, G_TYPE_OBJECT);
57 gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
60 * GtkFontChooser:font:
62 * The font description as a string, e.g. "Sans Italic 12".
64 g_object_interface_install_property
66 g_param_spec_string ("font",
68 P_("Font description as a string, e.g. \"Sans Italic 12\""),
69 GTK_FONT_CHOOSER_DEFAULT_FONT_NAME,
70 GTK_PARAM_READWRITE));
73 * GtkFontChooser:font-desc:
75 * The font description as a #PangoFontDescription.
77 g_object_interface_install_property
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));
86 * GtkFontChooser:preview-text:
88 * The string with which to preview the font.
90 g_object_interface_install_property
92 g_param_spec_string ("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));
99 * GtkFontChooser:show-preview-entry:
101 * Whether to show an entry to change the preview text.
103 g_object_interface_install_property
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"),
109 GTK_PARAM_READWRITE));
112 * GtkFontChooserWidget::font-activated:
113 * @self: the object which received the signal
114 * @fontname: the font name
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.
121 chooser_signals[SIGNAL_FONT_ACTIVATED] =
122 g_signal_new ("font-activated",
123 GTK_TYPE_FONT_CHOOSER,
125 G_STRUCT_OFFSET (GtkFontChooserIface, font_activated),
133 * gtk_font_chooser_get_font_family:
134 * @fontchooser: a #GtkFontChooser
136 * Gets the #PangoFontFamily representing the selected font family.
137 * Font families are a collection of font faces.
139 * If the selected font is not installed, returns %NULL.
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.
148 gtk_font_chooser_get_font_family (GtkFontChooser *fontchooser)
150 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
152 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_family (fontchooser);
156 * gtk_font_chooser_get_font_face:
157 * @fontchooser: a #GtkFontChooser
159 * Gets the #PangoFontFace representing the selected font group
160 * details (i.e. family, slant, weight, width, etc).
162 * If the selected font is not installed, returns %NULL.
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.
171 gtk_font_chooser_get_font_face (GtkFontChooser *fontchooser)
173 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
175 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_face (fontchooser);
179 * gtk_font_chooser_get_font_size:
180 * @fontchooser: a #GtkFontChooser
182 * The selected font size.
184 * Return value: A n integer representing the selected font size,
185 * or -1 if no font size is selected.
190 gtk_font_chooser_get_font_size (GtkFontChooser *fontchooser)
192 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
194 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_size (fontchooser);
198 * gtk_font_chooser_get_font:
199 * @fontchooser: a #GtkFontChooser
201 * Gets the currently-selected font name.
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".
209 * Use pango_font_description_equal() if you want to compare two
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().
219 gtk_font_chooser_get_font (GtkFontChooser *fontchooser)
223 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
225 g_object_get (fontchooser, "font", &fontname, NULL);
232 * gtk_font_chooser_set_font:
233 * @fontchooser: a #GtkFontChooser
234 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
236 * Sets the currently-selected font.
241 gtk_font_chooser_set_font (GtkFontChooser *fontchooser,
242 const gchar *fontname)
244 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
245 g_return_if_fail (fontname != NULL);
247 g_object_set (fontchooser, "font", fontname, NULL);
251 * gtk_font_chooser_get_font_desc:
252 * @fontchooser: a #GtkFontChooser
254 * Gets the currently-selected font.
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".
262 * Use pango_font_description_equal() if you want to compare two
265 * Return value: (transfer full) (allow-none): A #PangoFontDescription for the
266 * current font, or %NULL if no font is selected.
270 PangoFontDescription *
271 gtk_font_chooser_get_font_desc (GtkFontChooser *fontchooser)
273 PangoFontDescription *font_desc;
275 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
277 g_object_get (fontchooser, "font-desc", &font_desc, NULL);
283 * gtk_font_chooser_set_font_desc:
284 * @fontchooser: a #GtkFontChooser
285 * @font_desc: a #PangoFontDescription
287 * Sets the currently-selected font from @font_desc.
292 gtk_font_chooser_set_font_desc (GtkFontChooser *fontchooser,
293 const PangoFontDescription *font_desc)
295 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
296 g_return_if_fail (font_desc != NULL);
298 g_object_set (fontchooser, "font-desc", font_desc, NULL);
302 * gtk_font_chooser_get_preview_text:
303 * @fontchooser: a #GtkFontChooser
305 * Gets the text displayed in the preview area.
307 * Return value: (transfer full): the text displayed in the
313 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
317 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
319 g_object_get (fontchooser, "preview-text", &text, NULL);
325 * gtk_font_chooser_set_preview_text:
326 * @fontchooser: a #GtkFontChooser
327 * @text: (transfer none): the text to display in the preview area
329 * Sets the text displayed in the preview area.
330 * The @text is used to show how the selected font looks.
335 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
338 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
339 g_return_if_fail (text != NULL);
341 g_object_set (fontchooser, "preview-text", text, NULL);
345 * gtk_font_chooser_get_show_preview_entry:
346 * @fontchooser: a #GtkFontChooser
348 * Returns whether the preview entry is shown or not.
350 * Return value: %TRUE if the preview entry is shown
351 * or %FALSE if it is hidden.
356 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
360 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
362 g_object_get (fontchooser, "show-preview-entry", &show, NULL);
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
372 * Shows or hides the editable preview entry.
377 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
378 gboolean show_preview_entry)
380 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
382 show_preview_entry = show_preview_entry != FALSE;
383 g_object_set (fontchooser, "show-preview-entry", show_preview_entry, NULL);
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
393 * Adds a filter function that decides which fonts to display
394 * in the font chooser.
399 gtk_font_chooser_set_filter_func (GtkFontChooser *fontchooser,
400 GtkFontFilterFunc filter,
402 GDestroyNotify destroy)
404 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
406 GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_filter_func (fontchooser,
413 _gtk_font_chooser_font_activated (GtkFontChooser *chooser,
414 const gchar *fontname)
416 g_return_if_fail (GTK_IS_FONT_CHOOSER (chooser));
418 g_signal_emit (chooser, chooser_signals[SIGNAL_FONT_ACTIVATED], 0, fontname);