1/* testfontchooserdialog.c
2 * Copyright (C) 2011 Alberto Ruiz <aruiz@gnome.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include <string.h>
21#ifdef HAVE_PANGOFT
22#include <pango/pangofc-fontmap.h>
23#endif
24#include <gtk/gtk.h>
25
26
27static gboolean
28monospace_filter (const PangoFontFamily *family,
29 const PangoFontFace *face,
30 gpointer data)
31{
32 return pango_font_family_is_monospace (family: (PangoFontFamily *) family);
33}
34
35static void
36notify_font_cb (GtkFontChooser *fontchooser, GParamSpec *pspec, gpointer data)
37{
38 PangoFontFamily *family;
39 PangoFontFace *face;
40
41 g_debug ("Changed font name %s", gtk_font_chooser_get_font (fontchooser));
42
43 family = gtk_font_chooser_get_font_family (fontchooser);
44 face = gtk_font_chooser_get_font_face (fontchooser);
45 if (family)
46 {
47 g_debug (" Family: %s is-monospace:%s",
48 pango_font_family_get_name (family),
49 pango_font_family_is_monospace (family) ? "true" : "false");
50 }
51 else
52 g_debug (" No font family!");
53
54 if (face)
55 g_debug (" Face description: %s", pango_font_face_get_face_name (face));
56 else
57 g_debug (" No font face!");
58}
59
60static void
61notify_preview_text_cb (GObject *fontchooser, GParamSpec *pspec, gpointer data)
62{
63 g_debug ("Changed preview text %s", gtk_font_chooser_get_preview_text (GTK_FONT_CHOOSER (fontchooser)));
64}
65
66static void
67font_activated_cb (GtkFontChooser *chooser, const char *font_name, gpointer data)
68{
69 g_debug ("font-activated: %s", font_name);
70}
71
72static void
73quit_cb (GtkWidget *widget,
74 gpointer data)
75{
76 gboolean *done = data;
77
78 *done = TRUE;
79
80 g_main_context_wakeup (NULL);
81}
82
83int
84main (int argc, char *argv[])
85{
86 GtkWidget *window;
87 GtkWidget *font_button;
88 gboolean done = FALSE;
89
90 gtk_init ();
91
92 font_button = gtk_font_button_new ();
93
94#ifdef HAVE_PANGOFT
95 if (argc > 0)
96 {
97 FcConfig *config;
98 PangoFontMap *fontmap;
99 int i;
100
101 /* Create a custom font configuration by adding font files specified
102 * on the commandline to the default config.
103 */
104 config = FcInitLoadConfigAndFonts ();
105 for (i = 0; i < argc; i++)
106 FcConfigAppFontAddFile (config, file: (const FcChar8 *)argv[i]);
107
108 fontmap = pango_cairo_font_map_new_for_font_type (fonttype: CAIRO_FONT_TYPE_FT);
109 pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (fontmap), fcconfig: config);
110 gtk_font_chooser_set_font_map (GTK_FONT_CHOOSER (font_button), fontmap);
111 }
112#endif
113
114 gtk_font_button_set_use_font (GTK_FONT_BUTTON (font_button), TRUE);
115
116 window = gtk_window_new ();
117 gtk_window_set_child (GTK_WINDOW (window), child: font_button);
118 gtk_widget_show (widget: window);
119
120 g_signal_connect (font_button, "notify::font",
121 G_CALLBACK (notify_font_cb), NULL);
122 g_signal_connect (font_button, "notify::preview-text",
123 G_CALLBACK (notify_preview_text_cb), NULL);
124 g_signal_connect (font_button, "font-activated",
125 G_CALLBACK (font_activated_cb), NULL);
126
127 if (argc >= 2 && strcmp (s1: argv[1], s2: "--monospace") == 0)
128 {
129 gtk_font_chooser_set_filter_func (GTK_FONT_CHOOSER (font_button),
130 filter: monospace_filter, NULL, NULL);
131 }
132
133 g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
134
135 while (!done)
136 g_main_context_iteration (NULL, TRUE);
137
138 return 0;
139}
140

source code of gtk/tests/testfontchooserdialog.c