eventsgtkmm

Detect click on Gtk::Image?


I've been trying to detect click on a Gtk::Image with gtkmm for over 2 hours, but I couldn't get it to work. It does compile and execute fine, but the event is never triggered.

Some stuff I tried, that compiles, does not crash, but doesn't work:

m_image = manage(new Gtk::Image(Gtk::Stock::APPLY, Gtk::ICON_SIZE_BUTTON));
m_image->add_events(Gdk::ALL_EVENTS_MASK); 
m_hbox->pack_start(*m_image, Gtk::PACK_SHRINK);

m_image->signal_button_release_event()
    .connect(sigc::hide(sigc::mem_fun(*this, &Todo::switchStatus)));

m_image->show();

or

#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/stock.h>
#include <gtkmm/image.h>

#include <iostream>

using namespace std;

class Win : public Gtk::Window
{
    public:
        Win();
        bool link(GdkEventButton* e);

    private:
        Gtk::Image image;
};

Win::Win()
    : image(Gtk::Stock::APPLY, Gtk::ICON_SIZE_BUTTON)
{
    cerr << "created" << endl;

    image.add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
    image.signal_button_release_event().connect(sigc::mem_fun(*this, &Win::link));
    image.show();

    add(image);
}

bool Win::link(GdkEventButton* e)
{
    cerr << "kuh" << endl;
}

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

    Gtk::Window window;
    window.resize(300, 500);

    Win win;

    Gtk::Main::run(win);

    return 0;
}

Solution

  • From http://developer.gnome.org/gtkmm/unstable/classGtk_1_1Image.html:

    Gtk::Image is a "no window" widget (has no Gdk::Window of its own), so by default does not receive events. If you want to receive events on the image, such as button clicks, place the image inside a Gtk::EventBox, then connect to the event signals on the event box

    So I guess try to put a signal on an eventbox after wrapping the image with the EventBox.