Reputation: 89
I've made a simple glade UI file for my programm. It contains of 2 textfields and 1 button. And also I made a css style which contains 1 button class named "button". When I added this file in Glade as "Custom CSS provider" it changed my button style within the editor.
After that I created the UI using GtkBuilder (I am using GTK# 3.20 bindings + C#).
Builder b = new Builder ();
b.AddFromFile ("login.xml");
b.Autoconnect (this);
It created my simple form (2 fields and 1 button) but there is no CSS Style applied to it. So I checked XML code of my UI file and found out that there is no reference to CSS exept of this:
<style>
<class name="button"/>
</style>
After that I read that I need CssProvider. So I created one.
CssProvider css = new CssProvider ();
css.LoadFromPath ("ui_common.css");
But nothing happened. So, how should I apply custom CSS to my Builder UI?
Upvotes: 3
Views: 4214
Reputation: 11578
You are almost there; the last step is to take the GtkCssProvider and add it to a GtkStyleContext.
Since you are using a .ui
file and a .css
file, I assume you want to make your CSS apply to all widgets in the application. In that case, you can use gtk_style_context_add_provider_for_screen()
, passing gdk_screen_get_default()
as the GdkScreen. (This should be safe; it's what the GTK+ Inspector does. IIRC there was a more specific reason why this should be safe...)
You can also do this to individual widgets using gtk_widget_get_style_context()
and gtk_style_context_add_provider()
.
In both cases, use GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
as the priority for application-local CSS.
(I do not know the C# names; sorry.)
Upvotes: 8