EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: jmelson on August 29, 2022, 11:22:29 pm

Title: Gtk change font size in XML file?
Post by: jmelson on August 29, 2022, 11:22:29 pm
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
     
Title: Re: Gtk change font size in XML file?
Post by: Nominal Animal on August 31, 2022, 01:18:33 pm
I do not think so, as I've always used CSS (https://developer-old.gnome.org/gtk3/stable/chap-css-overview.html) instead, via GtkCssProvider() (https://developer-old.gnome.org/gtk3/stable/GtkCssProvider.html) 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: [Select]
    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 the cssfile and use gtk_css_provider_load_from_path(provider, "path", NULL) (https://docs.gtk.org/gtk3/method.CssProvider.load_from_path.html) instead.
See GTK CSS Overview (https://developer-old.gnome.org/gtk3/stable/chap-css-overview.html) for how to target the count1 GtkEntry, or all GtkEntries.

For targeting only that particular GtkEntry, a simple
Code: [Select]
entry#count1 { font: 21px; }
should work.
Title: Re: Gtk change font size in XML file?
Post by: jmelson on September 02, 2022, 12:38:07 am
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
Title: Re: Gtk change font size in XML file?
Post by: Nominal Animal on September 02, 2022, 05:01:29 pm
There are a few different ways one can instantiate Glade .ui files.

The main difference is whether you subclass GtkMainWindow and use gtk_widget_class_set_template_from_resource()/gtk_widget_class_set_template(), or whether you use a GtkBuilder object and gtk_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 (using glib-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 as default.css (/usr/share/yourapp/style/default.css), and use that unless an environment variable or command line option sets a different one, say YOURAPP_CSS=/usr/share/yourapp/style/dark.css.