/usr/share/pyshared/PythonCard/helpful.py is in python-pythoncard 0.8.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | """
__version__ = "$Revision: 1.5 $"
__date__ = "$Date: 2006/01/14 12:11:49 $"
"""
# Assorted helpful wrappers.
from PythonCard import model
import wx
import string, copy
import types
# AGT This could probably be generalized some more - but it handles current
# needs within this wrapper collection of cloning buttons and checkboxes.
# utility functions to manipulate controls at run time
class copyControl:
def __init__(self, aBg, Name, newname, Text=""):
Flds = ['position', 'size', 'backgroundColor', 'foregroundColor', 'command', 'font']
aWidget = aBg.components[Name]
d = {}
d['type'] = aWidget.__class__.__name__
for key in Flds: # attributes
# I'm not exactly sure why I have to special-case these tuples
if key == 'bitmap':
# this should get recreated from the file attribute
pass
elif key in ['position', 'size']:
d[key] = getattr(aWidget, key)
elif getattr(aWidget, key) is not None:
d[key] = getattr(aWidget, key)
if Text == "": Text = newname
d['label'] = Text
d['name'] = newname
aBg.components[newname] = d
self.name = newname
# dialog to present a block of text and a number of alternative buttons
class MultiButtonDialog(model.CustomDialog):
def __init__(self, parent, txt, buttons, rsrc):
model.CustomDialog.__init__(self, parent, rsrc)
self.components.Text.text = txt
self.components.Button.visible = False
self.components.Button.enabled = False
if len(buttons) == 0: buttons = ["OK"]
bx, by = self.components.Button.size
for b in buttons:
if isinstance(b, types.StringTypes):
self.components.Button.label = b
else:
self.components.Button.label = b[0]
newx, newy = self.components.Button.GetBestSize()
bx = max(bx, newx)
by = max(by, newy)
self.components.Button.size = (bx, newy)
dx = bx + 20
# check if all buttons will fit in window
owx, owy = self.size
startx, starty = self.components.Button.position
if len(buttons)*dx + bx + 40 > owx:
wx = len(buttons)*dx + bx + 40
self.size = (wx, owy)
startx, starty = (wx-bx-20, owy-by-30) # AGT - why 30 ??
tsx, tsy = self.components.Text.size
self.components.Text.size = (wx-20, tsy)
localbuttons = buttons
localbuttons.reverse()
count = 0
for b in localbuttons:
if isinstance(b, types.StringTypes):
theName = b
theToolTip = ''
else:
theName = b[0]
theToolTip = b[1]
n = copyControl(self, "Button", "Button"+str(count), theName)
self.components[n.name].position = startx-count*dx, starty
self.components[n.name].visible = True
self.components[n.name].enabled = True
self.components[n.name].toolTip = theToolTip
count += 1
self.accepted = False
self.text = ""
def on_mouseClick(self, event):
self.text = event.target.label
if self.text == "Cancel":
self.accepted = False
else:
self.accepted = True
self.Close()
def multiButtonDialog(parent, txt, buttons, title=""):
rsrc = {'type':'CustomDialog',
'name':'Template',
'title':'Template',
'position':(176, 176),
'size':(367, 230),
'components': [
{'type':'StaticText',
'name':'Text',
'position':(10, 10),
'size':(341, 123),
'actionBindings':{},
},
{'type':'Button',
'name':'Button',
'position':(269, 145),
'actionBindings':{},
'label':'template',
},
] # end components
} # end CustomDialog
rsrc["title"] = title
dlg = MultiButtonDialog(parent, txt, buttons, rsrc)
result = dlg.showModal()
result.accepted = dlg.accepted
result.text = dlg.text
dlg.destroy()
return result
# dialog to present a number of on/off checkboxes
class MultiCheckBoxDialog(model.CustomDialog):
# boxes is a list of (name, value) pairs, not a dictionary
# because a dictionary didn't allow the caller to control the order of presentation
def __init__(self, parent, boxes, rsrc):
model.CustomDialog.__init__(self, parent, rsrc)
self.components.box.visible = False
self.components.box.enabled = False
self.components.box.checked = False
# check if all buttons will fit in window
owx, owy = self.size
startx, starty = self.components.box.position
sx,sy = self.components.box.GetBestSize()
#rint starty, len(boxes), sy, (len(boxes)-1) * (sy+20), owy
wy = (len(boxes)+1) * (sy+20) + 30
self.size = (owx, wy)
count = 0
self.boxes = {}
for b in boxes:
val = False
toolTip = ''
if isinstance(b, types.StringTypes):
key = b
else:
key = b[0]
if len(b) > 1: val = b[1]
if len(b) > 2: toolTip = b[2]
n = copyControl(self, "box", "Box"+str(count), key)
self.components[n.name].position = startx, starty+count*(sy+20)
self.components[n.name].visible = True
self.components[n.name].enabled = True
self.components[n.name].checked = val
self.components[n.name].toolTip = toolTip
self.boxes[key] = val
count += 1
self.components.btnOK.position = (150, starty+count*(sy+20))
self.components.btnCancel.position = (250, starty+count*(sy+20))
self.accepted = False
def on_btnOK_mouseClick(self, event):
self.accepted = True
for count in range(len(self.boxes)):
name = "Box"+str(count)
self.boxes[self.components[name].label] = self.components[name].checked
self.Close()
def on_btnCancel_mouseClick(self, event):
self.accepted = False
self.Close()
def multiCheckBoxDialog(parent, boxes, title=""):
rsrc = {'type':'CustomDialog',
'name':'Template',
'title':'Template',
'position':(176, 176),
'size':(340, 230),
'components': [
{'type':'CheckBox',
'name':'box',
'position':(50, 15),
'size':(250,20),
'actionBindings':{},
'label':'template',
},
{'type':'Button',
'name':'btnOK',
'position':(250, 0),
'actionBindings':{},
'label':'OK',
},
{'type':'Button',
'name':'btnCancel',
'position':(320, 0),
'actionBindings':{},
'label':'Cancel',
},
] # end components
} # end CustomDialog
rsrc["title"] = title
dlg = MultiCheckBoxDialog(parent, boxes, rsrc)
result = dlg.showModal()
result.accepted = dlg.accepted
result.boxes = dlg.boxes
dlg.destroy()
return result
# wrapper to help with pop up menus
class PopUpMenu:
def __init__(self, aBg, items, pos):
# Yet another alternate way to do IDs. Some prefer them up top to
# avoid clutter, some prefer them close to the object of interest
# for clarity.
self.popup = {}
self.reverse = {}
self.selected = None
# make a menu
self.menu = wx.Menu()
# add the items
for it in items:
if isinstance(it, types.StringTypes):
Id = wx.NewId()
self.popup[it] = Id
self.reverse[Id] = it
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=self.popup[it])
self.menu.Append(self.popup[it], it)
else:
# make a menu
submenu = wx.Menu()
Id = wx.NewId()
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=Id)
for that in it:
if isinstance(that, types.StringTypes):
Id = wx.NewId()
self.popup[that] = Id
self.reverse[Id] = that
aBg.Bind(wx.EVT_MENU, self.OnPopup, id=self.popup[that])
submenu.Append(self.popup[that], that)
self.menu.AppendMenu(Id, "Test Submenu", submenu)
# Popup the menu. If an item is selected then its handler
# will be called before PopupMenu returns.
aBg.PopupMenu(self.menu, pos)
self.menu.Destroy()
def OnPopup(self, event):
self.selected = self.reverse[event.GetId()]
def popUpMenu(aBg, items, pos):
menu = PopUpMenu(aBg, items, pos)
return menu.selected
|