I have read, and tried the existing solutions for this question, and I can't get them to work. I was hoping someone could point out what I'm doing wrong, or tell me why those solutions aren't working anymore.
- https://thegnomejournal.wordpress.com/2011/03/15/styling-gtk-with-css/ (5 years old)
- Widgets to list files with gtk (2 years old)
- How do you change alternating background row colors of a gtk.TreeView in pygtk? (4 years old)
- https://askubuntu.com/questions/285559/how-to-reenable-alternating-grey-lines-in-nautilus-files-3-6-list-view (3 years old)
I wanted to be sure these solutions weren't working, so I made a style sheet like this:
GtkTreeView row {
color: #FFFFFF;
background-color: #FF0000;
}
GtkTreeView row:nth-child(even) {
background-color: #FF00FF;
}
GtkTreeView row:nth-child(odd) {
background-color: #00FFFF;
}
with garish colors, just to make the difference in row color really obvious. Then, I made a little application to load up a tree view:
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
int i;
gtk_init(&argc,&argv);
//GtkBuilder* b = gtk_builder_new_from_file("derp.glade.xml");
GtkWidget* top = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkListStore* items = gtk_list_store_new(2,
G_TYPE_STRING,
G_TYPE_STRING);
GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(items));
gtk_tree_view_insert_column_with_attributes
(GTK_TREE_VIEW(view),
0,
"Herp",
gtk_cell_renderer_text_new(),
"text",0,
NULL);
gtk_tree_view_insert_column_with_attributes
(GTK_TREE_VIEW(view),
1,
"Derp",
gtk_cell_renderer_text_new(),
"text",1,
NULL);
gtk_container_add(GTK_CONTAINER(top),view);
GtkCssProvider* prov = gtk_css_provider_new();
gtk_css_provider_load_from_path
(prov,
"derp.css",
NULL);
gtk_style_context_add_provider
(gtk_widget_get_style_context(view),
GTK_STYLE_PROVIDER(prov),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
GtkTreeIter iter;
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(items),&iter);
for(i=0;i<3;++i) {
gtk_list_store_insert_with_values
(items,&iter,0,
0, "Row",
1, "Row",
-1);
}
gtk_widget_show_all(top);
gtk_main();
return 0;
}
compiled with:
gcc -o test teststyle.c `pkg-config gtk+-3.0 --cflags --libs`
When I run the application, the three rows display with #00FFFF as the background color. They don't alternate. They only take on the color from the "row:nth-child(odd)" section, and even the even rows take on that color. Messing with the css file can do some funny things too. Switching odd and even, for instance:
GtkTreeView row {
color: #FFFFFF;
background-color: #FF0000;
}
GtkTreeView row:nth-child(odd) {
background-color: #FF00FF;
}
GtkTreeView row:nth-child(even) {
background-color: #00FFFF;
}
Now all the rows display as #FF00FF, with no alternation. I think GTK is just completely failing to read the pseudo-classes somehow, accidentally turning "GtkTreeView row:nth-child(odd)" into "GtkTreeView row" and completely missing the "nth-child(even)" selector entirely. If I remove the odd selector and only have even:
GtkTreeView row {
color: #FFFFFF;
background-color: #FF0000;
}
GtkTreeView row:nth-child(even) {
background-color: #FF00FF;
}
Now it's got a background color of #FF0000, so it's ignoring (even) rules entirely.
I tried setting the rules hint on the tree view, and it didn't do anything, other than complain that setting the rules hint was deprecated. I'm using GTK 3.18.9, on a basic Arch system, with XFCE as the window manager. Am I doing anything wrong here? Or is my version of GTK just messed up, somehow?