Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Background color of child in GTK3 (c language) with CSS

Tags:

c

css

gtk3

Like the Title says I'm developing an App and I need to set the Background of all 4 child, but I have no clue how to do it.

Here is an Example which explains how the App looks like: enter image description here

I need to change the backgrounds like this:

  • One - should be Red
  • Two - should be yellow
  • Three - Should be green
  • Four - Should be Blue

Here is the Code:

#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();
}

and the CSS file:

* {
    background-color: yellow;
}

GtkWindow {
    background-color: green;
    border-width: 3px;
    border-color: blue;
}

How do I do it in GTK3 using CSS ?

like image 450
Michi Avatar asked Jan 13 '17 16:01

Michi


People also ask

What is the CSS code for background color?

Background-color values can be expressed in hexadecimal values such as #FFFFFF, #000000, and #FF0000. Background-color values can be expressed using rgb such as rgb(255,255,255), rgb(0,0,0), and rgb(255,0,0). Background-color values can be expressed as named colors such as white, black, and red.

Does GTK use CSS?

GTK supports the CSS import rule, in order to load another style sheet in addition to the currently parsed one.


1 Answers

You probably need to give each of your widgets a name in order to give them styles by ID, e.g. #child3.

The name you would need to set in this case would be child3.

void
gtk_widget_set_name (GtkWidget *widget,
                     const gchar *name);

https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-set-name

like image 196
OregonTrail Avatar answered Oct 22 '22 09:10

OregonTrail