Search code examples
c++cssgtkgtkmmgtkmm3

Applying css style to Gtk::ToolButton is not working with a selector in gtkmm


I am trying to set an application wide css style in gtkmm-3.0 .This is how i initialize and load the style:

#include "MainWindow.h"

#include <string>
#include <iostream>

const std::string style = R"(
    toolbutton {
        border-color : #000000;
        border-width : 1px;
        border-radius: 0px;
    }
)";

void loadStyle()
{
    try
    {
        auto css = Gtk::CssProvider::create();

        css->load_from_data(style);

        Gtk::StyleContext::add_provider_for_screen(
            Gdk::Screen::get_default(), css,
            GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
        );
    }
    catch(const Gtk::CssProviderError error)
    {
        std::cerr << "Failed to load style:" <<error.code() << std::endl;
    }
}

int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv,"com.test");

    loadStyle();

    MainWindow window(800, 600);
    window.show_all();

    return app->run(window);
}

MainWindow is just a Gtk::Window that has a Gtk::Toolbar with a few Gtk::ToolButton.

But for some reason the style isn't getting applied to my ToolButton's at all. If i change my stylesheet selector to "select all elements" it get's applied to my toolbuttons:

* {
        border-color : #000000;
        border-width : 1px;
        border-radius: 0px;
}

So i am assuming my code is correct and the selector in my stylesheet is wrong. However the documentation says GtkToolButton has a single CSS node with name toolbutton. I am currently not setting any names or classes myself using set_name or add_class.

What am i doing wrong?


Solution

  • when we create Gtktoolbutton it's actually using Gtkbutton. enter image description here

    so in your css if you add

    toolbutton button {
    

    it will work.

    gtkcss can be kinda confusing to learn as you wont find proper documentation(well I was not able to find a very detailed doc and all).. it's better to use GtkInspector to debug gtk css..