| 1 |
/* font-manager-gtk-utils.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2024 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 |
/** |
| 31 |
* font_manager_set_application_style: |
| 32 |
* |
| 33 |
* Load application specific CSS and icons. |
| 34 |
*/ |
| 35 |
void |
| 36 |
font_manager_set_application_style (void) |
| 37 |
{ |
| 38 |
g_autofree gchar *css = g_build_path(G_DIR_SEPARATOR_S, |
| 39 |
FONT_MANAGER_BUS_PATH, |
| 40 |
"ui", |
| 41 |
"FontManager.css", |
| 42 |
NULL); |
| 43 |
|
| 44 |
g_autofree gchar *icons = g_build_path(G_DIR_SEPARATOR_S, |
| 45 |
FONT_MANAGER_BUS_PATH, |
| 46 |
"icons", |
| 47 |
NULL); |
| 48 |
|
| 49 |
GdkDisplay *default_display = gdk_display_get_default(); |
| 50 |
GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display(default_display); |
| 51 |
g_autoptr(GtkCssProvider) css_provider = gtk_css_provider_new(); |
| 52 |
g_debug("Adding icons from resource path : %s", icons); |
| 53 |
gtk_icon_theme_add_resource_path(icon_theme, icons); |
| 54 |
g_debug("Loading custom css from resource path : %s", css); |
| 55 |
gtk_css_provider_load_from_resource(css_provider, css); |
| 56 |
gtk_style_context_add_provider_for_display(default_display, |
| 57 |
GTK_STYLE_PROVIDER(css_provider), |
| 58 |
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* font_manager_clear_pango_cache: |
| 64 |
* @ctx: #PangoContext |
| 65 |
* |
| 66 |
* Forces Pango to update the cached font configuration. |
| 67 |
* |
| 68 |
* Required to render sourced fonts on Pango > 1.47 |
| 69 |
*/ |
| 70 |
void |
| 71 |
font_manager_clear_pango_cache (PangoContext *ctx) |
| 72 |
{ |
| 73 |
PangoFontMap *font_map = pango_context_get_font_map(ctx); |
| 74 |
pango_fc_font_map_config_changed((PangoFcFontMap *) font_map); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* font_manager_text_tag_table_new: |
| 80 |
* |
| 81 |
* Returns: (transfer full): A newly created #GtkTextTagTable. |
| 82 |
* Free the returned object using #g_object_unref(). |
| 83 |
*/ |
| 84 |
GtkTextTagTable * |
| 85 |
font_manager_text_tag_table_new (void) |
| 86 |
{ |
| 87 |
GtkTextTagTable *tags = gtk_text_tag_table_new(); |
| 88 |
g_autoptr(GtkTextTag) font = gtk_text_tag_new("FontDescription"); |
| 89 |
g_object_set(font, "fallback", FALSE, NULL); |
| 90 |
if (!gtk_text_tag_table_add(tags, font)) |
| 91 |
g_warning(G_STRLOC" : Failed to add tag to table: FontDescription"); |
| 92 |
g_autoptr(GtkTextTag) point_size = gtk_text_tag_new("SizePoint"); |
| 93 |
g_object_set(point_size, "family", "Monospace", "rise", 1250, "size-points", 6.5, NULL); |
| 94 |
if (!gtk_text_tag_table_add(tags, point_size)) |
| 95 |
g_warning(G_STRLOC" : Failed to add tag to table: size-points"); |
| 96 |
return tags; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* font_manager_widget_set_align: |
| 101 |
* @widget: #GtkWidget |
| 102 |
* @align: #GtkAlign |
| 103 |
* |
| 104 |
* Set both halign and valign to the same value. |
| 105 |
*/ |
| 106 |
void |
| 107 |
font_manager_widget_set_align (GtkWidget *widget, |
| 108 |
GtkAlign align) |
| 109 |
{ |
| 110 |
g_return_if_fail(GTK_IS_WIDGET(widget)); |
| 111 |
gtk_widget_set_halign(GTK_WIDGET(widget), align); |
| 112 |
gtk_widget_set_valign(GTK_WIDGET(widget), align); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* font_manager_widget_set_expand: |
| 118 |
* @widget: #GtkWidget |
| 119 |
* @expand: #gboolean |
| 120 |
* |
| 121 |
* Set both hexpand and vexpand to the same value. |
| 122 |
*/ |
| 123 |
void |
| 124 |
font_manager_widget_set_expand (GtkWidget *widget, |
| 125 |
gboolean expand) |
| 126 |
{ |
| 127 |
g_return_if_fail(GTK_IS_WIDGET(widget)); |
| 128 |
gtk_widget_set_hexpand(GTK_WIDGET(widget), expand); |
| 129 |
gtk_widget_set_vexpand(GTK_WIDGET(widget), expand); |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* font_manager_widget_set_margin: |
| 135 |
* @widget: #GtkWidget |
| 136 |
* @margin: #gint |
| 137 |
* |
| 138 |
* Set all margin properties to the same value. |
| 139 |
*/ |
| 140 |
void |
| 141 |
font_manager_widget_set_margin (GtkWidget *widget, |
| 142 |
gint margin) |
| 143 |
{ |
| 144 |
g_return_if_fail(GTK_IS_WIDGET(widget)); |
| 145 |
gtk_widget_set_margin_start(widget, margin); |
| 146 |
gtk_widget_set_margin_end(widget, margin); |
| 147 |
gtk_widget_set_margin_top(widget, margin); |
| 148 |
gtk_widget_set_margin_bottom(widget, margin); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* font_manager_widget_set_name: |
| 154 |
* @widget: #GtkWidget |
| 155 |
* @name: (nullable): widget name |
| 156 |
* |
| 157 |
* Set widget name and css properties to the same value. |
| 158 |
*/ |
| 159 |
void |
| 160 |
font_manager_widget_set_name (GtkWidget *widget, |
| 161 |
const gchar *name) |
| 162 |
{ |
| 163 |
GtkWidgetClass *widget_class = GTK_WIDGET_GET_CLASS(widget); |
| 164 |
|
| 165 |
if (name) |
| 166 |
gtk_widget_set_name(widget, name); |
| 167 |
else |
| 168 |
name = gtk_widget_get_name(widget); |
| 169 |
|
| 170 |
gtk_widget_add_css_class(widget, name); |
| 171 |
gtk_widget_class_set_css_name(widget_class, name); |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* font_manager_widget_dispose: |
| 177 |
* @widget: #GtkWidget |
| 178 |
* |
| 179 |
* Convenience function which iterates through the children of a #GtkWidget, |
| 180 |
* calls #gtk_widget_unparent() on each and sets the pointer to #NULL. |
| 181 |
*/ |
| 182 |
void |
| 183 |
font_manager_widget_dispose (GtkWidget *widget) |
| 184 |
{ |
| 185 |
g_return_if_fail(GTK_IS_WIDGET(widget)); |
| 186 |
GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(widget)); |
| 187 |
while (child) { |
| 188 |
GtkWidget *next = gtk_widget_get_next_sibling(child); |
| 189 |
g_clear_pointer(&child, gtk_widget_unparent); |
| 190 |
child = next; |
| 191 |
} |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* font_manager_get_localized_pangram: |
| 197 |
* |
| 198 |
* Retrieve a sample string from Pango for the default language. |
| 199 |
* If Pango does not have a sample string for language, |
| 200 |
* the classic "The quick brown fox..." is returned. |
| 201 |
* |
| 202 |
* Returns: (transfer full): A newly allocated string. Free the result using #g_free. |
| 203 |
*/ |
| 204 |
gchar * |
| 205 |
font_manager_get_localized_pangram (void) |
| 206 |
{ |
| 207 |
PangoLanguage * lang = pango_language_get_default(); |
| 208 |
const gchar *pangram = pango_language_get_sample_string(lang); |
| 209 |
return g_strdup(pangram); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* font_manager_get_localized_preview_text: |
| 214 |
* |
| 215 |
* Returns: (transfer full): A newly allocated string. Free the result using #g_free. |
| 216 |
*/ |
| 217 |
gchar * |
| 218 |
font_manager_get_localized_preview_text (void) |
| 219 |
{ |
| 220 |
g_autofree gchar *pangram = font_manager_get_localized_pangram(); |
| 221 |
return g_strdup_printf(FONT_MANAGER_DEFAULT_PREVIEW_TEXT, pangram); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* font_manager_get_shortcut_for_stateful_action: |
| 226 |
* @prefix: (nullable): Action prefix i.e. "app", "window", etc. |
| 227 |
* @name: Action name |
| 228 |
* @target: Action target |
| 229 |
* @accel: (nullable): A valid accelerator string as understood by #gtk_accelerator_parse |
| 230 |
* |
| 231 |
* Returns: (transfer full) (nullable): A newly created #GtkShortcut. |
| 232 |
* Free the returned object using #g_object_unref. |
| 233 |
*/ |
| 234 |
GtkShortcut * |
| 235 |
font_manager_get_shortcut_for_stateful_action (const gchar *prefix, |
| 236 |
const gchar *name, |
| 237 |
const gchar *target, |
| 238 |
const gchar *accel) |
| 239 |
{ |
| 240 |
g_return_val_if_fail(name != NULL && target != NULL, NULL); |
| 241 |
g_autofree gchar *action_name = prefix ? |
| 242 |
g_strdup_printf("%s.%s", prefix, name) : |
| 243 |
g_strdup(name); |
| 244 |
GtkShortcutAction *shortcut_action = gtk_named_action_new(action_name); |
| 245 |
GtkShortcutTrigger *shortcut_trigger = gtk_shortcut_trigger_parse_string(accel); |
| 246 |
GtkShortcut *shortcut = gtk_shortcut_new(shortcut_trigger, shortcut_action); |
| 247 |
GVariant *arg = g_variant_new_string(target); |
| 248 |
gtk_shortcut_set_arguments(shortcut, arg); |
| 249 |
return shortcut; |
| 250 |
} |
| 251 |
|