Search code examples
csscgtk3glade

Dont get css in gtk3 with glade to work


I want to load a glade-file and change the color of all toggle-buttons ( class "GtkToggleButton" , I want to change the "pressed" color) The toggle-button is one of many sub-sub-element in the glade-file. Here the C-code-snipped I use to load the .css and the .glade:

void on_minute_pressed(GtkWidget *button)
{
    GtkCssProvider *cssProvider = gtk_css_provider_new ();
    gtk_css_provider_load_from_path(cssProvider,"./test.css",NULL);
    GtkBuilder *builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "minute_dialog.glade", NULL);
    GtkWidget       *window= GTK_WIDGET(gtk_builder_get_object(builder, "MinuteWizard"));
    gtk_window_set_transient_for (window,main_window);
    gtk_style_context_add_provider(gtk_widget_get_style_context(window),cssProvider,GTK_STYLE_PROVIDER_PRIORITY_USER);
    gtk_builder_connect_signals(builder, NULL);

    gtk_widget_show(window);
    g_object_unref(builder);
}

And here the .css I currently use:

.button {
  padding: 30;
  background-color: shade (@bg_color, 55);
}

togglebutton entry {
  color: 900185;
}

button:active {
  background-color: #0274d9;
}

What currently happens: The new window load's fine, but it seems like it does not matter what I write into the .css, it does not change the look of the window-elements. I can see that the .css is loaded, because I get warnings like:

Gtk-WARNING **: Theme parsing error: test.css:2:13: Not using units is deprecated. Assuming 'px'.

What is wrong ? Do I need to apply the .css to each sub-widged separately?


Solution

  • Ok, I found out myself what was the problem:

    According to the documentation of gtk_style_context_add_provider css providers are not inherited to the children style contexts.

    So either one needs to apply the css to each single widget, or use gtk_style_context_add_provider_for_screen to change the css of the whole screen:

    gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(cssProvider), GTK_STYLE_PROVIDER_PRIORITY_USER);