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, see <http://www.gnu.org/licenses/>.
23 #include "gtkfontchooser.h"
24 #include "gtkfontchooserprivate.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkprivate.h"
30 * SECTION:gtkfontchooser
31 * @Short_description: Interface implemented by widgets displaying fonts
32 * @Title: GtkFontChooser
33 * @See_also: #GtkFontChooserDialog, #GtkFontChooserWidget, #GtkFontButton
35 * #GtkFontChooser is an interface that can be implemented by widgets
36 * displaying the list of fonts. In GTK+, the main objects
37 * that implement this interface are #GtkFontChooserWidget,
38 * #GtkFontChooserDialog and #GtkFontButton.
45 SIGNAL_FONT_ACTIVATED,
49 static guint chooser_signals[LAST_SIGNAL];
51 typedef GtkFontChooserIface GtkFontChooserInterface;
52 G_DEFINE_INTERFACE (GtkFontChooser, gtk_font_chooser, G_TYPE_OBJECT);
55 gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
58 * GtkFontChooser:font:
60 * The font description as a string, e.g. "Sans Italic 12".
62 g_object_interface_install_property
64 g_param_spec_string ("font",
66 P_("Font description as a string, e.g. \"Sans Italic 12\""),
67 GTK_FONT_CHOOSER_DEFAULT_FONT_NAME,
68 GTK_PARAM_READWRITE));
71 * GtkFontChooser:font-desc:
73 * The font description as a #PangoFontDescription.
75 g_object_interface_install_property
77 g_param_spec_boxed ("font-desc",
78 P_("Font description"),
79 P_("Font description as a PangoFontDescription struct"),
80 PANGO_TYPE_FONT_DESCRIPTION,
81 GTK_PARAM_READWRITE));
84 * GtkFontChooser:preview-text:
86 * The string with which to preview the font.
88 g_object_interface_install_property
90 g_param_spec_string ("preview-text",
92 P_("The text to display in order to demonstrate the selected font"),
93 pango_language_get_sample_string (NULL),
94 GTK_PARAM_READWRITE));
97 * GtkFontChooser:show-preview-entry:
99 * Whether to show an entry to change the preview text.
101 g_object_interface_install_property
103 g_param_spec_boolean ("show-preview-entry",
104 P_("Show preview text entry"),
105 P_("Whether the preview text entry is shown or not"),
107 GTK_PARAM_READWRITE));
110 * GtkFontChooserWidget::font-activated:
111 * @self: the object which received the signal
112 * @fontname: the font name
114 * Emitted when a font is activated from the widget's list.
115 * This usually happens when the user double clicks an item,
116 * or an item is selected and the user presses one of the keys
117 * Space, Shift+Space, Return or Enter.
119 chooser_signals[SIGNAL_FONT_ACTIVATED] =
120 g_signal_new ("font-activated",
121 GTK_TYPE_FONT_CHOOSER,
123 G_STRUCT_OFFSET (GtkFontChooserIface, font_activated),
131 * gtk_font_chooser_get_font_family:
132 * @fontchooser: a #GtkFontChooser
134 * Gets the #PangoFontFamily representing the selected font family.
135 * Font families are a collection of font faces.
137 * If the selected font is not installed, returns %NULL.
139 * Return value: (transfer none): A #PangoFontFamily representing the
140 * selected font family, or %NULL. The returned object is owned by @fontchooser
141 * and must not be modified or freed.
146 gtk_font_chooser_get_font_family (GtkFontChooser *fontchooser)
148 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
150 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_family (fontchooser);
154 * gtk_font_chooser_get_font_face:
155 * @fontchooser: a #GtkFontChooser
157 * Gets the #PangoFontFace representing the selected font group
158 * details (i.e. family, slant, weight, width, etc).
160 * If the selected font is not installed, returns %NULL.
162 * Return value: (transfer none): A #PangoFontFace representing the
163 * selected font group details, or %NULL. The returned object is owned by
164 * @fontchooser and must not be modified or freed.
169 gtk_font_chooser_get_font_face (GtkFontChooser *fontchooser)
171 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
173 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_face (fontchooser);
177 * gtk_font_chooser_get_font_size:
178 * @fontchooser: a #GtkFontChooser
180 * The selected font size.
182 * Return value: A n integer representing the selected font size,
183 * or -1 if no font size is selected.
188 gtk_font_chooser_get_font_size (GtkFontChooser *fontchooser)
190 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
192 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_size (fontchooser);
196 * gtk_font_chooser_get_font:
197 * @fontchooser: a #GtkFontChooser
199 * Gets the currently-selected font name.
201 * Note that this can be a different string than what you set with
202 * gtk_font_chooser_set_font(), as the font chooser widget may
203 * normalize font names and thus return a string with a different
204 * structure. For example, "Helvetica Italic Bold 12" could be
205 * normalized to "Helvetica Bold Italic 12".
207 * Use pango_font_description_equal() if you want to compare two
210 * Return value: (transfer full) (allow-none): A string with the name
211 * of the current font, or %NULL if no font is selected. You must
212 * free this string with g_free().
217 gtk_font_chooser_get_font (GtkFontChooser *fontchooser)
221 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
223 g_object_get (fontchooser, "font", &fontname, NULL);
230 * gtk_font_chooser_set_font:
231 * @fontchooser: a #GtkFontChooser
232 * @fontname: a font name like "Helvetica 12" or "Times Bold 18"
234 * Sets the currently-selected font.
239 gtk_font_chooser_set_font (GtkFontChooser *fontchooser,
240 const gchar *fontname)
242 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
243 g_return_if_fail (fontname != NULL);
245 g_object_set (fontchooser, "font", fontname, NULL);
249 * gtk_font_chooser_get_font_desc:
250 * @fontchooser: a #GtkFontChooser
252 * Gets the currently-selected font.
254 * Note that this can be a different string than what you set with
255 * gtk_font_chooser_set_font(), as the font chooser widget may
256 * normalize font names and thus return a string with a different
257 * structure. For example, "Helvetica Italic Bold 12" could be
258 * normalized to "Helvetica Bold Italic 12".
260 * Use pango_font_description_equal() if you want to compare two
263 * Return value: (transfer full) (allow-none): A #PangoFontDescription for the
264 * current font, or %NULL if no font is selected.
268 PangoFontDescription *
269 gtk_font_chooser_get_font_desc (GtkFontChooser *fontchooser)
271 PangoFontDescription *font_desc;
273 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
275 g_object_get (fontchooser, "font-desc", &font_desc, NULL);
281 * gtk_font_chooser_set_font_desc:
282 * @fontchooser: a #GtkFontChooser
283 * @font_desc: a #PangoFontDescription
285 * Sets the currently-selected font from @font_desc.
290 gtk_font_chooser_set_font_desc (GtkFontChooser *fontchooser,
291 const PangoFontDescription *font_desc)
293 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
294 g_return_if_fail (font_desc != NULL);
296 g_object_set (fontchooser, "font-desc", font_desc, NULL);
300 * gtk_font_chooser_get_preview_text:
301 * @fontchooser: a #GtkFontChooser
303 * Gets the text displayed in the preview area.
305 * Return value: (transfer full): the text displayed in the
311 gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
315 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
317 g_object_get (fontchooser, "preview-text", &text, NULL);
323 * gtk_font_chooser_set_preview_text:
324 * @fontchooser: a #GtkFontChooser
325 * @text: (transfer none): the text to display in the preview area
327 * Sets the text displayed in the preview area.
328 * The @text is used to show how the selected font looks.
333 gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
336 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
337 g_return_if_fail (text != NULL);
339 g_object_set (fontchooser, "preview-text", text, NULL);
343 * gtk_font_chooser_get_show_preview_entry:
344 * @fontchooser: a #GtkFontChooser
346 * Returns whether the preview entry is shown or not.
348 * Return value: %TRUE if the preview entry is shown
349 * or %FALSE if it is hidden.
354 gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
358 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
360 g_object_get (fontchooser, "show-preview-entry", &show, NULL);
366 * gtk_font_chooser_set_show_preview_entry:
367 * @fontchooser: a #GtkFontChooser
368 * @show_preview_entry: whether to show the editable preview entry or not
370 * Shows or hides the editable preview entry.
375 gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
376 gboolean show_preview_entry)
378 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
380 show_preview_entry = show_preview_entry != FALSE;
381 g_object_set (fontchooser, "show-preview-entry", show_preview_entry, NULL);
385 * gtk_font_chooser_set_filter_func:
386 * @fontchooser: a #GtkFontChooser
387 * @filter: (allow-none): a #GtkFontFilterFunc, or %NULL
388 * @user_data: data to pass to @filter
389 * @destroy: function to call to free @data when it is no longer needed
391 * Adds a filter function that decides which fonts to display
392 * in the font chooser.
397 gtk_font_chooser_set_filter_func (GtkFontChooser *fontchooser,
398 GtkFontFilterFunc filter,
400 GDestroyNotify destroy)
402 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
404 GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_filter_func (fontchooser,
411 _gtk_font_chooser_font_activated (GtkFontChooser *chooser,
412 const gchar *fontname)
414 g_return_if_fail (GTK_IS_FONT_CHOOSER (chooser));
416 g_signal_emit (chooser, chooser_signals[SIGNAL_FONT_ACTIVATED], 0, fontname);