Wired issue about mouse click on GtkBox

The double click works well, but not the right-click(the popup menu does not show), what trouble is it?

// pack image and title into the eventbox
Box add_conn_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
add_conn_box.append (add_conn_image);
add_conn_box.append (add_conn_label);
add_conn_box.set_data<string>("action", action_name);
add_conn_box.set_data<string>("title", database_connection_title);
add_conn_box.set_data<ServerSetting?>("connection", server);

var button_press_event = new Gtk.GestureClick();
button_press_event.pressed.connect(handle_connection_clicked);
add_conn_box.add_controller(button_press_event);
            
protected void handle_connection_clicked(Gtk.GestureClick event, int n_press, double x, double y)
{
    if ((Gdk.BUTTON_SECONDARY == event.get_current_button()) && (1 == n_press)) {
        Gdk.Rectangle rect = { (int)x, (int)y, 0, 0, };
        m_context_menu.set_pointing_to(rect);
        m_context_menu.popup();
    } else if ((Gdk.BUTTON_PRIMARY == event.get_current_button()) && (2 == n_press)) {
        string action_name = event_box.get_data<string>("action");
        KApplication.singleton.workbench.call_action(StartViewActions.ACTION_GROUP, action_name);
    }
}

Hi! You should use two GtkGestureClicks: one for the left button and one for the right button. Here’s how to add a second GtkGestureClick for the rest get button:

var gesture_right_click = new Gtk.GestureClick();
gesture_right_click.set_button(Gdk.BUTTON_SECONDARY);
gesture_right_click.pressed.connect(handle_connection_clicked);
add_conn_box.add_controller(gesture_right_click);

Note: set_button / get_button are inherited from the GtkGestureSingle base class.

Thanks for @lb90 suggestion, found that
GtkGestureClick default handle primary button, 0 for all button

var button_press_event = new Gtk.GestureClick();
button_press_event .button = 0;
button_press_event.pressed.connect(handle_connection_clicked);
add_conn_box.add_controller(button_press_event);
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.