1

I am trying to set the background color of all labels in GtkGrid. Here is a simplified example:

#include <gtk/gtk.h>

static void activate (GtkApplication* app, gpointer user_data)
{
    GtkWidget *window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window1");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

    GtkWidget *grid = gtk_grid_new ();
    gtk_container_add (GTK_CONTAINER (window), grid);
    GtkWidget *label1 = gtk_label_new("Hello world!");
    gtk_widget_set_hexpand( label1, TRUE);
    gtk_grid_attach(GTK_GRID (grid), label1, 0,0,1,1);
    GtkWidget *label2 = gtk_label_new("Simple Gtk example");
    gtk_widget_set_hexpand( label2, TRUE);
    gtk_grid_attach(GTK_GRID (grid), label2, 0,1,1,1);
    GtkCssProvider *provider = gtk_css_provider_new ();
    gtk_css_provider_load_from_data (
        provider, "label {background-color: #AAAAAA;}", -1, NULL);
    GtkStyleContext *context = gtk_widget_get_style_context (grid);
    gtk_style_context_add_provider(
        context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);

    gtk_widget_show_all (window);
}

int main (int argc, char **argv) {
    GtkApplication *app = gtk_application_new(
        "org.gtk.example", G_APPLICATION_FLAGS_NONE );
    g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
    int status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref (app);
    return status;
}

However, adding the style context provider to the grid's context (as shown above) does not work.

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • "Does not work" - be specific. – DYZ Jan 21 '18 at 07:22
  • @DYZ The background color of the labels are not set to `#AAAAAA`. It seems like the background color is not set at all. – Håkon Hægland Jan 21 '18 at 07:23
  • `gtk_css_provider_load_from_data (provider, "GtkGrid {background-color: #AAAAAA;}", -1, NULL);` will change the background color of the grid and thus also the background color of the labels, but I don't know if this is the effect you've intended, so I haven't written that as an answer. – GTK 1.2.6 fanboy Jan 21 '18 at 14:18
  • @mondegreendispenser Thanks for the suggestion! But I was not able to get it to work. Did you add that provider to `grid`, as I did? Ideally I would like to apply the style to the labels, and not to the whole grid, though.. – Håkon Hægland Jan 21 '18 at 16:19
  • All I did was replacing "label {background-color: #AAAAAA;}" with "GtkGrid {background-color: #AAAAAA;}". However, I found a solution that works when do a general setting for GtkLabel. I'll write an answer for that, see if it works for you. – GTK 1.2.6 fanboy Jan 21 '18 at 18:53

1 Answers1

2

I've modified your code, this will change the background of the labels:

#include <gtk/gtk.h>

static void activate (GtkApplication* app, gpointer user_data)
{
    GtkWidget *window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), "Window1");
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

    GtkWidget *grid = gtk_grid_new ();
    gtk_container_add (GTK_CONTAINER (window), grid);
    GtkWidget *label1 = gtk_label_new("Hello world!");
    gtk_widget_set_hexpand( label1, TRUE);
    gtk_grid_attach(GTK_GRID (grid), label1, 0,0,1,1);
    GtkWidget *label2 = gtk_label_new("Simple Gtk example");
    gtk_widget_set_hexpand( label2, TRUE);
    gtk_grid_attach(GTK_GRID (grid), label2, 0,1,1,1);
    GtkCssProvider *provider = gtk_css_provider_new ();
    GdkDisplay *display = gdk_display_get_default();
    GdkScreen *screen = gdk_display_get_default_screen (display);
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    gtk_css_provider_load_from_data (
        provider, "GtkLabel { background-color: #AAAAAA;}", -1, NULL);

    gtk_widget_show_all (window);
}

int main (int argc, char **argv) {
    GtkApplication *app = gtk_application_new(
        "org.gtk.example", G_APPLICATION_FLAGS_NONE );
    g_signal_connect( app, "activate", G_CALLBACK(activate), NULL);
    int status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref (app);
    return status;
}

Result:

enter image description here

GTK 1.2.6 fanboy
  • 839
  • 1
  • 5
  • 20
  • Great, this works! I also found a hint in the [documentation](https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-context-add-provider): *"Note that a style provider added by this function only affects the style of the widget to which context belongs"*. – Håkon Hægland Jan 21 '18 at 19:12
  • So, then the you either have use `gtk_style_context_add_provider()` on each single label, or to use a single call to `gtk_style_context_add_provider_for_screen()` (as you did). It also means that it is not possible to add a style provider for the grid using `gtk_style_context_add_provider()` that will affect its child widgets (here: the labels). – Håkon Hægland Jan 21 '18 at 19:12
  • Sorry, my answer was a bit too rushed; I used `gdk_display_get_default_screen (display)` twice there, so I've modified my answer. As for your comment about the child widgets: yes, that was answered somewhere here on StackOverflow; unfortunately I could not find that answer again, but you've found it yourself now in the documentation. – GTK 1.2.6 fanboy Jan 21 '18 at 19:16