1 # Based on iwidgets2.2.0/buttonbox.itk code.
7 class ButtonBox(Pmw.MegaWidget):
8 def __init__(self, parent = None, **kw):
10 # Define the megawidget options.
13 ('labelmargin', 0, INITOPT),
14 ('labelpos', None, INITOPT),
15 ('orient', 'horizontal', INITOPT),
19 self.defineoptions(kw, optiondefs, dynamicGroups = ('Button',))
21 # Initialise the base class (after defining the options).
22 Pmw.MegaWidget.__init__(self, parent)
24 # Create the components.
25 interior = self.interior()
26 if self['labelpos'] is None:
27 self._buttonBoxFrame = self._hull
30 self._buttonBoxFrame = self.createcomponent('frame',
32 Tkinter.Frame, (interior,))
33 self._buttonBoxFrame.grid(column=2, row=2, sticky='nsew')
36 self.createlabel(interior)
38 orient = self['orient']
39 if orient == 'horizontal':
40 interior.grid_columnconfigure(columnOrRow, weight = 1)
41 elif orient == 'vertical':
42 interior.grid_rowconfigure(columnOrRow, weight = 1)
44 raise ValueError, 'bad orient option ' + repr(orient) + \
45 ': must be either \'horizontal\' or \'vertical\''
47 # Initialise instance variables.
49 # List of tuples describing the buttons:
54 # The index of the default button.
55 self._defaultButton = None
59 # Check keywords and initialise options.
60 self.initialiseoptions()
64 self.after_cancel(self._timerId)
66 Pmw.MegaWidget.destroy(self)
69 return len(self._buttonList)
71 def index(self, index, forInsert = 0):
72 listLength = len(self._buttonList)
73 if type(index) == types.IntType:
74 if forInsert and index <= listLength:
76 elif not forInsert and index < listLength:
79 raise ValueError, 'index "%s" is out of range' % index
80 elif index is Pmw.END:
86 raise ValueError, 'ButtonBox has no buttons'
87 elif index is Pmw.DEFAULT:
88 if self._defaultButton is not None:
89 return self._defaultButton
90 raise ValueError, 'ButtonBox has no default'
92 names = map(lambda t: t[0], self._buttonList)
94 return names.index(index)
95 validValues = 'a name, a number, Pmw.END or Pmw.DEFAULT'
97 'bad index "%s": must be %s' % (index, validValues)
99 def insert(self, componentName, beforeComponent = 0, **kw):
100 if componentName in self.components():
101 raise ValueError, 'button "%s" already exists' % componentName
102 if not kw.has_key('text'):
103 kw['text'] = componentName
104 kw['default'] = 'normal'
105 button = apply(self.createcomponent, (componentName,
107 Tkinter.Button, (self._buttonBoxFrame,)), kw)
109 index = self.index(beforeComponent, 1)
110 horizontal = self['orient'] == 'horizontal'
111 numButtons = len(self._buttonList)
113 # Shift buttons up one position.
114 for i in range(numButtons - 1, index - 1, -1):
115 widget = self._buttonList[i][1]
118 widget.grid(column = pos, row = 0)
120 widget.grid(column = 0, row = pos)
122 # Display the new button.
124 button.grid(column = index * 2 + 1, row = 0, sticky = 'ew',
125 padx = self['padx'], pady = self['pady'])
126 self._buttonBoxFrame.grid_columnconfigure(
127 numButtons * 2 + 2, weight = 1)
129 button.grid(column = 0, row = index * 2 + 1, sticky = 'ew',
130 padx = self['padx'], pady = self['pady'])
131 self._buttonBoxFrame.grid_rowconfigure(
132 numButtons * 2 + 2, weight = 1)
133 self._buttonList.insert(index, (componentName, button))
137 def add(self, componentName, **kw):
138 return apply(self.insert, (componentName, len(self._buttonList)), kw)
140 def delete(self, index):
141 index = self.index(index)
142 (name, widget) = self._buttonList[index]
144 self.destroycomponent(name)
146 numButtons = len(self._buttonList)
148 # Shift buttons down one position.
149 horizontal = self['orient'] == 'horizontal'
150 for i in range(index + 1, numButtons):
151 widget = self._buttonList[i][1]
154 widget.grid(column = pos, row = 0)
156 widget.grid(column = 0, row = pos)
159 self._buttonBoxFrame.grid_columnconfigure(numButtons * 2 - 1,
161 self._buttonBoxFrame.grid_columnconfigure(numButtons * 2, weight = 0)
163 self._buttonBoxFrame.grid_rowconfigure(numButtons * 2, weight = 0)
164 del self._buttonList[index]
166 def setdefault(self, index):
167 # Turn off the default ring around the current default button.
168 if self._defaultButton is not None:
169 button = self._buttonList[self._defaultButton][1]
170 button.configure(default = 'normal')
171 self._defaultButton = None
173 # Turn on the default ring around the new default button.
174 if index is not None:
175 index = self.index(index)
176 self._defaultButton = index
177 button = self._buttonList[index][1]
178 button.configure(default = 'active')
180 def invoke(self, index = Pmw.DEFAULT, noFlash = 0):
181 # Invoke the callback associated with the *index* button. If
182 # *noFlash* is not set, flash the button to indicate to the
183 # user that something happened.
185 button = self._buttonList[self.index(index)][1]
187 state = button.cget('state')
188 relief = button.cget('relief')
189 button.configure(state = 'active', relief = 'sunken')
190 self.update_idletasks()
192 button.configure(state = state, relief = relief)
193 return button.invoke()
195 def button(self, buttonIndex):
196 return self._buttonList[self.index(buttonIndex)][1]
198 def alignbuttons(self, when = 'later'):
200 if not self._timerId:
201 self._timerId = self.after_idle(self.alignbuttons, 'now')
203 self.update_idletasks()
206 # Determine the width of the maximum length button.
208 horizontal = (self['orient'] == 'horizontal')
209 for index in range(len(self._buttonList)):
210 gridIndex = index * 2 + 1
212 width = self._buttonBoxFrame.grid_bbox(gridIndex, 0)[2]
214 width = self._buttonBoxFrame.grid_bbox(0, gridIndex)[2]
218 # Set the width of all the buttons to be the same.
220 for index in range(len(self._buttonList)):
221 self._buttonBoxFrame.grid_columnconfigure(index * 2 + 1,
224 self._buttonBoxFrame.grid_columnconfigure(0, minsize = max)