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, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "config.h"
22
23#include "gtkfontchooser.h"
24#include "gtkfontchooserprivate.h"
25#include "gtkintl.h"
26#include "gtktypebuiltins.h"
27#include "gtkprivate.h"
28
29/**
30 * GtkFontChooser:
31 *
32 * `GtkFontChooser` is an interface that can be implemented by widgets
33 * for choosing fonts.
34 *
35 * In GTK, the main objects that implement this interface are
36 * [class@Gtk.FontChooserWidget], [class@Gtk.FontChooserDialog] and
37 * [class@Gtk.FontButton].
38 */
39
40enum
41{
42 SIGNAL_FONT_ACTIVATED,
43 LAST_SIGNAL
44};
45
46static guint chooser_signals[LAST_SIGNAL];
47
48typedef GtkFontChooserIface GtkFontChooserInterface;
49G_DEFINE_INTERFACE (GtkFontChooser, gtk_font_chooser, G_TYPE_OBJECT);
50
51static void
52gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
53{
54 /**
55 * GtkFontChooser:font: (attributes org.gtk.Property.get=gtk_font_chooser_get_font org.gtk.Property.set=gtk_font_chooser_set_font)
56 *
57 * The font description as a string, e.g. "Sans Italic 12".
58 */
59 g_object_interface_install_property
60 (g_iface: iface,
61 pspec: g_param_spec_string (name: "font",
62 P_("Font"),
63 P_("Font description as a string, e.g. “Sans Italic 12”"),
64 GTK_FONT_CHOOSER_DEFAULT_FONT_NAME,
65 GTK_PARAM_READWRITE));
66
67 /**
68 * GtkFontChooser:font-desc: (attributes org.gtk.Property.get=gtk_font_chooser_get_font_desc org.gtk.Property.set=gtk_font_chooser_set_font_desc)
69 *
70 * The font description as a `PangoFontDescription`.
71 */
72 g_object_interface_install_property
73 (g_iface: iface,
74 pspec: g_param_spec_boxed (name: "font-desc",
75 P_("Font description"),
76 P_("Font description as a PangoFontDescription struct"),
77 PANGO_TYPE_FONT_DESCRIPTION,
78 GTK_PARAM_READWRITE));
79
80 /**
81 * GtkFontChooser:preview-text: (attributes org.gtk.Property.get=gtk_font_chooser_get_preview_text org.gtk.Property.set=gtk_font_chooser_set_preview_text)
82 *
83 * The string with which to preview the font.
84 */
85 g_object_interface_install_property
86 (g_iface: iface,
87 pspec: g_param_spec_string (name: "preview-text",
88 P_("Preview text"),
89 P_("The text to display in order to demonstrate the selected font"),
90 default_value: pango_language_get_sample_string (NULL),
91 GTK_PARAM_READWRITE));
92
93 /**
94 * GtkFontChooser:show-preview-entry: (attributes org.gtk.Property.get=gtk_font_chooser_get_show_preview_entry org.gtk.Property.set=gtk_font_chooser_set_show_preview_entry)
95 *
96 * Whether to show an entry to change the preview text.
97 */
98 g_object_interface_install_property
99 (g_iface: iface,
100 pspec: g_param_spec_boolean (name: "show-preview-entry",
101 P_("Show preview text entry"),
102 P_("Whether the preview text entry is shown or not"),
103 TRUE,
104 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
105
106 /**
107 * GtkFontChooser:level: (attributes org.gtk.Property.get=gtk_font_chooser_get_level org.gtk.Property.set=gtk_font_chooser_set_level)
108 *
109 * The level of granularity to offer for selecting fonts.
110 */
111 g_object_interface_install_property
112 (g_iface: iface,
113 pspec: g_param_spec_flags (name: "level",
114 P_("Selection level"),
115 P_("Whether to select family, face or font"),
116 flags_type: GTK_TYPE_FONT_CHOOSER_LEVEL,
117 default_value: GTK_FONT_CHOOSER_LEVEL_FAMILY |
118 GTK_FONT_CHOOSER_LEVEL_STYLE |
119 GTK_FONT_CHOOSER_LEVEL_SIZE,
120 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
121
122 /**
123 * GtkFontChooser:font-features: (attributes org.gtk.Property.get=gtk_font_chooser_get_font_features)
124 *
125 * The selected font features.
126 *
127 * The format of the string is compatible with
128 * CSS and with Pango attributes.
129 */
130 g_object_interface_install_property
131 (g_iface: iface,
132 pspec: g_param_spec_string (name: "font-features",
133 P_("Font features"),
134 P_("Font features as a string"),
135 default_value: "",
136 GTK_PARAM_READABLE));
137
138 /**
139 * GtkFontChooser:language: (attributes org.gtk.Property.get=gtk_font_chooser_get_language org.gtk.Property.set=gtk_font_chooser_set_language)
140 *
141 * The language for which the font features were selected.
142 */
143 g_object_interface_install_property
144 (g_iface: iface,
145 pspec: g_param_spec_string (name: "language",
146 P_("Language"),
147 P_("Language for which features have been selected"),
148 default_value: "",
149 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
150
151 /**
152 * GtkFontChooser::font-activated:
153 * @self: the object which received the signal
154 * @fontname: the font name
155 *
156 * Emitted when a font is activated.
157 *
158 * This usually happens when the user double clicks an item,
159 * or an item is selected and the user presses one of the keys
160 * Space, Shift+Space, Return or Enter.
161 */
162 chooser_signals[SIGNAL_FONT_ACTIVATED] =
163 g_signal_new (I_("font-activated"),
164 GTK_TYPE_FONT_CHOOSER,
165 signal_flags: G_SIGNAL_RUN_FIRST,
166 G_STRUCT_OFFSET (GtkFontChooserIface, font_activated),
167 NULL, NULL,
168 NULL,
169 G_TYPE_NONE,
170 n_params: 1, G_TYPE_STRING);
171}
172
173/**
174 * gtk_font_chooser_get_font_family:
175 * @fontchooser: a `GtkFontChooser`
176 *
177 * Gets the `PangoFontFamily` representing the selected font family.
178 *
179 * Font families are a collection of font faces.
180 *
181 * If the selected font is not installed, returns %NULL.
182 *
183 * Returns: (nullable) (transfer none): A `PangoFontFamily` representing the
184 * selected font family
185 */
186PangoFontFamily *
187gtk_font_chooser_get_font_family (GtkFontChooser *fontchooser)
188{
189 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
190
191 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_family (fontchooser);
192}
193
194/**
195 * gtk_font_chooser_get_font_face:
196 * @fontchooser: a `GtkFontChooser`
197 *
198 * Gets the `PangoFontFace` representing the selected font group
199 * details (i.e. family, slant, weight, width, etc).
200 *
201 * If the selected font is not installed, returns %NULL.
202 *
203 * Returns: (nullable) (transfer none): A `PangoFontFace` representing the
204 * selected font group details
205 */
206PangoFontFace *
207gtk_font_chooser_get_font_face (GtkFontChooser *fontchooser)
208{
209 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
210
211 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_face (fontchooser);
212}
213
214/**
215 * gtk_font_chooser_get_font_size:
216 * @fontchooser: a `GtkFontChooser`
217 *
218 * The selected font size.
219 *
220 * Returns: A n integer representing the selected font size,
221 * or -1 if no font size is selected.
222 */
223int
224gtk_font_chooser_get_font_size (GtkFontChooser *fontchooser)
225{
226 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), -1);
227
228 return GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_size (fontchooser);
229}
230
231/**
232 * gtk_font_chooser_get_font: (attributes org.gtk.Method.get_property=font)
233 * @fontchooser: a `GtkFontChooser`
234 *
235 * Gets the currently-selected font name.
236 *
237 * Note that this can be a different string than what you set with
238 * [method@Gtk.FontChooser.set_font], as the font chooser widget may
239 * normalize font names and thus return a string with a different
240 * structure. For example, “Helvetica Italic Bold 12” could be
241 * normalized to “Helvetica Bold Italic 12”.
242 *
243 * Use [method@Pango.FontDescription.equal] if you want to compare two
244 * font descriptions.
245 *
246 * Returns: (nullable) (transfer full): A string with the name
247 * of the current font
248 */
249char *
250gtk_font_chooser_get_font (GtkFontChooser *fontchooser)
251{
252 char *fontname;
253
254 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
255
256 g_object_get (object: fontchooser, first_property_name: "font", &fontname, NULL);
257
258
259 return fontname;
260}
261
262/**
263 * gtk_font_chooser_set_font: (attributes org.gtk.Method.set_property=font)
264 * @fontchooser: a `GtkFontChooser`
265 * @fontname: a font name like “Helvetica 12” or “Times Bold 18”
266 *
267 * Sets the currently-selected font.
268 */
269void
270gtk_font_chooser_set_font (GtkFontChooser *fontchooser,
271 const char *fontname)
272{
273 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
274 g_return_if_fail (fontname != NULL);
275
276 g_object_set (object: fontchooser, first_property_name: "font", fontname, NULL);
277}
278
279/**
280 * gtk_font_chooser_get_font_desc: (attributes org.gtk.Method.get_property=font-desc)
281 * @fontchooser: a `GtkFontChooser`
282 *
283 * Gets the currently-selected font.
284 *
285 * Note that this can be a different string than what you set with
286 * [method@Gtk.FontChooser.set_font], as the font chooser widget may
287 * normalize font names and thus return a string with a different
288 * structure. For example, “Helvetica Italic Bold 12” could be
289 * normalized to “Helvetica Bold Italic 12”.
290 *
291 * Use [method@Pango.FontDescription.equal] if you want to compare two
292 * font descriptions.
293 *
294 * Returns: (nullable) (transfer full): A `PangoFontDescription` for the
295 * current font
296 */
297PangoFontDescription *
298gtk_font_chooser_get_font_desc (GtkFontChooser *fontchooser)
299{
300 PangoFontDescription *font_desc;
301
302 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
303
304 g_object_get (object: fontchooser, first_property_name: "font-desc", &font_desc, NULL);
305
306 return font_desc;
307}
308
309/**
310 * gtk_font_chooser_set_font_desc: (attributes org.gtk.Method.set_property=font-desc)
311 * @fontchooser: a `GtkFontChooser`
312 * @font_desc: a `PangoFontDescription`
313 *
314 * Sets the currently-selected font from @font_desc.
315 */
316void
317gtk_font_chooser_set_font_desc (GtkFontChooser *fontchooser,
318 const PangoFontDescription *font_desc)
319{
320 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
321 g_return_if_fail (font_desc != NULL);
322
323 g_object_set (object: fontchooser, first_property_name: "font-desc", font_desc, NULL);
324}
325
326/**
327 * gtk_font_chooser_get_preview_text: (attributes org.gtk.Method.get_property=preview-text)
328 * @fontchooser: a `GtkFontChooser`
329 *
330 * Gets the text displayed in the preview area.
331 *
332 * Returns: (transfer full): the text displayed in the preview area
333 */
334char *
335gtk_font_chooser_get_preview_text (GtkFontChooser *fontchooser)
336{
337 char *text;
338
339 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
340
341 g_object_get (object: fontchooser, first_property_name: "preview-text", &text, NULL);
342
343 return text;
344}
345
346/**
347 * gtk_font_chooser_set_preview_text: (attributes org.gtk.Method.set_property=preview-text)
348 * @fontchooser: a `GtkFontChooser`
349 * @text: (transfer none): the text to display in the preview area
350 *
351 * Sets the text displayed in the preview area.
352 *
353 * The @text is used to show how the selected font looks.
354 */
355void
356gtk_font_chooser_set_preview_text (GtkFontChooser *fontchooser,
357 const char *text)
358{
359 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
360 g_return_if_fail (text != NULL);
361
362 g_object_set (object: fontchooser, first_property_name: "preview-text", text, NULL);
363}
364
365/**
366 * gtk_font_chooser_get_show_preview_entry: (attributes org.gtk.Method.get_property=show-preview-entry)
367 * @fontchooser: a `GtkFontChooser`
368 *
369 * Returns whether the preview entry is shown or not.
370 *
371 * Returns: %TRUE if the preview entry is shown or %FALSE if it is hidden.
372 */
373gboolean
374gtk_font_chooser_get_show_preview_entry (GtkFontChooser *fontchooser)
375{
376 gboolean show;
377
378 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), FALSE);
379
380 g_object_get (object: fontchooser, first_property_name: "show-preview-entry", &show, NULL);
381
382 return show;
383}
384
385/**
386 * gtk_font_chooser_set_show_preview_entry: (attributes org.gtk.Method.set_property=show-preview-entry)
387 * @fontchooser: a `GtkFontChooser`
388 * @show_preview_entry: whether to show the editable preview entry or not
389 *
390 * Shows or hides the editable preview entry.
391 */
392void
393gtk_font_chooser_set_show_preview_entry (GtkFontChooser *fontchooser,
394 gboolean show_preview_entry)
395{
396 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
397
398 show_preview_entry = show_preview_entry != FALSE;
399 g_object_set (object: fontchooser, first_property_name: "show-preview-entry", show_preview_entry, NULL);
400}
401
402/**
403 * gtk_font_chooser_set_filter_func:
404 * @fontchooser: a `GtkFontChooser`
405 * @filter: (nullable): a `GtkFontFilterFunc`
406 * @user_data: (closure): data to pass to @filter
407 * @destroy: function to call to free @data when it is no longer needed
408 *
409 * Adds a filter function that decides which fonts to display
410 * in the font chooser.
411 */
412void
413gtk_font_chooser_set_filter_func (GtkFontChooser *fontchooser,
414 GtkFontFilterFunc filter,
415 gpointer user_data,
416 GDestroyNotify destroy)
417{
418 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
419
420 GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_filter_func (fontchooser,
421 filter,
422 user_data,
423 destroy);
424}
425
426void
427_gtk_font_chooser_font_activated (GtkFontChooser *chooser,
428 const char *fontname)
429{
430 g_return_if_fail (GTK_IS_FONT_CHOOSER (chooser));
431
432 g_signal_emit (instance: chooser, signal_id: chooser_signals[SIGNAL_FONT_ACTIVATED], detail: 0, fontname);
433}
434
435/**
436 * gtk_font_chooser_set_font_map:
437 * @fontchooser: a `GtkFontChooser`
438 * @fontmap: (nullable): a `PangoFontMap`
439 *
440 * Sets a custom font map to use for this font chooser widget.
441 *
442 * A custom font map can be used to present application-specific
443 * fonts instead of or in addition to the normal system fonts.
444 *
445 * ```c
446 * FcConfig *config;
447 * PangoFontMap *fontmap;
448 *
449 * config = FcInitLoadConfigAndFonts ();
450 * FcConfigAppFontAddFile (config, my_app_font_file);
451 *
452 * fontmap = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
453 * pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (fontmap), config);
454 *
455 * gtk_font_chooser_set_font_map (font_chooser, fontmap);
456 * ```
457 *
458 * Note that other GTK widgets will only be able to use the
459 * application-specific font if it is present in the font map they use:
460 *
461 * ```c
462 * context = gtk_widget_get_pango_context (label);
463 * pango_context_set_font_map (context, fontmap);
464 * ```
465 */
466void
467gtk_font_chooser_set_font_map (GtkFontChooser *fontchooser,
468 PangoFontMap *fontmap)
469{
470 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
471 g_return_if_fail (fontmap == NULL || PANGO_IS_FONT_MAP (fontmap));
472
473 if (GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_font_map)
474 GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->set_font_map (fontchooser, fontmap);
475}
476
477/**
478 * gtk_font_chooser_get_font_map:
479 * @fontchooser: a `GtkFontChooser`
480 *
481 * Gets the custom font map of this font chooser widget,
482 * or %NULL if it does not have one.
483 *
484 * Returns: (nullable) (transfer full): a `PangoFontMap`
485 */
486PangoFontMap *
487gtk_font_chooser_get_font_map (GtkFontChooser *fontchooser)
488{
489 PangoFontMap *fontmap = NULL;
490
491 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
492
493 if (GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_map)
494 fontmap = GTK_FONT_CHOOSER_GET_IFACE (fontchooser)->get_font_map (fontchooser);
495
496 return fontmap;
497}
498
499/**
500 * gtk_font_chooser_set_level: (attributes org.gtk.Method.set_property=level)
501 * @fontchooser: a `GtkFontChooser`
502 * @level: the desired level of granularity
503 *
504 * Sets the desired level of granularity for selecting fonts.
505 */
506void
507gtk_font_chooser_set_level (GtkFontChooser *fontchooser,
508 GtkFontChooserLevel level)
509{
510 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
511
512 g_object_set (object: fontchooser, first_property_name: "level", level, NULL);
513}
514
515/**
516 * gtk_font_chooser_get_level: (attributes org.gtk.Method.get_property=level)
517 * @fontchooser: a `GtkFontChooser`
518 *
519 * Returns the current level of granularity for selecting fonts.
520 *
521 * Returns: the current granularity level
522 */
523GtkFontChooserLevel
524gtk_font_chooser_get_level (GtkFontChooser *fontchooser)
525{
526 GtkFontChooserLevel level;
527
528 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), 0);
529
530 g_object_get (object: fontchooser, first_property_name: "level", &level, NULL);
531
532 return level;
533}
534
535/**
536 * gtk_font_chooser_get_font_features: (attributes org.gtk.Method.get_property=font-features)
537 * @fontchooser: a `GtkFontChooser`
538 *
539 * Gets the currently-selected font features.
540 *
541 * Returns: the currently selected font features
542 */
543char *
544gtk_font_chooser_get_font_features (GtkFontChooser *fontchooser)
545{
546 char *text;
547
548 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
549
550 g_object_get (object: fontchooser, first_property_name: "font-features", &text, NULL);
551
552 return text;
553}
554
555/**
556 * gtk_font_chooser_get_language: (attributes org.gtk.Method.get_property=language)
557 * @fontchooser: a `GtkFontChooser`
558 *
559 * Gets the language that is used for font features.
560 *
561 * Returns: the currently selected language
562 */
563char *
564gtk_font_chooser_get_language (GtkFontChooser *fontchooser)
565{
566 char *text;
567
568 g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
569
570 g_object_get (object: fontchooser, first_property_name: "language", &text, NULL);
571
572 return text;
573}
574
575/**
576 * gtk_font_chooser_set_language: (attributes org.gtk.Method.set_property=language)
577 * @fontchooser: a `GtkFontChooser`
578 * @language: a language
579 *
580 * Sets the language to use for font features.
581 */
582void
583gtk_font_chooser_set_language (GtkFontChooser *fontchooser,
584 const char *language)
585{
586 g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
587
588 g_object_set (object: fontchooser, first_property_name: "language", language, NULL);
589}
590

source code of gtk/gtk/gtkfontchooser.c