Search code examples
c++gladegtkmm3

How to set theme from css resource for multiple glade widgets in gtkmm3


I did all the includes and used name spaces

#include "include/main.h"
#include <iostream>
#include <gtkmm.h>

using namespace std;
using namespace Gtk;

Window* appWindow = nullptr;

Loaded the glade file and tried to load a theme for widgets

    auto refBuilder = Builder::create();
    auto refProvider = CssProvider::create();
    auto refStyleContext = StyleContext::create();

try
{
    refBuilder->add_from_resource("/res/gui.glade");
    refProvider->load_from_resource("/res/theme.css");
}

Caught errors. Which it does. It gives an error when it cannnot load either of the resources.

catch(const Glib::FileError& ex)
{
    cerr << "FileError: " << ex.what() << endl;
    return 1;
}
catch(const Glib::MarkupError& ex)
{
    cerr << "MarkupError: " << ex.what() << endl;
    return 1;
}
catch(const Gtk::BuilderError& ex)
{
    cerr << "BuilderError: " << ex.what() << endl;
    return 1;
}

Now I cant seem to apply the css for my widgets

refBuilder->get_widget("winFrame", appWindow);
if(appWindow)
{
    Glib::RefPtr<Gdk::Screen>refScreen = appWindow->get_screen();

    refBuilder->get_widget("quit_Item", quitItem);
    refStyleContext->add_provider_for_screen(refScreen, refProvider, GTK_STYLE_PROVIDER_PRIORITY_USER);

    if(quitItem)
    {
        quitItem->signal_activate().connect( sigc::ptr_fun(on_button_clicked) );
    };

};
app->run(*appWindow);
delete appWindow;
return 0;

I have the following css which i used to try, but not even a button changed colour

.recordButton {
color: red;
background: blue;
border-color: red;
}

GtkBox{
background: #669999;
text-shadow: 1px 1px 5px black;
box-shadow: 0px 0px 5px black;
border: 1px solid black;
}
Buton{
background: #669999;
text-shadow: 1px 1px 5px black;
box-shadow: 0px 0px 5px black;
border: 1px solid black;
}

Solution

  • After revisiting my code. I tried to copy the statements for

    try{}
    catch {}
    

    This is how I was able to get it to work

    refBuilder->get_widget("winFrame", appWindow);
    if(appWindow)
    {
        try
        {
            refProvider->load_from_resource("/res/default-theme.css");
            Glib::RefPtr<Gdk::Screen> refScreen = appWindow->get_screen();
            refStyleContext->add_provider_for_screen(refScreen, refProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
        }
        catch(const Gio::ResourceError& ex)
        {
            cerr << "ResourceError: " << ex.what() << endl;
            return 1;
        }
        catch(const Glib::MarkupError& ex)
        {
            cerr << "MarkupError: " << ex.what() << endl;
            return 1;
        }
        catch(const Gtk::CssProviderError& ex)
        {
            cerr << "CssProviderError: " << ex.what() << endl;
            return 1;
        }
        catch(const Glib::Error& ex)
        {
            cerr << "GlibError: " << ex.what() << endl;
            return 1;
        };
    
    };
    

    I also found out that my css file format had issues. I got help from GTK+ CSS Reference. My glade has a GtkBox after the windows so new css has

    box * {
    background-color: teal;
    font: italic bold 14px Ani;
    }
    

    Which was applied to child under the GtkBox Saddly I'm still working on getting borders around widgets to work but atleast now I know the css theme is being applied