Debian Codegraph @

lib/gtk/font-manager-properties-page.c

raw
1 /* font-manager-properties-page.c
2 *
3 * Copyright (C) 2009-2025 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-properties-page.h"
22
23 /**
24 * SECTION: font-manager-properties-page
25 * @short_description: Font properties widget
26 * @title: Properties Pane
27 * @include: font-manager-properties-page.h
28 *
29 * This widget displays extended information about the selected font file,
30 * including any embedded designer details, copyright and design description.
31 */
32
33 struct _FontManagerPropertiesPage
34 {
35 GtkWidget parent;
36
37 GtkWidget *copyright;
38 GtkWidget *description;
39 GtkWidget *designer;
40 GtkWidget *designer_label;
41 GtkWidget *grid;
42
43 JsonObject *properties;
44 };
45
46 G_DEFINE_TYPE(FontManagerPropertiesPage, font_manager_font_properties_page, GTK_TYPE_WIDGET)
47
48 static const struct
49 {
50 const gchar *member_name;
51 const gchar *display_name;
52 }
53 FontPropertyRow [] =
54 {
55 { "psname", N_("PostScript Name") },
56 { "family", N_("Family") },
57 { "style", N_("Style") },
58 { "width", N_("Width") },
59 { "slant", N_("Slant") },
60 { "weight", N_("Weight") },
61 { "spacing", N_("Spacing") },
62 { "version", N_("Version") },
63 /* Translators : For context see https://docs.microsoft.com/en-us/typography/opentype/spec/os2#achvendid */
64 { "vendor", N_("Vendor") },
65 { "filetype", N_("FileType") },
66 /* Keep this entry last */
67 { "filesize", N_("Filesize") },
68 };
69
70 #define N_FONT_PROPERTIES G_N_ELEMENTS(FontPropertyRow)
71 #define FILESIZE_PROPERTY (N_FONT_PROPERTIES - 1)
72
73 static GtkWidget *
74 create_title_label (FontManagerPropertiesPage *self, gint property)
75 {
76
77 GtkWidget *title = gtk_label_new(g_dgettext(NULL, FontPropertyRow[property].display_name));
78 gtk_widget_set_sensitive(title, FALSE);
79 gtk_widget_set_opacity(title, 0.9);
80 gtk_widget_set_halign(title, GTK_ALIGN_END);
81 font_manager_widget_set_margin(title, FONT_MANAGER_DEFAULT_MARGIN);
82 return title;
83 }
84
85 static GtkWidget *
86 create_value_label (FontManagerPropertiesPage *self, gint property)
87 {
88 GtkWidget *value = gtk_label_new(NULL);
89 gtk_label_set_ellipsize(GTK_LABEL(value), PANGO_ELLIPSIZE_END);
90 gtk_widget_set_halign(value, GTK_ALIGN_START);
91 font_manager_widget_set_margin(value, FONT_MANAGER_DEFAULT_MARGIN);
92 return value;
93 }
94
95 static GtkWidget *
96 construct_start_child (FontManagerPropertiesPage *self)
97 {
98 GtkWidget *scroll = gtk_scrolled_window_new();
99 self->grid = gtk_grid_new();
100 for (gint i = 0; i < N_FONT_PROPERTIES; i++) {
101 gtk_grid_attach(GTK_GRID(self->grid), create_title_label(self, i), 0, i, 1, 1);
102 if (i == FILESIZE_PROPERTY) {
103 GtkWidget *value = gtk_link_button_new_with_label("", NULL);
104 GtkWidget *label = gtk_button_get_child(GTK_BUTTON(value));
105 gtk_widget_set_halign(label, GTK_ALIGN_START);
106 gtk_widget_set_halign(value, GTK_ALIGN_START);
107 gtk_widget_remove_css_class(value, "text-button");
108 gtk_widget_remove_css_class(value, "link");
109 gtk_grid_attach(GTK_GRID(self->grid), value, 1, i, 1, 1);
110 } else {
111 gtk_grid_attach(GTK_GRID(self->grid), create_value_label(self, i), 1, i, 1, 1);
112 }
113 }
114 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll), self->grid);
115 font_manager_widget_set_expand(self->grid, FALSE);
116 font_manager_widget_set_margin(self->grid, FONT_MANAGER_DEFAULT_MARGIN * 2);
117 gtk_widget_set_margin_start(self->grid, FONT_MANAGER_DEFAULT_MARGIN * 3);
118 return scroll;
119 }
120
121 static GtkWidget *
122 construct_end_child (FontManagerPropertiesPage *self)
123 {
124 GtkWidget *child2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
125 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
126 GtkWidget *scroll = gtk_scrolled_window_new();
127 self->copyright = gtk_label_new(NULL);
128 self->description = gtk_label_new(NULL);
129 self->designer = gtk_link_button_new("");
130 self->designer_label = gtk_label_new("");
131 gtk_label_set_ellipsize(GTK_LABEL(self->designer_label), PANGO_ELLIPSIZE_END);
132 gtk_widget_set_margin_top(self->copyright, FONT_MANAGER_DEFAULT_MARGIN * 3);
133 gtk_widget_set_margin_bottom(self->copyright, 0);
134 gtk_label_set_wrap(GTK_LABEL(self->copyright), TRUE);
135 gtk_label_set_wrap_mode(GTK_LABEL(self->copyright), PANGO_WRAP_WORD_CHAR);
136 gtk_label_set_wrap(GTK_LABEL(self->description), TRUE);
137 gtk_label_set_wrap_mode(GTK_LABEL(self->description), PANGO_WRAP_WORD_CHAR);
138 gtk_box_append(GTK_BOX(box), self->copyright);
139 gtk_box_append(GTK_BOX(box), self->description);
140 gtk_widget_set_size_request(box, 0, 0);
141 gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll), box);
142 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
143 GTK_POLICY_NEVER,
144 GTK_POLICY_AUTOMATIC);
145 gtk_box_append(GTK_BOX(child2), scroll);
146 gtk_box_append(GTK_BOX(child2), self->designer);
147 gtk_box_append(GTK_BOX(child2), self->designer_label);
148 gtk_label_set_yalign(GTK_LABEL(self->copyright), 0.0);
149 gtk_label_set_yalign(GTK_LABEL(self->description), 0.0);
150 gtk_label_set_xalign(GTK_LABEL(self->copyright), 0.0);
151 gtk_label_set_xalign(GTK_LABEL(self->description), 0.0);
152 font_manager_widget_set_expand(GTK_WIDGET(self), TRUE);
153 font_manager_widget_set_expand(box, TRUE);
154 font_manager_widget_set_expand(scroll, TRUE);
155 font_manager_widget_set_expand(self->copyright, FALSE);
156 font_manager_widget_set_expand(self->description, TRUE);
157 font_manager_widget_set_expand(self->designer, FALSE);
158 font_manager_widget_set_expand(self->designer_label, FALSE);
159 font_manager_widget_set_margin(self->copyright, FONT_MANAGER_DEFAULT_MARGIN * 2);
160 font_manager_widget_set_margin(self->description, FONT_MANAGER_DEFAULT_MARGIN * 2);
161 /* Affected by the margin set on our pane separator */
162 gtk_widget_set_margin_start(self->copyright, 0);
163 gtk_widget_set_margin_start(self->description, 0);
164 font_manager_widget_set_margin(self->designer, FONT_MANAGER_DEFAULT_MARGIN);
165 font_manager_widget_set_margin(self->designer_label, FONT_MANAGER_DEFAULT_MARGIN * 2);
166 gtk_widget_set_margin_start(child2, FONT_MANAGER_DEFAULT_MARGIN * 1.5);
167 gtk_widget_set_margin_end(child2, FONT_MANAGER_DEFAULT_MARGIN * 1.5);
168 return child2;
169 }
170
171 static void
172 set_row_visible (FontManagerPropertiesPage *self,
173 gint row,
174 gboolean visible)
175 {
176 GtkWidget *title = gtk_grid_get_child_at(GTK_GRID(self->grid), 0, row);
177 GtkWidget *value = gtk_grid_get_child_at(GTK_GRID(self->grid), 1, row);
178 gtk_widget_set_visible(title, visible);
179 gtk_widget_set_visible(value, visible);
180 return;
181 }
182
183 static void
184 reset (FontManagerPropertiesPage *self)
185 {
186 g_return_if_fail(self != NULL);
187 for (gint i = 0; i < N_FONT_PROPERTIES; i++) {
188 set_row_visible(self, i, TRUE);
189 GtkWidget *widget = gtk_grid_get_child_at(GTK_GRID(self->grid), 1, i);
190 if (i == FILESIZE_PROPERTY) {
191 gtk_link_button_set_uri(GTK_LINK_BUTTON(widget), "");
192 gtk_button_set_label(GTK_BUTTON(widget), NULL);
193 } else {
194 gtk_label_set_label(GTK_LABEL(widget), NULL);
195 gtk_widget_set_tooltip_text(widget, NULL);
196 }
197 }
198 gtk_label_set_text(GTK_LABEL(self->copyright), NULL);
199 gtk_label_set_text(GTK_LABEL(self->description), NULL);
200 gtk_button_set_label(GTK_BUTTON(self->designer), "");
201 gtk_link_button_set_uri(GTK_LINK_BUTTON(self->designer), "");
202 gtk_widget_set_tooltip_text(self->designer, "");
203 gtk_label_set_label(GTK_LABEL(self->designer_label), "");
204 return;
205 }
206
207 static void
208 update (FontManagerPropertiesPage *self)
209 {
210 reset(self);
211
212 for (gint i = 0; i < N_FONT_PROPERTIES; i++) {
213
214 const gchar *member = FontPropertyRow[i].member_name;
215 if (!json_object_has_member(self->properties, member)) {
216 set_row_visible(self, i, FALSE);
217 continue;
218 }
219 const gchar *value = json_object_get_string_member(self->properties, member);
220 if (!value) {
221 set_row_visible(self, i, FALSE);
222 continue;
223 }
224
225 GtkWidget *widget = gtk_grid_get_child_at(GTK_GRID(self->grid), 1, i);
226
227 if (i == FILESIZE_PROPERTY) {
228 g_autofree gchar *uri = NULL;
229 if (json_object_has_member(self->properties, "filepath")) {
230 const gchar *filepath = json_object_get_string_member(self->properties, "filepath");
231 gtk_widget_set_tooltip_text(GTK_WIDGET(widget), filepath);
232 g_autofree gchar *dirpath = g_path_get_dirname(filepath);
233 uri = g_strdup_printf("file://%s", dirpath);
234 }
235 gtk_link_button_set_uri(GTK_LINK_BUTTON(widget), uri ? uri : "");
236 gtk_button_set_label(GTK_BUTTON(widget), value);
237 } else {
238 gtk_label_set_label(GTK_LABEL(widget), value);
239 gtk_widget_set_tooltip_text(widget, value);
240 }
241
242 }
243
244 // Addresses issue #292
245 // Unfortunately also makes pane adjustments pointless.
246 GtkPaned *paned = (GtkPaned *) gtk_widget_get_first_child(GTK_WIDGET(self));
247 g_autoptr(GtkRequisition) req = gtk_requisition_new();
248 gtk_widget_get_preferred_size(GTK_WIDGET(self->grid), NULL, req);
249 gtk_paned_set_position(paned, req->width);
250
251 const gchar *copyright = NULL;
252 const gchar *description = NULL;
253 const gchar *designer = NULL;
254 const gchar *designer_url = NULL;
255
256 if (json_object_has_member(self->properties, "copyright"))
257 copyright = json_object_get_string_member(self->properties, "copyright");
258
259 if (json_object_has_member(self->properties, "description"))
260 description = json_object_get_string_member(self->properties, "description");
261
262 if (json_object_has_member(self->properties, "designer"))
263 designer = json_object_get_string_member(self->properties, "designer");
264
265 if (json_object_has_member(self->properties, "designer-url"))
266 designer_url = json_object_get_string_member(self->properties, "designer-url");
267
268 gtk_label_set_label(GTK_LABEL(self->copyright), copyright);
269 gtk_label_set_label(GTK_LABEL(self->description), description);
270 gtk_button_set_label(GTK_BUTTON(self->designer), designer ? designer : "");
271 gtk_label_set_label(GTK_LABEL(self->designer_label), designer ? designer : "");
272 gtk_link_button_set_uri(GTK_LINK_BUTTON(self->designer), designer_url ? designer_url : "");
273 gtk_widget_set_tooltip_text(self->designer, designer_url ? designer_url : "");
274 gtk_widget_set_visible(self->designer, (designer && designer_url));
275 gtk_widget_set_visible(self->designer_label, (designer && !designer_url));
276 GtkWidget *button_label = gtk_button_get_child(GTK_BUTTON(self->designer));
277 if (GTK_IS_LABEL(button_label))
278 gtk_label_set_ellipsize(GTK_LABEL(button_label), PANGO_ELLIPSIZE_END);
279 return;
280 }
281
282 static void
283 font_manager_font_properties_page_dispose (GObject *gobject)
284 {
285 g_return_if_fail(gobject != NULL);
286 FontManagerPropertiesPage *self = FONT_MANAGER_PROPERTIES_PAGE(gobject);
287 g_clear_pointer(&self->properties, json_object_unref);
288 font_manager_widget_dispose(GTK_WIDGET(self));
289 G_OBJECT_CLASS(font_manager_font_properties_page_parent_class)->dispose(gobject);
290 }
291
292 static void
293 font_manager_font_properties_page_class_init (FontManagerPropertiesPageClass *klass)
294 {
295 GObjectClass *object_class = G_OBJECT_CLASS(klass);
296 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
297 object_class->dispose = font_manager_font_properties_page_dispose;
298 gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT);
299 gtk_widget_class_set_css_name(widget_class, "FontManagerPropertiesPage");
300 return;
301 }
302
303 static void
304 font_manager_font_properties_page_init (FontManagerPropertiesPage *self)
305 {
306 g_return_if_fail(self != NULL);
307 gtk_widget_add_css_class(GTK_WIDGET(self), FONT_MANAGER_STYLE_CLASS_VIEW);
308 font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerPropertiesPage");
309 GtkWidget *pane = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
310 gtk_widget_set_parent(pane, GTK_WIDGET(self));
311 font_manager_widget_set_expand(pane, TRUE);
312 gtk_paned_set_start_child(GTK_PANED(pane), construct_start_child(self));
313 gtk_paned_set_end_child(GTK_PANED(pane), construct_end_child(self));
314 font_manager_widget_set_expand(GTK_WIDGET(self), TRUE);
315 return;
316 }
317
318 /**
319 * font_manager_preoperties_pane_update:
320 * @properties: (nullable): #JsonObject or %NULL
321 *
322 * The following are valid members for @properties
323 *
324 * {
325 * "psname" : string,
326 * "family" : string,
327 * "style" : string,
328 * "width" : string,
329 * "slant" : string,
330 * "weight" : string,
331 * "spacing" : string,
332 * "version" : string,
333 * "vendor" : string,
334 * "filetype" : string,
335 * "filepath" : string,
336 * "filesize" : string,
337 * "copyright" : string,
338 * "description" : string,
339 * "designer" : string,
340 * "designer-url" : string
341 * }
342 *
343 * Missing members and %NULL values are allowed.
344 * Members not listed above are ignored.
345 */
346 void
347 font_manager_font_properties_page_update (FontManagerPropertiesPage *self,
348 JsonObject *properties)
349 {
350 g_return_if_fail(self != NULL);
351 g_clear_pointer(&self->properties, json_object_unref);
352 self->properties = properties ? json_object_ref(properties) : NULL;
353 update(self);
354 return;
355 }
356
357 /**
358 * font_manager_font_properties_page_new:
359 *
360 * Returns: (transfer full): A newly created #FontManagerPropertiesPage.
361 * Free the returned object using #g_object_unref().
362 */
363 GtkWidget *
364 font_manager_font_properties_page_new ()
365 {
366 return g_object_new(FONT_MANAGER_TYPE_PROPERTIES_PAGE, NULL);
367 }
368