After this GTK+ change font to spin button and this how to apply CSS to GTK+ code? question, I started understanding how to apply a css style to a GTK code, yet there are still a lot confusing details.
This is the code with which I create two buttons and three labels:
test.c
#include <gtk/gtk.h>
#include <string.h>
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window = gtk_application_window_new (app);;
GtkWidget *button_01 = gtk_button_new_with_label("This is button 01");
GtkWidget *button_02 = gtk_button_new_with_label("This is button 02");
GtkWidget *label0 = gtk_label_new("hello 0");
GtkWidget *label1 = gtk_label_new("hello 1");
GtkWidget *label2 = gtk_label_new("hello 2");
GtkWidget * main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 25);
GtkCssProvider *cssProvider = gtk_css_provider_new();
gtk_css_provider_load_from_path(cssProvider, "theme.css", NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_box_set_homogeneous (GTK_BOX (main_box), TRUE);
gtk_container_add (GTK_CONTAINER (window), main_box);
gtk_container_add (GTK_CONTAINER (main_box), button_01);
gtk_container_add (GTK_CONTAINER (main_box), label0);
gtk_container_add (GTK_CONTAINER (main_box), label1);
gtk_container_add (GTK_CONTAINER (main_box), label2);
gtk_container_add (GTK_CONTAINER (main_box), button_02);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
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;
}
and this is the css file:
theme.css
GtkWindow {
font: Comic Sans 12
}
.button {
color: green;
}
The code works fine, because all fonts are changed and all buttons are red. What I want to do is to style each widget individually, for example to make the label0 appear red, the label1 appear green and the label 2 appear blue.
According to these examples https://developer.gnome.org/gtk3/stable/chap-css-overview.html , something something this should do the trick:
label#label0 {
color: red;
}
label#label1 {
color: green;
}
label#label2 {
color: blue;
}
but apparently it doesn't which means that I miss something there.
Any help about how to style widgets individually with css code would be appreciated.
The CSS file doesn't know about the names that you give to your variables in your program. You have to name the widgets with gtk_widget_set_name()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With