1 /* font-manager-gtk-utils.c
2  *
3  * Copyright (C) 2009 - 2021 Jerry Casiano
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.
17  *
18  * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20 
21 #include "font-manager-gtk-utils.h"
22 
23 /**
24  * SECTION: font-manager-gtk-utils
25  * @short_description: Gtk related utility functions
26  * @title: Gtk utility functions
27  * @include: font-manager-gtk-utils.h
28  */
29 
30 GType
font_manager_drag_target_type_get_type(void)31 font_manager_drag_target_type_get_type (void)
32 {
33   static volatile gsize g_define_type_id__volatile = 0;
34 
35   if (g_once_init_enter (&g_define_type_id__volatile))
36     {
37       static const GEnumValue values[] = {
38         { FONT_MANAGER_DRAG_TARGET_TYPE_FAMILY, "FONT_MANAGER_DRAG_TARGET_TYPE_FAMILY", "family" },
39         { FONT_MANAGER_DRAG_TARGET_TYPE_COLLECTION, "FONT_MANAGER_DRAG_TARGET_TYPE_COLLECTION", "collection" },
40         { FONT_MANAGER_DRAG_TARGET_TYPE_EXTERNAL, "FONT_MANAGER_DRAG_TARGET_TYPE_EXTERNAL", "external" },
41         { 0, NULL, NULL }
42       };
43       GType g_define_type_id =
44         g_enum_register_static (g_intern_static_string ("FontManagerDragTargetType"), values);
45       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
46     }
47 
48   return g_define_type_id__volatile;
49 }
50 
51 /**
52  * font_manager_set_application_style:
53  *
54  * Load application specific CSS and icons.
55  */
font_manager_set_application_style()56 void font_manager_set_application_style ()
57 {
58     g_autofree gchar *css = g_build_path(G_DIR_SEPARATOR_S,
59                                          FONT_MANAGER_BUS_PATH,
60                                          "ui",
61                                          "FontManager.css",
62                                          NULL);
63 
64     g_autofree gchar *icons = g_build_path(G_DIR_SEPARATOR_S,
65                                            FONT_MANAGER_BUS_PATH,
66                                            "icons",
67                                            NULL);
68 
69     GdkScreen *screen = gdk_screen_get_default();
70     GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
71     g_autoptr(GtkCssProvider) css_provider = gtk_css_provider_new();
72     gtk_icon_theme_add_resource_path(icon_theme, icons);
73     gtk_css_provider_load_from_resource(css_provider, css);
74     gtk_style_context_add_provider_for_screen(screen,
75                                               GTK_STYLE_PROVIDER(css_provider),
76                                               GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
77     return;
78 }
79 
80 /**
81  * font_manager_widget_set_align:
82  * @widget:     #GtkWidget
83  * @align:      #GtkAlign
84  *
85  * Set both halign and valign to the same value.
86  */
87 void
font_manager_widget_set_align(GtkWidget * widget,GtkAlign align)88 font_manager_widget_set_align (GtkWidget *widget, GtkAlign align)
89 {
90     g_return_if_fail(GTK_IS_WIDGET(widget));
91     gtk_widget_set_halign(GTK_WIDGET(widget), align);
92     gtk_widget_set_valign(GTK_WIDGET(widget), align);
93     return;
94 }
95 
96 /**
97  * font_manager_widget_set_expand:
98  * @widget:     #GtkWidget
99  * @expand:     %TRUE or %FALSE
100  *
101  * Set both hexpand and vexpand to the same value.
102  */
103 void
font_manager_widget_set_expand(GtkWidget * widget,gboolean expand)104 font_manager_widget_set_expand (GtkWidget *widget, gboolean expand)
105 {
106     g_return_if_fail(GTK_IS_WIDGET(widget));
107     gtk_widget_set_hexpand(GTK_WIDGET(widget), expand);
108     gtk_widget_set_vexpand(GTK_WIDGET(widget), expand);
109     return;
110 }
111 
112 /**
113  * font_manager_widget_set_margin:
114  * @widget:     #GtkWidget
115  * @margin:     margin in pixels
116  *
117  * Set all margin properties to the same value.
118  */
119 void
font_manager_widget_set_margin(GtkWidget * widget,gint margin)120 font_manager_widget_set_margin (GtkWidget *widget, gint margin)
121 {
122     g_return_if_fail(GTK_IS_WIDGET(widget));
123     gtk_widget_set_margin_start(widget, margin);
124     gtk_widget_set_margin_end(widget, margin);
125     gtk_widget_set_margin_top(widget, margin);
126     gtk_widget_set_margin_bottom(widget, margin);
127     return;
128 }
129 
130 /**
131  * font_manager_get_localized_pangram:
132  *
133  * Retrieve a sample string from Pango for the default language.
134  * If Pango does not have a sample string for language,
135  * the classic "The quick brown fox..." is returned.
136  *
137  * Returns: (transfer full): A newly allocated string. Free the result using #g_free.
138  */
139 gchar *
font_manager_get_localized_pangram(void)140 font_manager_get_localized_pangram (void)
141 {
142     PangoLanguage * lang = pango_language_get_default();
143     const gchar *pangram = pango_language_get_sample_string(lang);
144     return g_strdup(pangram);
145 }
146 
147 /**
148  * font_manager_get_localized_preview_text:
149  *
150  * Returns: (transfer full): A newly allocated string. Free the result using #g_free.
151  */
152 gchar *
font_manager_get_localized_preview_text(void)153 font_manager_get_localized_preview_text (void)
154 {
155     g_autofree gchar *pangram = font_manager_get_localized_pangram();
156     return g_strdup_printf(FONT_MANAGER_DEFAULT_PREVIEW_TEXT, pangram);
157 }
158 
159 /**
160  * font_manager_add_keyboard_shortcut:
161  * @action:                                 #GSimpleAction
162  * @action_name:                            name of action
163  * @accels: (array zero-terminated=1):      list of accelerators
164  *
165  * Add the given @action and @accels to the default application.
166  */
167 void
font_manager_add_keyboard_shortcut(GSimpleAction * action,const gchar * action_name,const gchar * const * accels)168 font_manager_add_keyboard_shortcut (GSimpleAction *action,
169                                     const gchar *action_name,
170                                     const gchar * const *accels)
171 {
172     GtkApplication *application = GTK_APPLICATION(g_application_get_default());
173     g_action_map_add_action(G_ACTION_MAP(application), G_ACTION(action));
174     g_autofree gchar *detailed_action_name = g_strdup_printf("app.%s", action_name);
175     gtk_application_set_accels_for_action(application, detailed_action_name, accels);
176     return;
177 }
178 
179 /**
180  * font_manager_clear_pango_cache:
181  * @ctx:    #PangoContext
182  *
183  * Forces Pango to update the cached font configuration.
184  *
185  * Required to render sourced fonts on Pango > 1.47
186  */
187 void
font_manager_clear_pango_cache(PangoContext * ctx)188 font_manager_clear_pango_cache (PangoContext *ctx)
189 {
190     PangoFontMap *font_map = pango_context_get_font_map(ctx);
191     pango_fc_font_map_config_changed((PangoFcFontMap *) font_map);
192     return;
193 }
194