Search code examples
c++cssuser-interfacegtkgtk3

Change GTK 3.0 Widget colors


I want to change the default color of my application, which affects various widgets, specifically CheckButtons and Scales. Currently, the default color is purple, and I want to change it to blue.

I am fine with a solution that changes the default color for the entire application or just for specific widgets.

I have tried using a CSS provider, but it only changes the color of the checkmark icon.

auto css_provider = Gtk::CssProvider::create();
    css_provider->load_from_data(
        "checkbutton check {"
            "background-color: #ff0000;" 
            "color: #ffffff;" 
        "}"
        "checkbutton check:checked {"
            "background-color: #0000ff;" 
            "color: #ff0000;"
        "}"
    );
    auto screen = Gdk::Screen::get_default();
    Gtk::StyleContext::add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);

Images of what code is doing: (want to change the purple section)

Before code:

Before code

After code:

After code

EDIT 1: Change background color of checkbutton

Tried this new code with the following result:

css_provider->load_from_data(R"(
    checkbutton {
        background-color: #0000ff;
    }
    checkbutton check:checked {
        color: #ff0000;
    }
)");

After code


Solution

  • One of the solutions I ask for in the proposal is to change the default color of the whole application. Although it is not the best, it can work as a makeshift solution

    To do this we must change the theme assigned, it can be done at the operating system or application level. To do it at application level:

    const char *theme_name = "Adwaita";
    g_object_set(gtk_settings_get_default(), "gtk-theme-name", theme_name, NULL);
    

    Being Adwaita the new theme. It is important to take into account that it is only possible to add a theme installed in the system, with the color palettes already defined, however, it is always possible to create a customized one. This solution is not versatile because it does not allow a complete customization of the GTK widgets but it can be useful to get by.