Search code examples
buttongtkgtk3gtkmm

Gtk Button not triggering button release event on left click


I'm wondering why a GtkButton's button-release-event is not triggered by left mouse button clicks. In the following code (using gtkmm3) the button fires the clicked event on a left button click and the button-release event when clicked on using any other mouse button. I'd like to determine whether any modifier keys are being pressed while clicking the button, so I need the additional information provided by the button release event. Is there a way to get it to trigger on left clicks as well?

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

void clicked()
{
    std::cout << "clicked" << std::endl;
}

bool button_released( GdkEventButton* evt )
{
    std::cout << "button released" << std::endl;
    return false;
}

int main( int argc, char* argv[] )
{
    Glib::RefPtr<Gtk::Application> app =
        Gtk::Application::create( argc, argv, "org.gtkmm.examples.base" );

    Gtk::Window window;
    window.set_default_size( 300, 200 );

    Gtk::Button* btn = Gtk::manage( new Gtk::Button( "Click me!" ) );
    btn->add_events( Gdk::BUTTON_RELEASE_MASK );
    btn->signal_clicked().connect( sigc::ptr_fun( clicked ) );
    btn->signal_button_release_event().connect( sigc::ptr_fun( button_released ) );
    window.add( *btn );

    window.show_all();

    return app->run( window );
}

Solution

  • The button implementation handles the button release event itself (if it is the primary mouse button, usually the left one) and propagates a GtkButton specific clicked/button-release-event signal instead.

    Source

    Update:

    If you really got special needs you either can write a widget handling button presses yourself (either by starting from scratch or starting from a copy of GtkButton) or you subclass and overwrite the handler for that specific event (not sure this is possible, there seem to be quite a few functions doing some magic with events in GtkButton - if it is possible, then just wrap that callbacked hooked to the signal specific function pointer and make it call the original callback (but do not care about the return value) and always return GDK_PROPAGATE_EVENT)