Skip to content

Commit

Permalink
Add popup support to the Menu widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Apr 25, 2013
1 parent 867fef1 commit 5363a56
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
8 changes: 7 additions & 1 deletion enaml/qt/qt_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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())
16 changes: 16 additions & 0 deletions enaml/widgets/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
9 changes: 9 additions & 0 deletions enaml/wx/wx_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions examples/widgets/popup_menu.enaml
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 5363a56

Please sign in to comment.