2
__version__ = "$Revision: 1.5 $"
3
__date__ = "$Date: 2006/01/14 12:11:49 $"
6
# Assorted helpful wrappers.
8
from PythonCard import model
15
# AGT This could probably be generalized some more - but it handles current
16
# needs within this wrapper collection of cloning buttons and checkboxes.
18
# utility functions to manipulate controls at run time
20
def __init__(self, aBg, Name, newname, Text=""):
21
Flds = ['position', 'size', 'backgroundColor', 'foregroundColor', 'command', 'font']
22
aWidget = aBg.components[Name]
24
d['type'] = aWidget.__class__.__name__
25
for key in Flds: # attributes
26
# I'm not exactly sure why I have to special-case these tuples
28
# this should get recreated from the file attribute
30
elif key in ['position', 'size']:
31
d[key] = getattr(aWidget, key)
32
elif getattr(aWidget, key) is not None:
33
d[key] = getattr(aWidget, key)
34
if Text == "": Text = newname
37
aBg.components[newname] = d
41
# dialog to present a block of text and a number of alternative buttons
42
class MultiButtonDialog(model.CustomDialog):
44
def __init__(self, parent, txt, buttons, rsrc):
45
model.CustomDialog.__init__(self, parent, rsrc)
46
self.components.Text.text = txt
47
self.components.Button.visible = False
48
self.components.Button.enabled = False
49
if len(buttons) == 0: buttons = ["OK"]
50
bx, by = self.components.Button.size
52
if isinstance(b, types.StringTypes):
53
self.components.Button.label = b
55
self.components.Button.label = b[0]
57
newx, newy = self.components.Button.GetBestSize()
60
self.components.Button.size = (bx, newy)
62
# check if all buttons will fit in window
64
startx, starty = self.components.Button.position
65
if len(buttons)*dx + bx + 40 > owx:
66
wx = len(buttons)*dx + bx + 40
68
startx, starty = (wx-bx-20, owy-by-30) # AGT - why 30 ??
69
tsx, tsy = self.components.Text.size
70
self.components.Text.size = (wx-20, tsy)
71
localbuttons = buttons
72
localbuttons.reverse()
74
for b in localbuttons:
75
if isinstance(b, types.StringTypes):
81
n = copyControl(self, "Button", "Button"+str(count), theName)
82
self.components[n.name].position = startx-count*dx, starty
83
self.components[n.name].visible = True
84
self.components[n.name].enabled = True
85
self.components[n.name].toolTip = theToolTip
90
def on_mouseClick(self, event):
91
self.text = event.target.label
92
if self.text == "Cancel":
98
def multiButtonDialog(parent, txt, buttons, title=""):
99
rsrc = {'type':'CustomDialog',
102
'position':(176, 176),
106
{'type':'StaticText',
115
'position':(269, 145),
122
rsrc["title"] = title
123
dlg = MultiButtonDialog(parent, txt, buttons, rsrc)
124
result = dlg.showModal()
125
result.accepted = dlg.accepted
126
result.text = dlg.text
131
# dialog to present a number of on/off checkboxes
132
class MultiCheckBoxDialog(model.CustomDialog):
134
# boxes is a list of (name, value) pairs, not a dictionary
135
# because a dictionary didn't allow the caller to control the order of presentation
136
def __init__(self, parent, boxes, rsrc):
137
model.CustomDialog.__init__(self, parent, rsrc)
138
self.components.box.visible = False
139
self.components.box.enabled = False
140
self.components.box.checked = False
141
# check if all buttons will fit in window
143
startx, starty = self.components.box.position
144
sx,sy = self.components.box.GetBestSize()
145
#rint starty, len(boxes), sy, (len(boxes)-1) * (sy+20), owy
146
wy = (len(boxes)+1) * (sy+20) + 30
147
self.size = (owx, wy)
154
if isinstance(b, types.StringTypes):
158
if len(b) > 1: val = b[1]
159
if len(b) > 2: toolTip = b[2]
160
n = copyControl(self, "box", "Box"+str(count), key)
161
self.components[n.name].position = startx, starty+count*(sy+20)
162
self.components[n.name].visible = True
163
self.components[n.name].enabled = True
164
self.components[n.name].checked = val
165
self.components[n.name].toolTip = toolTip
166
self.boxes[key] = val
168
self.components.btnOK.position = (150, starty+count*(sy+20))
169
self.components.btnCancel.position = (250, starty+count*(sy+20))
170
self.accepted = False
172
def on_btnOK_mouseClick(self, event):
175
for count in range(len(self.boxes)):
176
name = "Box"+str(count)
177
self.boxes[self.components[name].label] = self.components[name].checked
181
def on_btnCancel_mouseClick(self, event):
182
self.accepted = False
185
def multiCheckBoxDialog(parent, boxes, title=""):
186
rsrc = {'type':'CustomDialog',
189
'position':(176, 176),
217
rsrc["title"] = title
218
dlg = MultiCheckBoxDialog(parent, boxes, rsrc)
219
result = dlg.showModal()
220
result.accepted = dlg.accepted
221
result.boxes = dlg.boxes
225
# wrapper to help with pop up menus
228
def __init__(self, aBg, items, pos):
229
# Yet another alternate way to do IDs. Some prefer them up top to
230
# avoid clutter, some prefer them close to the object of interest
236
self.menu = wx.Menu()
239
if isinstance(it, types.StringTypes):
242
self.reverse[Id] = it
243
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=self.popup[it])
244
self.menu.Append(self.popup[it], it)
249
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=Id)
251
if isinstance(that, types.StringTypes):
253
self.popup[that] = Id
254
self.reverse[Id] = that
255
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=self.popup[that])
256
submenu.Append(self.popup[that], that)
258
self.menu.AppendMenu(Id, "Test Submenu", submenu)
260
# Popup the menu. If an item is selected then its handler
261
# will be called before PopupMenu returns.
262
aBg.PopupMenu(self.menu, pos)
265
def OnPopup(self, event):
266
self.selected = self.reverse[event.GetId()]
268
def popUpMenu(aBg, items, pos):
269
menu = PopUpMenu(aBg, items, pos)