Looking for help with setting the background of all 4 child elements in my App. I'm a bit lost on how to do it.
Here is an Example of what the App looks like: https://i.sstatic.net/KqEYm.png
I want to change the backgrounds as follows:
- One - Red
- Two - Yellow
- Three - Green
- Four - Blue
The code is below:
#include <gtk/gtk.h>
int main(int argc, char *argv[]){
//---------- CSS -------------
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
//---------------------------
GtkWidget *window;
GtkWidget *child, *child2, *child3, *child4;
GtkWidget *grid;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Equalizer");
gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
gtk_container_set_border_width(GTK_CONTAINER(window), 5);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
// ---------------------------------------------------- CSS -----------------------------------------------------------
provider = gtk_css_provider_new ();
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
const gchar *myCssFile = "mystyle.css";
GError *error = 0;
gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
g_object_unref (provider);
// --------------------------------------------------------------------------------------------------------------------
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
child = gtk_label_new ("One");
gtk_grid_attach (GTK_GRID (grid), child, 0, 1, 1, 1);
child2 = gtk_label_new ("Two");
g_object_set (child, "margin", 55, NULL);
gtk_grid_attach_next_to (GTK_GRID (grid), child2, child, GTK_POS_RIGHT, 50, 50);
child3 = gtk_label_new ("Three");
g_object_set (child3, "margin", 55, NULL);
gtk_grid_attach_next_to (GTK_GRID (grid), child3, child2, GTK_POS_RIGHT, 50, 50);
child4 = gtk_label_new ("Four");
g_object_set (child4, "margin", 0, NULL);
gtk_grid_attach_next_to (GTK_GRID (grid), child4, child3, GTK_POS_RIGHT, 50, 50);
gtk_widget_show_all(window);
gtk_main();
}
CSS file content:
* {
background-color: yellow;
}
GtkWindow {
background-color: green;
border-width: 3px;
border-color: blue;
}
How can this be achieved in GTK3
using CSS
?