Re: Button depressing



On 5/27/06, Alex Buell <alex buell munted org uk> wrote:
On Sat, 27 May 2006 15:55:08 +0200, I waved a wand and this message
magically appeared:

> why don't you set the state with the method:

Right, following on from my previous post: Here's some code the OP can
play with as I've just cracked it:

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

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

                void set_color(Gdk::Color c)
                {
                        Glib::RefPtr<Gtk::Style> style =
Gtk::Style::create(); style->set_bg(Gtk::STATE_ACTIVE, c);
                        style->set_bg(Gtk::STATE_PRELIGHT, c);
                        set_style(style);
                }

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

        protected:
                virtual bool on_expose_event(GdkEventExpose* event);

        private:
                Gdk::Color color;

        };

        bool DepressedToggleButton::on_expose_event(GdkEventExpose*
event) {
                return Gtk::Button::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[4];

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"));
        m_DepressedToggleButton[3].set_label("Fourth"); // use default
color

        for (int i = 0; i < 4; 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";
}

Thanks!  I'll give that a shot.

Joe



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