1
# Copyright (C) 2001, 2002 by Intevation GmbH
3
# Bernhard Herzog <bh@intevation.de>
5
# This program is free software under the GPL (>=v2)
6
# Read the file COPYING coming with Thuban for details.
10
__version__ = "$Revision: 1.6 $"
16
"""Represent a menu or submenu.
18
A menu has a name and a title. The name can be used to identify
19
menus internally while the title is intended for use in the GUI.
21
Menu items can be added with the Insert* methods.
24
def __init__(self, name, title, items = None):
25
"""Initialize the menu.
28
name -- the name of the menu
29
title -- the (possibly localized) title of the menu
30
items -- (optional) a list of menu items.
32
The items list may contains strings with command names, None to
33
indicate separators or Menu instances for submenus.
42
def item_index(self, item):
43
"""Return the index of item in the menu.
45
item -- may be the name of a non-menu entry or the
46
name of a menu or a menu itself.
48
Return None it item is not found.
50
for i in range(len(self.items)):
53
# this case takes care of item being the name of an
56
elif isinstance(temp, Menu) and temp.name == item:
57
# item is the name of a menu
59
# Didn't find the item so return None
62
def find_menu(self, name):
63
"""Return the submenu named name or None if no such item exists"""
64
for item in self.items:
65
if isinstance(item, Menu) and item.name == name:
69
def InsertItem(self, item, menu = (), after = None):
70
"""Insert a menu item.
74
item -- the menu item to insert must be either a string with
75
the command's name or a Menu instance.
77
menu -- (optional) the submenu to insert into. It should be a
78
sequence of menu names.
80
after -- (optional) insert the new item after this one. after
81
should be the name of a command.
83
# if menu is given, get the first submenu
85
submenu_index = self.find_menu(menu[0])
86
if submenu_index is not None:
87
submenu_index.InsertItem(item, menu = menu[1:], after = after)
89
# the submenu doesn't exist yet. Raise an error.
90
raise KeyError(_("Submenu %s doesn't exist") % menu[0])
93
idx = self.item_index(after)
98
self.items.insert(idx + 1, item)
100
self.items.append(item)
102
def InsertSeparator(self, after = None):
103
"""Insert a separator
105
after -- (optional) insert the separator after this one. after
106
should be the name of a command.
108
self.InsertItem(None, after = after)
110
def InsertMenu(self, name, title, menu = (), after = None):
111
"""Insert and return a new menu.
115
name -- the (internal) name of the menu
117
title -- the (possibly localized) title
119
menu -- (optional) the submenu to insert into. It should be a
120
sequence of menu names.
122
after -- (optional) insert the new item after this one. after
123
should be the name of a command.
125
newmenu = Menu(name, title)
126
self.InsertItem(newmenu, menu = menu, after = after)
129
def SetItems(self, items):
130
"""Replace the contents of the menu by items."""
133
def RemoveItem(self, item):
134
"""Remove an item from the menu.
136
item -- the (internal) name of the item.
138
i = self.item_index(item)