Search code examples
gtkgtk3vala

Can't set CSS to specified widget in GTK+


I'm using Vala with GTK+ and now I'm trying to add custom CSS to specified widget. I can add fe. backgroudn to GtkWidget but not for #sidebar

#sidebar { //It doesn't work                                                           
    color: white;                                                                
}                                                                       

GtkWindow {  // It works                                                                    
    background-color: red;                                                                        
}   

I'm adding class to widget like that:

sidebar = new Gtk.Label("Hello");
sidebar.set_name("sidebar");

And it's changes color to GtkWindow, but not for this label.

Any ideas?


Solution

  • I haven't programmed in Vala, but you should add class to StyleContext. This is in C

       sidebar = gtk_label_new ("Hello');
       gtk_style_context_add_class ( gtk_widget_get_style_context ("mysidebar"), sidebar);
    

    Also, style "sidebar", is already defined in GtkStyle. You should change the "sidebar" in CSS into something else (sidebar is used by views, toolbar etc) But if you persist, the syntax should be:

       .mysidebar {
           #anything
       }