how could i get the offset of content area relative to window top left corner ? the gtk_widget_get_allocation api returns (0,0) with GtkWindow instead of (0, titlebarHeight),
or is there any way to translate position in widget to position in screen?
when should i destroy the menu with gtk_widget_destroy? when i select a menu item, the activate signal of menu item is emitted after unmap signal of popup menu, so i couldn’t destroy the menu in unmap event.
How old is “old”? GTK 3.24 was released in September 2018. In general, it’s pointless to support older versions of GTK 3. If you need to support older Linux distributions, you should consider tools like Flatpak or Snap.
You don’t destroy menus explicitly: they get destroyed when they are closed.
If you want to keep around a pointer to the menu, in order to see if one is already open, you can use gtk_menu_attach_to_widget() to attach the menu to a widget, and receive a notification when the menu is detached; in that notification function you can nullify the pointer to the menu you are keeping.
For instance, if you want to pop up a menu on a right button press:
static GtkWidget *popup_menu;
popup_detached (GtkWidget *attach_widget, GtkMenu *menu)
popup_menu = NULL;
on_button_press (GtkWidget *widget, GdkEvent *event, gpointer data)
// we only care about right click button presses
if (gdk_event_get_button (event) != GDK_BUTTON_SECONDARY ||
gdk_event_get_event_type (event) != GDK_BUTTON_PRESS)
// a popup menu is still lying around; destroy it
if (popup_menu != NULL)
// create_context_menu() is defined elsewhere
popup_menu = create_context_menu ();
gtk_menu_attach_to_widget (GTK_MENU (popup_menu), widget, popup_detached);
gtk_menu_popup_at_pointer (GTK_MENU (popup_menu), event);