diff --git a/enaml/qt/qt_menu.py b/enaml/qt/qt_menu.py index eed0ca7ad..b21a77359 100644 --- a/enaml/qt/qt_menu.py +++ b/enaml/qt/qt_menu.py @@ -6,7 +6,7 @@ # The full license is in the file COPYING.txt, distributed with this software. #------------------------------------------------------------------------------ from PyQt4.QtCore import Qt -from PyQt4.QtGui import QMenu +from PyQt4.QtGui import QMenu, QCursor from atom.api import Typed @@ -227,3 +227,9 @@ def set_context_menu(self, context): """ self.widget.setContextMenu(context) + + def popup(self): + """ Popup the menu over the current mouse location. + + """ + self.widget.exec_(QCursor.pos()) diff --git a/enaml/widgets/menu.py b/enaml/widgets/menu.py index db6f37078..60a79759b 100644 --- a/enaml/widgets/menu.py +++ b/enaml/widgets/menu.py @@ -33,6 +33,9 @@ def set_visible(self, visible): def set_context_menu(self, context): raise NotImplementedError + def popup(self): + raise NotImplementedError + class Menu(ToolkitObject): """ A widget used as a menu in a MenuBar. @@ -72,3 +75,16 @@ def _update_proxy(self, change): """ # The superclass implementation is sufficient. super(Menu, self)._update_proxy(change) + + #-------------------------------------------------------------------------- + # Utility Methods + #-------------------------------------------------------------------------- + def popup(self): + """ Popup the menu over the current mouse location. + + """ + if not self.is_initialized: + self.initialize() + if not self.proxy_is_active: + self.activate_proxy() + self.proxy.popup() diff --git a/enaml/wx/wx_menu.py b/enaml/wx/wx_menu.py index 6ba7ffc0b..7623bb9b5 100644 --- a/enaml/wx/wx_menu.py +++ b/enaml/wx/wx_menu.py @@ -717,3 +717,12 @@ def set_context_menu(self, context): """ self.widget.SetContextMenu(context) + + def popup(self): + """ Popup the menu at the current mouse location. + + """ + # This is not supported on wx. Wx requires the menu to be + # popped up over a specified window. It can't be done using + # global coordinates. + pass diff --git a/examples/widgets/popup_menu.enaml b/examples/widgets/popup_menu.enaml new file mode 100644 index 000000000..4bc814e1f --- /dev/null +++ b/examples/widgets/popup_menu.enaml @@ -0,0 +1,33 @@ +#------------------------------------------------------------------------------ +# Copyright (c) 2013, Nucleic Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#------------------------------------------------------------------------------ +from enaml.widgets.api import Window, Container, PushButton, Menu, Action + + +enamldef PopupMenu(Menu): + Action: + text = 'foo' + triggered :: print text + ' triggered' + Action: + text = 'bar' + triggered :: print text + ' triggered' + Action: + text = 'baz' + triggered :: print text + ' triggered' + Action: + text = 'spam' + triggered :: print text + ' triggered' + Action: + text = 'ham' + triggered :: print text + ' triggered' + + +enamldef Main(Window): + Container: + PushButton: + text = 'Popup Menu' + clicked :: PopupMenu().popup()