Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Add context menu support to Wx menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Jan 7, 2013
1 parent c8c9b6f commit 153f312
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions enaml/wx/wx_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class wxMenu(wx.Menu):
working with wxMenu and wxAction children.
"""
def __init__(self, *args, **kwargs):
def __init__(self, parent, *args, **kwargs):
""" Initialize a wxMenu.
Parameters
Expand All @@ -30,6 +30,7 @@ def __init__(self, *args, **kwargs):
"""
super(wxMenu, self).__init__(*args, **kwargs)
self._parent = parent
self._title = u''
self._all_items = []
self._menus_map = {}
Expand All @@ -38,6 +39,7 @@ def __init__(self, *args, **kwargs):
self._bar_enabled = True
self._visible = True
self._batch = False
self._is_context_menu = False
self._id = wx.NewId()

#--------------------------------------------------------------------------
Expand Down Expand Up @@ -226,6 +228,17 @@ def OnMenuChanged(self, event):
item.SetItemLabel(menu.GetTitle())
item.Enable(menu.IsEnabled())

def OnShowContextMenu(self, event):
""" A private event handler for displaying the context menu.
This handler is connected to the context menu event on the
parent widget when this menu is marked as a context menu.
"""
parent = self._parent
if parent and isinstance(parent, wx.Window):
parent.PopupMenu(self)

#--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -335,6 +348,38 @@ def SetVisible(self, visible):
self._visible = visible
self._EmitChanged()

def IsContextMenu(self):
""" Whether this menu acts as a context menu for its parent.
Returns
-------
result : bool
True if this menu acts as a context menu, False otherwise.
"""
return self._is_context_menu

def SetContextMenu(self, context):
""" Set whether this menu acts as a context menu for its parent.
Parameters
----------
context : bool
True if this menu should act as a context menu, False
otherwise.
"""
old_context = self._is_context_menu
self._is_context_menu = context
if old_context != context:
parent = self._parent
if parent:
handler = self.OnShowContextMenu
if context:
parent.Bind(wx.EVT_CONTEXT_MENU, handler)
else:
parent.Unbind(wx.EVT_CONTEXT_MENU, handler=handler)

def AddMenu(self, menu):
""" Add a wx menu to the Menu.
Expand Down Expand Up @@ -540,7 +585,7 @@ def create_widget(self, parent, tree):
""" Create the underlying wx menu widget.
"""
widget = wxMenu()
widget = wxMenu(parent)
widget.BeginBatch()
return widget

Expand All @@ -550,6 +595,7 @@ def create(self, tree):
"""
super(WxMenu, self).create(tree)
self.set_title(tree['title'])
self.set_context_menu(tree['context_menu'])
self.widget().EndBatch(emit=False)

def init_layout(self):
Expand Down Expand Up @@ -633,6 +679,12 @@ def on_action_set_title(self, content):
"""
self.set_title(content['title'])

def on_action_set_context_menu(self, content):
""" Handle the 'set_context_menu' action from the Enaml widget.
"""
self.set_context_menu(content['context_menu'])

#--------------------------------------------------------------------------
# Widget Update Methods
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -660,6 +712,12 @@ def set_visible(self, visible):
"""
self.widget().SetVisible(visible)

def set_context_menu(self, context):
""" Set whether or not the menu is a context menu.
"""
self.widget().SetContextMenu(context)

def set_minimum_size(self, min_size):
""" Overridden parent class method.
Expand All @@ -676,3 +734,11 @@ def set_maximum_size(self, max_size):
"""
pass

def set_tool_tip(self, tool_tip):
""" Overridden parent class method.
Menus do not have a tool tip, so this method is a no-op.
"""
pass

0 comments on commit 153f312

Please sign in to comment.