| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" The *definition* of a menu in a menu bar or menu. """ |
| 11 |
|
| 12 |
# Enthought library imports. |
| 13 |
from traits.api import Instance, List, Str |
| 14 |
|
| 15 |
# Local imports. |
| 16 |
from .group import Group |
| 17 |
from .location import Location |
| 18 |
|
| 19 |
|
| 20 |
class CGroup(Instance): |
| 21 |
"""A trait type for a 'Group' or anything that can be cast to a 'Group'. |
| 22 |
|
| 23 |
Currently, the only cast allowed is from string -> Group using the |
| 24 |
string as the group's ID. |
| 25 |
|
| 26 |
""" |
| 27 |
|
| 28 |
########################################################################### |
| 29 |
# 'object' interface. |
| 30 |
########################################################################### |
| 31 |
|
| 32 |
def __init__(self, **kw): |
| 33 |
"""Constructor.""" |
| 34 |
|
| 35 |
super().__init__(klass=Group, **kw) |
| 36 |
|
| 37 |
########################################################################### |
| 38 |
# 'TraitType' interface. |
| 39 |
########################################################################### |
| 40 |
|
| 41 |
def validate(self, object, name, value): |
| 42 |
"""Validate a value.""" |
| 43 |
|
| 44 |
if isinstance(value, str): |
| 45 |
value = Group(id=value) |
| 46 |
|
| 47 |
return super().validate(object, name, value) |
| 48 |
|
| 49 |
|
| 50 |
class Menu(Location): |
| 51 |
"""The *definition* of a menu in a menu bar or menu.""" |
| 52 |
|
| 53 |
# The menu's unique identifier (unique within the group that the menu is to |
| 54 |
# be added to). |
| 55 |
id = Str |
| 56 |
|
| 57 |
# The menu name (appears on the menu bar or menu). |
| 58 |
name = Str |
| 59 |
|
| 60 |
# The groups in the menu. |
| 61 |
groups = List(CGroup) |
| 62 |
|
| 63 |
# The optional name of a class that implements the menu. The class must |
| 64 |
# support the **pyface.action.MenuManager** protocol. |
| 65 |
class_name = Str |
| 66 |
|
| 67 |
########################################################################### |
| 68 |
# 'object' interface |
| 69 |
########################################################################### |
| 70 |
|
| 71 |
def __str__(self): |
| 72 |
"""Return the 'informal' string representation of the object.""" |
| 73 |
|
| 74 |
return "Menu(%s)" % self.name |
| 75 |
|
| 76 |
__repr__ = __str__ |
| 77 |
|
| 78 |
########################################################################### |
| 79 |
# 'Menu' interface |
| 80 |
########################################################################### |
| 81 |
|
| 82 |
def _id_default(self): |
| 83 |
"""Trait initializer.""" |
| 84 |
|
| 85 |
return self.name.strip("&") |