1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" The wx specific implementation of a menu manager.
"""
import wx
from traits.api import Str, Bool, provides
from pyface.action.action_manager import ActionManager
from pyface.action.action_manager_item import ActionManagerItem
from pyface.action.group import Group
from pyface.action.i_menu_manager import IMenuManager
@provides(IMenuManager)
class MenuManager(ActionManager, ActionManagerItem):
""" A menu manager realizes itself in a menu control.
This could be a sub-menu or a context (popup) menu.
"""
# 'MenuManager' interface ---------------------------------------------#
# The menu manager's name (if the manager is a sub-menu, this is what its
# label will be).
name = Str()
# Does the menu require a separator before the menu item name?
separator = Bool(True)
# ------------------------------------------------------------------------
# 'MenuManager' interface.
# ------------------------------------------------------------------------
def create_menu(self, parent, controller=None):
""" Creates a menu representation of the manager. """
# If a controller is required it can either be set as a trait on the
# menu manager (the trait is part of the 'ActionManager' API), or
# passed in here (if one is passed in here it takes precedence over the
# trait).
if controller is None:
controller = self.controller
return _Menu(self, parent, controller)
# ------------------------------------------------------------------------
# 'ActionManagerItem' interface.
# ------------------------------------------------------------------------
def add_to_menu(self, parent, menu, controller):
""" Adds the item to a menu. """
sub = self.create_menu(parent, controller)
id = sub.GetId()
# fixme: Nasty hack to allow enabling/disabling of menus.
sub._id = id
sub._menu = menu
menu.Append(id, self.name, sub)
def add_to_toolbar(self, parent, tool_bar, image_cache, controller):
""" Adds the item to a tool bar. """
raise ValueError("Cannot add a menu manager to a toolbar.")
class _Menu(wx.Menu):
""" The toolkit-specific menu control. """
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __init__(self, manager, parent, controller):
""" Creates a new tree. """
# Base class constructor.
wx.Menu.__init__(self)
# The parent of the menu.
self._parent = parent
# The manager that the menu is a view of.
self._manager = manager
# The controller.
self._controller = controller
# List of menu items
self.menu_items = []
# Create the menu structure.
self.refresh()
# Listen to the manager being updated.
self._manager.observe(self.refresh, "changed")
self._manager.observe(self._on_enabled_changed, "enabled")
return
def dispose(self):
self._manager.observe(self.refresh, "changed", remove=True)
self._manager.observe(self._on_enabled_changed, "enabled", remove=True)
# Removes event listeners from downstream menu items
self.clear()
# ------------------------------------------------------------------------
# '_Menu' interface.
# ------------------------------------------------------------------------
def clear(self):
""" Clears the items from the menu. """
for item in self.GetMenuItems():
if item.GetSubMenu() is not None:
item.GetSubMenu().clear()
self.Delete(item.GetId())
for item in self.menu_items:
item.dispose()
self.menu_items = []
def is_empty(self):
""" Is the menu empty? """
return self.GetMenuItemCount() == 0
def refresh(self, event=None):
""" Ensures that the menu reflects the state of the manager. """
self.clear()
manager = self._manager
parent = self._parent
previous_non_empty_group = None
for group in manager.groups:
previous_non_empty_group = self._add_group(
parent, group, previous_non_empty_group
)
def show(self, x=None, y=None):
""" Show the menu at the specified location. """
if x is None or y is None:
self._parent.PopupMenu(self)
else:
self._parent.PopupMenu(self, x, y)
return
# ------------------------------------------------------------------------
# Private interface.
# ------------------------------------------------------------------------
def _on_enabled_changed(self, event):
""" Dynamic trait change handler. """
# fixme: Nasty hack to allow enabling/disabling of menus.
#
# We cannot currently (AFAIK) disable menus on the menu bar. Hence
# we don't give them an '_id'...
if hasattr(self, "_id"):
self._menu.Enable(self._id, event.new)
def _add_group(self, parent, group, previous_non_empty_group=None):
""" Adds a group to a menu. """
if len(group.items) > 0:
# Is a separator required?
if previous_non_empty_group is not None and group.separator:
self.AppendSeparator()
# Create actions and sub-menus for each contribution item in
# the group.
for item in group.items:
if isinstance(item, Group):
if len(item.items) > 0:
self._add_group(parent, item, previous_non_empty_group)
if (
previous_non_empty_group is not None
and previous_non_empty_group.separator
and item.separator
):
self.AppendSeparator()
previous_non_empty_group = item
else:
if isinstance(item, MenuManager):
if item.separator:
self.AppendSeparator()
previous_non_empty_group = item
item.add_to_menu(parent, self, self._controller)
previous_non_empty_group = group
return previous_non_empty_group
|