Shadow_m2 Shadow_m2 - 1 month ago 22
CSS Question

CSS Styling in GTK+

Ok, i recently started programming with GTK+ in C.
I have a GdkToggleButton in my code and i want to add styling to it with css.
I've read almost all of GTK3+ documentation here https://developer.gnome.org/gtk3/stable but found nothing that fits my case.
Actually there is a part of the documentation about CSS but i couldn't find the part to actually parse the css file.

All I want to do is to change color of a GdkToggleButton either with CSS or any other method.

I want to know where to put the css file and how to parse it and add it to a Widget.

I actually could get GdkRGBA (color) of a button but changes have no effect.
Here's how i change GdkRGBA of button.

GdkRGBA *color ;
GtkWidget *button;
GtkStyleContext *context;

color = gdk_rgba_copy (color); // I don't know to initialize GdkRGBA :(

context = gtk_widget_get_style_context(button);
button = gtk_toggle_button_new_with_label ("Hello World");
gtk_style_context_get_color(context,
GTK_STATE_FLAG_NORMAL,
color);
gdk_rgba_parse(color,
"red");


SOLVED

gtk_css_provider_load_from_path (cssProvider,
"/home/shadow_m2/code.css",
NULL);
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER); // I had used wrong priority on first try
gtk_style_context_save (context);

Answer

You can load custom CSS embedded in your program with gtk_css_provider_load_from_data(). GtkCssProvider is how to load CSS in general.

Then you get the GtkWidget's style context and call add_provider() on it. Use the APPLICATION priority; that's what you're adding anyway (application-specific CSS).

Also the save() call is not necessary.

Comments