Products > Programming

Gtk change font size in XML file?

(1/1)

jmelson:
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
     

Nominal Animal:
I do not think so, as I've always used CSS instead, via GtkCssProvider() 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);
    }
--- End code ---
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) instead.
See GTK CSS Overview for how to target the count1 GtkEntry, or all GtkEntries.

For targeting only that particular GtkEntry, a simple

--- Code: ---entry#count1 { font: 21px; }
--- End code ---
should work.

jmelson:
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

Nominal Animal:
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.

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks WYSIWYG Editor
Powered by SMFPacks Advanced Attachments Uploader Mod