Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtk(mm) 3 button background color change

Tags:

c++

gtkmm

I'm trying to change the background color of a button to red but it doesn't seem to work. I paste the example code. If anybody can tell me how to fix my code please help.

#include <gtkmm.h>

// g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`

int  main(int argc, char *argv[])
{
    Gtk::Main kit(argc, argv);
    Gtk::Window  window;
    Gtk::Button  button("TEST");
    button.override_background_color(Gdk::RGBA("red"));
    window.add(button);
    window.show_all();
    Gtk::Main::run(window);
    return EXIT_SUCCESS;
}

UPDATE: ok here's how I solved:

mr_screen = Gdk::Screen::get_default();
mr_style_context = mp_window->get_style_context();
mr_css_provider = Gtk::CssProvider::create();
mr_css_provider->load_from_path(Glib::build_filename(m_glade_dir_path, "filename.css"));
mr_style_context->add_provider_for_screen(mr_screen, mr_css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);

and the content of filename.css is:

column-header .button {
    background-image: -gtk-gradient (linear,
        left top,
        left bottom,
        from (#51cccc),
        color-stop (0.5, darker (#51cccc)),
        to (#51cccc));
}
like image 661
giuspen Avatar asked Jan 21 '12 11:01

giuspen


2 Answers

Just indeed, I learned it the hard way, that CssProvider is the right way to handle such things. You don't need to use glade, even good is to have css in your code, like so

#include <gtkmm.h>

Gtk::Button* button = new Gtk::Button("Test");
Glib::RefPtr<Gtk::CssProvider> css_provider = Gtk::CssProvider::create();
css_provider->load_from_data(
    "button {background-image: image(cyan);}\
     button:hover {background-image: image(green);}\
     button:active {background-image: image(brown);}");
button->get_style_context()->add_provider(
    css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);

You have to deal with background-image, because background-color is overlapped from the default background-image and thus not changed. I have my wisdom from Ruby-GNOME2

like image 111
Erich Kuester Avatar answered Sep 22 '22 15:09

Erich Kuester


i have no experience with c++, but i use python, in python you have to do this:

button.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1.0 , 0.0 , 0.0, 1.0))

maybe in c++ you have to do this

button.override_background_color(GTK_STATE_FLAGS_NORMAL, Gdk::RGBA(1.0 , 0.0 , 0.0, 1.0));
like image 39
shehata Avatar answered Sep 21 '22 15:09

shehata