1 # Manager widget for menus.
8 class MenuBar(Pmw.MegaWidget):
10 def __init__(self, parent = None, **kw):
12 # Define the megawidget options.
15 ('balloon', None, None),
16 ('hotkeys', 1, INITOPT),
19 self.defineoptions(kw, optiondefs, dynamicGroups = ('Menu', 'Button'))
21 # Initialise the base class (after defining the options).
22 Pmw.MegaWidget.__init__(self, parent)
25 # Map from a menu name to a tuple of information about the menu.
26 # The first item in the tuple is the name of the parent menu (for
27 # toplevel menus this is None). The second item in the tuple is
28 # a list of status help messages for each item in the menu.
29 # The third item in the tuple is the id of the binding used
30 # to detect mouse motion to display status help.
31 # Information for the toplevel menubuttons is not stored here.
33 self._mydeletecommand = self.component('hull').tk.deletecommand
34 # Cache this method for use later.
36 # Check keywords and initialise options.
37 self.initialiseoptions()
39 def deletemenuitems(self, menuName, start, end = None):
40 self.component(menuName + '-menu').delete(start, end)
42 del self._menuInfo[menuName][1][start]
44 self._menuInfo[menuName][1][start:end+1] = []
46 def deletemenu(self, menuName):
47 """Delete should be called for cascaded menus before main menus.
50 # Clean up binding for this menu.
51 parentName = self._menuInfo[menuName][0]
52 bindId = self._menuInfo[menuName][2]
53 _bindtag = 'PmwMenuBar' + str(self) + menuName
54 self.unbind_class(_bindtag, '<Motion>')
55 self._mydeletecommand(bindId) # unbind_class does not clean up
56 del self._menuInfo[menuName]
58 if parentName is None:
59 self.destroycomponent(menuName + '-button')
61 parentMenu = self.component(parentName + '-menu')
63 menu = self.component(menuName + '-menu')
65 for item in range(parentMenu.index('end') + 1):
66 if parentMenu.type(item) == 'cascade':
67 itemMenu = str(parentMenu.entrycget(item, 'menu'))
68 if itemMenu == menuId:
69 parentMenu.delete(item)
70 del self._menuInfo[parentName][1][item]
73 self.destroycomponent(menuName + '-menu')
76 for menuName in self._menuInfo.keys():
77 if self._menuInfo[menuName][0] is None:
78 menubutton = self.component(menuName + '-button')
79 menubutton.configure(state = 'disabled')
82 for menuName in self._menuInfo.keys():
83 if self._menuInfo[menuName][0] is None:
84 menubutton = self.component(menuName + '-button')
85 menubutton.configure(state = 'normal')
87 def addmenu(self, menuName, balloonHelp, statusHelp = None,
88 side = 'left', traverseSpec = None, **kw):
90 self._addmenu(None, menuName, balloonHelp, statusHelp,
91 traverseSpec, side, 'text', kw)
93 def addcascademenu(self, parentMenuName, menuName, statusHelp = '',
94 traverseSpec = None, **kw):
96 self._addmenu(parentMenuName, menuName, None, statusHelp,
97 traverseSpec, None, 'label', kw)
99 def _addmenu(self, parentMenuName, menuName, balloonHelp, statusHelp,
100 traverseSpec, side, textKey, kw):
102 if (menuName + '-menu') in self.components():
103 raise ValueError, 'menu "%s" already exists' % menuName
106 if kw.has_key('tearoff'):
107 menukw['tearoff'] = kw['tearoff']
110 menukw['tearoff'] = 0
112 if not kw.has_key(textKey):
113 kw[textKey] = menuName
115 self._addHotkeyToOptions(parentMenuName, kw, textKey, traverseSpec)
117 if parentMenuName is None:
118 button = apply(self.createcomponent, (menuName + '-button',
120 Tkinter.Menubutton, (self.interior(),)), kw)
121 button.pack(side=side, padx = self['padx'])
122 balloon = self['balloon']
123 if balloon is not None:
124 balloon.bind(button, balloonHelp, statusHelp)
127 parentMenu = self.component(parentMenuName + '-menu')
128 apply(parentMenu.add_cascade, (), kw)
129 self._menuInfo[parentMenuName][1].append(statusHelp)
131 menu = apply(self.createcomponent, (menuName + '-menu',
133 Tkinter.Menu, (parentMenu,)), menukw)
134 if parentMenuName is None:
135 button.configure(menu = menu)
137 parentMenu.entryconfigure('end', menu = menu)
139 # Need to put this binding after the class bindings so that
140 # menu.index() does not lag behind.
141 _bindtag = 'PmwMenuBar' + str(self) + menuName
142 bindId = self.bind_class(_bindtag, '<Motion>',
143 lambda event=None, self=self, menuName=menuName:
144 self._menuHelp(menuName))
145 menu.bindtags(menu.bindtags() + (_bindtag,))
146 menu.bind('<Leave>', self._resetHelpmessage)
148 self._menuInfo[menuName] = (parentMenuName, [], bindId)
150 def addmenuitem(self, menuName, itemType, statusHelp = '',
151 traverseSpec = None, **kw):
153 menu = self.component(menuName + '-menu')
154 if itemType != 'separator':
155 self._addHotkeyToOptions(menuName, kw, 'label', traverseSpec)
157 if itemType == 'command':
158 command = menu.add_command
159 elif itemType == 'separator':
160 command = menu.add_separator
161 elif itemType == 'checkbutton':
162 command = menu.add_checkbutton
163 elif itemType == 'radiobutton':
164 command = menu.add_radiobutton
165 elif itemType == 'cascade':
166 command = menu.add_cascade
168 raise ValueError, 'unknown menuitem type "%s"' % itemType
170 self._menuInfo[menuName][1].append(statusHelp)
171 apply(command, (), kw)
173 def _addHotkeyToOptions(self, menuName, kw, textKey, traverseSpec):
175 if (not self['hotkeys'] or kw.has_key('underline') or
176 not kw.has_key(textKey)):
179 if type(traverseSpec) == types.IntType:
180 kw['underline'] = traverseSpec
185 for menuName in self._menuInfo.keys():
186 if self._menuInfo[menuName][0] is None:
187 menubutton = self.component(menuName + '-button')
188 underline = string.atoi(str(menubutton.cget('underline')))
190 label = str(menubutton.cget(textKey))
191 if underline < len(label):
192 hotkey = string.lower(label[underline])
193 if hotkey not in hotkeyList:
194 hotkeyList.append(hotkey)
196 menu = self.component(menuName + '-menu')
197 end = menu.index('end')
199 for item in range(end + 1):
200 if menu.type(item) not in ('separator', 'tearoff'):
201 underline = string.atoi(
202 str(menu.entrycget(item, 'underline')))
204 label = str(menu.entrycget(item, textKey))
205 if underline < len(label):
206 hotkey = string.lower(label[underline])
207 if hotkey not in hotkeyList:
208 hotkeyList.append(hotkey)
212 if type(traverseSpec) == types.StringType:
213 lowerLetter = string.lower(traverseSpec)
214 if traverseSpec in name and lowerLetter not in hotkeyList:
215 kw['underline'] = string.index(name, traverseSpec)
217 targets = string.digits + string.letters
218 lowerName = string.lower(name)
219 for letter_index in range(len(name)):
220 letter = lowerName[letter_index]
221 if letter in targets and letter not in hotkeyList:
222 kw['underline'] = letter_index
225 def _menuHelp(self, menuName):
226 menu = self.component(menuName + '-menu')
227 index = menu.index('active')
229 balloon = self['balloon']
230 if balloon is not None:
232 balloon.showstatus('')
234 if str(menu.cget('tearoff')) == '1':
237 help = self._menuInfo[menuName][1][index]
238 balloon.showstatus(help)
240 def _resetHelpmessage(self, event=None):
241 balloon = self['balloon']
242 if balloon is not None:
243 balloon.clearstatus()