Re: TreeView with right-click



Hi Paul,

Paul Davis wrote:
Hey,

I'm banging my head against a wall trying to implement the infamous TreeView right-click.

I understand how and what the signal handlers are doing, and I seem to be getting screwed because TreeView insists on returning true from its signal handling (Thus preventing signal handlers to do anything afterwards).

Other than break down and inhert from TreeView to provide a selection update, is there anyway to accomplish this?

Here's how I do it. I'm not sure if it's the 'right' way but it's worked for me:

#include <gtkmm.h>

struct RecordDef : Gtk::TreeModel::ColumnRecord
{
    RecordDef() { add(number); }
    Gtk::TreeModelColumn<int> number;
};

class TestWindow : public Gtk::Window
{
    public:
        TestWindow() :
            record_def_(),
            model_(Gtk::ListStore::create(record_def_))
        {
            view_.add_events(Gdk::BUTTON_PRESS_MASK);
            view_.signal_button_press_event().connect(
                sigc::mem_fun(*this, &TestWindow::on_press),
                false
            );

            view_.set_model(model_);
            view_.append_column("Number", record_def_.number);
            add(view_);

            for (unsigned i = 0; i != 4; ++i) model_->append();

            show_all_children();
        };

    protected:
        bool on_press(GdkEventButton *event)
        {
            if (event->type == GDK_BUTTON_PRESS &&
                event->button == 3)
            {
                // Might want to call default handler here
                // to update selection etc
                Gtk::MessageDialog("hello!").run();
                return true; // event fully handled
            }
            return false; // event not fully handled
        }

    private:
        RecordDef record_def_;
        Glib::RefPtr<Gtk::ListStore> model_;
        Gtk::TreeView view_;
};


int main(int argc, char **argv)
{
    Gtk::Main kit(argc, argv);
    TestWindow window;
    Gtk::Main::run(window);

    return 0;
}


Kind regards,

Edd













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