Gtk C - changing color of separtaor

,

Hello all,
I would like to ask for advice:

I would like to change color of separator in my in C written app.

Few monts ago I have here question obout changing background color of whole window:

I have tested similar code, but separators color is not changed. Please could anybody give me any advice, what is wrong in my code:

#include <gtk/gtk.h>
#include <string>

using namespace std;


static void appActivate (GApplication *app, gpointer user_data) {
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
    gtk_window_set_title (GTK_WINDOW (win), "Title");
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);

    gtk_window_set_child (GTK_WINDOW (win), vBox);

    GtkWidget *labText = gtk_label_new("label");
    gtk_box_append (GTK_BOX (vBox), labText);

    GtkWidget *oddelovac = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_box_append (GTK_BOX (vBox), oddelovac);

    //change color of separator
    GtkCssProvider *provider = gtk_css_provider_new();
    string data = "separator {outline-color: #FF0000;}";

    gtk_css_provider_load_from_data(provider,data.c_str(),-1);
    GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (win));
// or context from oddelovac does not work too
//GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (oddelovac));
    gtk_css_provider_load_from_data (provider, data.c_str(), -1);
    gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    //end - change color of separator

    GtkWidget *labText2 = gtk_label_new("label2");
    gtk_box_append (GTK_BOX (vBox), labText2);

    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_widget_set_visible (win, true);
}


int main(int argc, char **argv) {
    GtkApplication *app;
    app = gtk_application_new ("testing.app", G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    return g_application_run (G_APPLICATION (app), NULL, NULL);
    g_object_unref (app);

    return 0;
}

I thing that problem is in: string data = ā€¦
I have tested few css codes which I have found in GTK Inspector, like:

color
-gtk-secondary-caret-color
outline-color
text-decoration-color
caret-color
border-top-color
border-right-color
border-left-color
border-bottom-color
background-color

But I have not found any which causes separators color changing.
Thanks a lot

Hi,

The correct CSS property for the separator color is background.

Here a slightly curated version of your code:

#include <gtk/gtk.h>
#include <string>

using namespace std;


static void appActivate (GApplication *app, gpointer user_data) {
    GtkWidget *win = gtk_application_window_new (GTK_APPLICATION (app));
    gtk_window_set_title (GTK_WINDOW (win), "Title");
    gtk_window_set_default_size (GTK_WINDOW (win), 400, 300);

    GtkWidget  *vBox = gtk_box_new (GTK_ORIENTATION_VERTICAL,10);

    gtk_window_set_child (GTK_WINDOW (win), vBox);

    GtkWidget *labText = gtk_label_new("label");
    gtk_box_append (GTK_BOX (vBox), labText);

    GtkWidget *oddelovac = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_box_append (GTK_BOX (vBox), oddelovac);

    //change color of separator
    GtkCssProvider *provider = gtk_css_provider_new();
    string data = "separator {background: #FF0000;}";

    gtk_css_provider_load_from_string (provider, data.c_str());
    GdkDisplay *disp = gdk_display_get_default ();
    gtk_style_context_add_provider_for_display (disp, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    //end - change color of separator

    GtkWidget *labText2 = gtk_label_new("label2");
    gtk_box_append (GTK_BOX (vBox), labText2);

    GtkWidget *btnBack = gtk_button_new_with_label("Close");
    gtk_box_append  (GTK_BOX (vBox), btnBack);
    g_signal_connect_swapped(GTK_BUTTON(btnBack), "clicked", G_CALLBACK(gtk_window_destroy), win);

    gtk_window_present (GTK_WINDOW (win));
}


int main(int argc, char **argv) {
    GtkApplication *app;
    app = gtk_application_new ("testing.app", G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect (app, "activate", G_CALLBACK (appActivate), NULL);
    int ret = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);

    return ret;
}
1 Like

thank you a lot! with small change it is now working.
instead:

 gtk_css_provider_load_from_string (provider, data.c_str());

I use:

gtk_css_provider_load_from_data (provider, data.c_str(), -1);

because I receive error message:

error: use of undeclared identifier 'gtk_css_provider_load_from_string'; did you mean 'gtk_css_provider_load_from_file'?|
1 Like