c++user-interfacec++11gtk3gtkmm3

catch mouse motion in gtkmm


I am trying to catch the mouse motion when I hold the mouse middle button. The goal is to implement a rotation feature in an stl viewer.

I found the event mask BUTTON2_MOTION_MASK. But I have a hard time figuring out which signal catches it.

Here's the two line I use to create and hook the event. These two line are inside a GtkApplicationWindow Constructor.

glWidget.add_events(Gdk::BUTTON2_MOTION_MASK);
glWidget.signal_motion_notify_event().connect(sigc::mem_fun(*this,&mainWindow::rotate));

Here's the function I am trying to connect.

bool mainWindow::rotate(GdkEventMotion* motion_event)
{
    cout<<"test"<<endl;
}

Am I using the correct method? The code does not react when I hold the middle mouse button and move mouse.

I managed to get glArea widget to react to scrolling this way.

glWidget.add_events(Gdk::SMOOTH_SCROLL_MASK);

glWidget.signal_scroll_event().connect(sigc::mem_fun(*this,&mainWindow::zoom));

the function I connected:

bool mainWindow::zoom(GdkEventScroll *eventScroll)
{
        cout<<"test"<<endl;
        return true;
}

Solution

  • I figured it out. You need to both add the Gdk::Button1_MOTION_MASK and the Gdk::BUTTON_PRESS_MASK.

    glWidget.add_events(Gdk::Button1_MOTION_MASK | Gdk::BUTTON_PRESS_MASK);
    

    This will catch the signal when the left mouse button is clicked and positioned on the widget.

    BUTTON2_MOTION_MASK will require that 2 button are pressed. For some reason, it's only the left mouse button(I want the middle button).