1 |
# Copyright (C) 2001, 2002 by Intevation GmbH |
2 |
# Author: |
3 |
# Bernhard Herzog <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
"""Menu management""" |
9 |
|
10 |
__version__ = "$Revision$" |
11 |
|
12 |
|
13 |
class Menu: |
14 |
|
15 |
"""Represent a menu or submenu. |
16 |
|
17 |
A menu has a name and a title. The name can be used to identify |
18 |
menus internally while the title is intended for use in the GUI. |
19 |
|
20 |
Menu items can be added with the Insert* methods. |
21 |
""" |
22 |
|
23 |
def __init__(self, name, title, items = None): |
24 |
"""Initialize the menu. |
25 |
|
26 |
Parameters: |
27 |
name -- the name of the menu |
28 |
title -- the (possibly localized) title of the menu |
29 |
items -- (optional) a list of menu items. |
30 |
|
31 |
The items list may contains strings with command names, None to |
32 |
indicate separators or Menu instances for submenus. |
33 |
""" |
34 |
self.name = name |
35 |
self.title = title |
36 |
if items is None: |
37 |
self.items = [] |
38 |
else: |
39 |
self.items = items |
40 |
|
41 |
def item_index(self, item): |
42 |
"""Return the index of item in the menu. |
43 |
|
44 |
Return None it item is not found. |
45 |
""" |
46 |
try: |
47 |
return self.items.index(item) |
48 |
except ValueError: |
49 |
return None |
50 |
|
51 |
def find_menu(self, name): |
52 |
"""Return the submenu named name or None if no such item exists""" |
53 |
for item in self.items: |
54 |
if isinstance(item, Menu) and item.name == name: |
55 |
return item |
56 |
return None |
57 |
|
58 |
def InsertItem(self, item, menu = (), after = None): |
59 |
"""Insert a menu item. |
60 |
|
61 |
Parameters: |
62 |
|
63 |
item -- the menu item to insert must be either a string with |
64 |
the command's name or a Menu instance. |
65 |
|
66 |
menu -- (optional) the submenu to insert into. It should be a |
67 |
sequence of menu names. |
68 |
|
69 |
after -- (optional) insert the new item after this one. after |
70 |
should be the name of a command. |
71 |
""" |
72 |
# if menu is given, get the first submenu |
73 |
if menu: |
74 |
submenu_index = self.find_menu(menu[0]) |
75 |
if submenu_index is not None: |
76 |
submenu_index.InsertItem(item, menu = menu[1:], after = after) |
77 |
else: |
78 |
# the submenu doesn't exist yet. Raise an error. |
79 |
raise KeyError("Submenu %s doesn't exist" % menu[0]) |
80 |
else: |
81 |
if after is not None: |
82 |
idx = self.item_index(after) |
83 |
else: |
84 |
idx = None |
85 |
|
86 |
if idx is not None: |
87 |
self.items.insert(idx + 1, item) |
88 |
else: |
89 |
self.items.append(item) |
90 |
|
91 |
def InsertSeparator(self): |
92 |
"""Insert a separator""" |
93 |
self.InsertItem(None) |
94 |
|
95 |
def InsertMenu(self, name, title, menu = (), after = None): |
96 |
"""Insert and return a new menu. |
97 |
|
98 |
Parameters: |
99 |
|
100 |
name -- the (internal) name of the menu |
101 |
|
102 |
title -- the (possibly localized) title |
103 |
|
104 |
menu -- (optional) the submenu to insert into. It should be a |
105 |
sequence of menu names. |
106 |
|
107 |
after -- (optional) insert the new item after this one. after |
108 |
should be the name of a command. |
109 |
""" |
110 |
newmenu = Menu(name, title) |
111 |
self.InsertItem(newmenu, menu = menu, after = after) |
112 |
return newmenu |
113 |
|
114 |
def SetItems(self, items): |
115 |
"""Replace the contents of the menu by items.""" |
116 |
self.items = items |
117 |
|