Synthetic events

Hello! I want to show a popup from an event that isn’t a signal, but when i try to synthesize the event, the popup closes immediately after it is opened. My synthetic event is in the commented code. Thanks!

#include <gtk/gtk.h>

int main(int argc, char* argv[]) {
  gtk_init(&argc, &argv);

  static GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  g_signal_connect(
    G_OBJECT(window),
    "button-press-event",
    G_CALLBACK(+[](gpointer data, GdkEvent *event) {
      const int RIGHT_CLICK = 3;
      GdkEventButton* bevent = (GdkEventButton*) event;
      if (bevent->button != RIGHT_CLICK) return;

      GtkWidget* popup = gtk_menu_new();

      auto item = gtk_menu_item_new_with_label("hello");

      gtk_widget_show(item);
      gtk_menu_shell_append(GTK_MENU_SHELL(popup), item);

      // auto win = GDK_WINDOW(gtk_widget_get_window(window));
      // auto seat = gdk_display_get_default_seat(gdk_display_get_default());
      // auto mouse_device = gdk_seat_get_pointer(seat);

      // auto e = gdk_event_new(GDK_BUTTON_PRESS);
      // e->button.time = GDK_CURRENT_TIME;
      // e->button.window = win;
      // gdk_event_set_device(e, mouse_device);

      gtk_menu_popup_at_pointer(
        GTK_MENU(popup),
        nullptr
      );
  
      gtk_widget_show(popup);
    }),
    window
  );
 
  gtk_widget_show(window);

  gtk_main();
}
c++ example.cc`pkg-config --cflags --libs gtk+-3.0`

Still no luck with this although ive tried a lot of different combinations of things/

You should never synthesize events, and you definitely cannot do that to pop up a menu.

If you want to pop up a menu at a given position, use gtk_menu_popup_at_rect() with a NULL event.

I switched to using the method you suggested…

    auto win = GDK_WINDOW(gtk_widget_get_window(window));
    auto seat = gdk_display_get_default_seat(gdk_display_get_default());
    auto mouse_device = gdk_seat_get_pointer(seat);

    GdkRectangle rect;
    gint x, y;

    gdk_window_get_device_position(win, mouse_device, &x, &y, NULL);

    rect.x = x;
    rect.y = y;
    rect.width = 0;
    rect.height = 0;

    gtk_menu_popup_at_rect(
      GTK_MENU(popup),
      win,
      &rect,
      GDK_GRAVITY_SOUTH_WEST,
      GDK_GRAVITY_NORTH_WEST,
      NULL
    );

However, it doesn’t solve the problem, additionally, providing NULL (or nullptr) as the event causes this warning…

Gtk-WARNING **: 11:35:00.393: no trigger event for menu popup

I also tried gtk_get_current_event() instead of NULL

Maybe some more details could help. In my window, I have an instance of wk-webview which raises a context menu event, upon that event, i want to then show the native popup at the mouse position. The menu shows up, its positioned correctly, even using the new method, but, it just seems to want to automatically close itself. if i hold the mouse button down it stays open, i can even select menu items. but it just doesnt want to stay open after the button press.

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