问题
I'm refactoring some code that normally requires the user to click a button for a popup menu to appear (it appears beside the button), what I'm trying to do is show the popup menu if the user hovers the mouse over the button icon for a preset duration and hide it again if the user moves onto another button icon.
However, activating the menu via gtk_menu_popup
steals mouse and keyboard focus, I have made little progress trying to grab focus for the button widget.
Is it possible to return focus to the button icon?
回答1:
Debugging this turned out to be quite troublesome, as gtk_menu_popup
grabs the gdk keyboard and pointer focus, which prevents one from stepping through the code with gdb, I eventually managed it by running the code in a VM and using a remote debugging session with gdb over SSH.
There are a few different mechanisms through which the popup may steal focus from the original widget
- Via transient window relationships (only if you pass
parent_menu_shell
) - Via
gdk_pointer_grab
- Via
gtk_grab_add
As I set the parent_menu_shell
shell parameter to NULL, this meant that I had to undo the last two 'grabs', like so:
gdk_pointer_ungrab(GDK_CURRENT_TIME);
gdk_keyboard_ungrab(GDK_CURRENT_TIME);
gtk_grab_remove(menu);
This did the trick but focus gets temporarily stolen by the popup, which causes problems too
来源:https://stackoverflow.com/questions/12731981/can-gtk-menus-displayed-via-gtk-menu-popup-release-focus