loading...

Styling a TextView with GTK and Vala

opensussex profile image gholden ・1 min read

This might seem a trivial task. And it should be. But I'm new to Vala programming, and new to GTK. For the life of me I couldn't work out how to do this. I searched high and low on the web to get the answer I needed, but to very little avail!

But finally, I figured it out - so I thought I'd make a little post.

The problem. I wanted to style a TextView - sounds simple.

So, here it is.

        var style = """
            textview text {
                background-color: #131c16;
                color: #3ce85c;
            }
        """;

        var css_provider = new Gtk.CssProvider();

        try {
            css_provider.load_from_data(style, -1);
        } catch (GLib.Error e) {
            warning ("Failed to parse css style : %s", e.message);
        }

        Gtk.StyleContext.add_provider_for_screen (
                Gdk.Screen.get_default (),
                css_provider,
                Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
            );

For full context - go see the github repository - https://github.com/opensussex/gtk-textview

Discussion

pic
Editor guide