Haowen Liu
Haowen Liu

Reputation: 167

gtk3 TreeView has a different default background color

The Issue

This is how my gtk3 app currently look like on Ubuntu 20.04:

enter image description here

The tree view on the left has a slightly brighter background color than the others. I created the tree view by:

tree_view = gtk_tree_view_new();
gtk_widget_set_margin_bottom(tree_view, 12);
gtk_widget_set_margin_top(tree_view, 12);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), FALSE);

All my other widgets are created similarly with modifying any styles.

Desired Behavior

I want all the bright backgrounds to match each other. I don't mind if they all become the brighter white or the darker white. An example is gtk3-demo, where all backgrounds become the brighter white, as in the tree view:

enter image description here

(Its stack header is still the darker white, but I don't mind that as long as the major regions match.)

Attempted Solutions

It's tempting to set it with CSS and make them match. But I realize those backgrouds can change with different themes. I still want my app to respond to theme changes. That is, if the user is using a dark theme, I still want my app to become dark (the same dark color).

Is there a way to do this? Thank you!

EDIT:

I noticed that gtk3-demo got its bright background color because it used GtkNotebook. I tried but GtkNotebook only gives a light color when it has its tabs enabled, which I don't want. Once it disable show-tabs, its background returns to the darker white.

Upvotes: 1

Views: 215

Answers (1)

Haowen Liu
Haowen Liu

Reputation: 167

I manually queried the background of the GtkTreeView and applied it to other widgets. This solution is much more ugly than I want it to be, but I guess it works.

  context = gtk_widget_get_style_context(GTK_WIDGET(tree_view));
  GdkRGBA *c;
  gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, "background-color", &c,
                        NULL);

  GdkDisplay *display;
  GdkScreen *screen;
  display = gdk_display_get_default();
  screen = gdk_display_get_default_screen(display);
  GtkCssProvider *provider;
  provider = gtk_css_provider_new();
  gtk_style_context_add_provider_for_screen(
      screen, GTK_STYLE_PROVIDER(provider),
      GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

  char css[128];
  snprintf(css, 128, ".custom_bg { background-color: rgba(%d, %d, %d, 1) }",
           (int)(c->red * 255), (int)(c->green * 255), (int)(c->blue * 255));
  gtk_css_provider_load_from_data(provider, css, -1, NULL);

  g_object_unref(provider);
  gdk_rgba_free(c);

Upvotes: 0

Related Questions