Gtk change font size in XML file? - Page 1
Forum section: electronics / general

author: jmelson
author_rank: Super Contributor
timestamp: August 29, 2022, 11:22:29 pm
content: I have a c app with a GUI built with glade. I need to change the font size, and it seems easiest to just change the XML file. Here's a snip of one of the GtkEntry sections:<child><object class="GtkEntry" id="count1"><property name="visible">True</property><property name="can_focus">True</property><property name="editable">False</property><property name="invisible_char">●</property><property name="activates_default">True</property></object><packing><property name="expand">True</property><property name="fill">True</property><property name="position">1</property></packing></child>Can I just add a line there that will specify the font or just the font size?Thanks,Jon

author: Nominal Animal
author_rank: Super Contributor
timestamp: August 31, 2022, 01:18:33 pm
content: I do not think so, as I've always usedCSSinstead, viaGtkCssProvider()interface. It is exactly as simple, except that you have a second file (a .css one) along with the GtkBuilder .ui one.In the simplest case, before you show your UI,Code:GFile *cssfile = g_file_new_for_uri("uri-to-css-file");if (cssfile) {GtkCssProvider *provider = gtk_css_provider_new();if (!gtk_css_provider_load_from_file(provider, cssfile, NULL)) {gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);}g_object_unref(cssfile);}where the URI can be e.g.resource://TLD/DOMAIN/APP/FILE.css, or you can drop thecssfileand usegtk_css_provider_load_from_path(provider, "path", NULL)instead.SeeGTK CSS Overviewfor how to target thecount1GtkEntry, or all GtkEntries.For targeting only that particular GtkEntry, a simpleCode:entry#count1 { font: 21px; }should work.

author: jmelson
author_rank: Super Contributor
timestamp: September 02, 2022, 12:38:07 am
content: Wow, I wish I understood this well enough to implement it, but I really don't. I tried to follow what is in your links, but I'd just be trying stuff at random, I'm afraid.Thanks for the hints, anyway.Jon

author: Nominal Animal
author_rank: Super Contributor
timestamp: September 02, 2022, 05:01:29 pm
content: There are a few different ways one can instantiate Glade .ui files.The main difference is whether you subclass GtkMainWindow and usegtk_widget_class_set_template_from_resource()/gtk_widget_class_set_template(), or whether you use a GtkBuilder object andgtk_builder_new_from_file()/gtk_builder_new_from_resource()/gtk_builder_new_from_string(), to "load" it.Which one of these are you using?Could you post a C snippet showing how you do load the UI? Is the XML stored in a separate file, compiled into a resource (usingglib-compile-resource) at build time, or as a string in your C source file?I'm sure it would be a simple, straightforward addition to add a parallel CSS file so you can override the visuals.You might consider whether the CSS file should be external, and user-selectable via command-line option or an environment variable, too.A typical option is to put the CSS file in your application directory asdefault.css(/usr/share/yourapp/style/default.css), and use that unless an environment variable or command line option sets a different one, sayYOURAPP_CSS=/usr/share/yourapp/style/dark.css.