Gtk3, adding application style to GtkCellRendererProgress which can be overridden by theme

In my application, I would like to change the progressbar color to red, but this only works if I set the style priority to GTK_STYLE_PROVIDER_PRIORITY_SETTINGS, and themes cannot override it. If I leave GTK_STYLE_PROVIDER_PRIORITY_THEME then I don’t get any effect from the style. For tests I use Adwaita and other themes. In non-test code, I have a class for my application so that themes can distinguish it from others.

What can I do?

#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkListStore *store;
  GtkTreeIter iter;
  GtkWidget *tree_view;
  GtkCssProvider *provider;
  GtkStyleContext *context;

  //====================================================================
  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (
    provider,
    "treeview.view .progressbar { background: image(red); }",
    -1, NULL);
  gtk_style_context_add_provider_for_screen (
    gdk_screen_get_default (),
    GTK_STYLE_PROVIDER (provider),
    GTK_STYLE_PROVIDER_PRIORITY_THEME);
  //====================================================================

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  store = gtk_list_store_new (1, G_TYPE_INT);
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, 50, -1);
  tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
  gtk_tree_view_append_column (
    GTK_TREE_VIEW (tree_view),
    gtk_tree_view_column_new_with_attributes (
      "progress",
       gtk_cell_renderer_progress_new (),
       "value", 0,
       NULL));
  gtk_container_add (GTK_CONTAINER (window), tree_view);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Hi,

If the user theme defines a progressbar background, then it will overrides your red one.

I don’t think it’s possible to control the mixing, either your style has higher prio and always overrides the user theme, or has lower prio then the user theme overrides it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.