Re: Gtk::ToggleButton



Alex Buell wrote:

Following a recent post about Gtk::Button and changing its state to
depressed and redrawing with different colours, I wrote the code as
below. I can initially set the colour when creating the button, but
I don't seem to be able to change its colour later - see set_color().
Any ideas on how I can force the button to redraw itself with new color?
Thanks.

#include <iostream>
#include <gtkmm.h>

namespace Gtk
{
	class DepressedToggleButton : public Gtk::ToggleButton
	{
	public:
		DepressedToggleButton() : color(Gdk::Color("grey")) {}
		DepressedToggleButton(Gdk::Color c) : color(c) {}
		~DepressedToggleButton() {}

Why did you use Gtk:STATE_INSENSITIVE only? You had to use all possible button states with appropriate color values to see different background.

void set_color(Gdk::Color c) { color = c;
			modify_bg(Gtk::STATE_INSENSITIVE, color);
			queue_draw(); // need to force the button to
redraw with new color - HOW?!? }

		Gdk::Color get_color()
		{
			return color;
		}

	protected:
		virtual bool on_expose_event(GdkEventExpose* event);
		Gdk::Color color;
	};

you don't need to override on_expose_event to change color.

	bool DepressedToggleButton::on_expose_event(GdkEventExpose*
event) {
		modify_bg(Gtk::STATE_INSENSITIVE, color);
		return Gtk::Widget::on_expose_event(event);
	}
}

class TestButton : public Gtk::Window
{
public:
	TestButton();
	~TestButton();

protected:
	void HandleButton(int i);

	Gtk::VBox m_VBox;
	Gtk::HButtonBox m_HButtonBox;
	Gtk::DepressedToggleButton m_DepressedToggleButton[3];

private:
	bool state;
};

TestButton::TestButton() :
	state(false)
{
	set_title("DepressedToggleButton");
	add(m_VBox);

	m_HButtonBox.set_border_width(5);
	m_HButtonBox.set_layout(Gtk::BUTTONBOX_END);
	m_HButtonBox.set_spacing(5);

	m_DepressedToggleButton[0].set_label("First");
	m_DepressedToggleButton[0].set_color(Gdk::Color("red"));
	m_DepressedToggleButton[1].set_label("Second");
	m_DepressedToggleButton[1].set_color(Gdk::Color("blue"));
	m_DepressedToggleButton[2].set_label("Third");
	m_DepressedToggleButton[2].set_color(Gdk::Color("green"));

	for (int i = 0; i < 3; i++)
	{
		m_DepressedToggleButton[i].signal_clicked().connect(
			sigc::bind<int>(sigc::mem_fun(*this,
&TestButton::HandleButton), i)); m_HButtonBox.add
(m_DepressedToggleButton[i]); }

	m_VBox.pack_start(m_HButtonBox, Gtk::BUTTONBOX_END, 0);

	set_resizable(false);
	show_all_children();
}

TestButton::~TestButton()
{
}

int main(int argc, char *argv[])
{
	Gtk::Main app(argc, argv);

	TestButton test;
	Gtk::Main::run(test);
	return 0;
}


void TestButton::HandleButton(int i)
{
	std::cout << "Button " << i << " clicked!\n";
}
	






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]