/usr/lib/python2.7/dist-packages/PythonCard/components/multicolumnlist.py is in python-pythoncard 0.8.2-5.
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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | """
+__version__ = "$Revision: 1.28 $"
+__date__ = "$Date: 2004/11/07 18:13:13 $"
"""
import wx
from wx.lib.mixins.listctrl import ColumnSorterMixin, ListCtrlAutoWidthMixin
from types import TupleType, ListType, StringTypes, NoneType, IntType
from PythonCard import event, widget
class MultiColumnListEvent(event.Event):
def decorate(self, aWxEvent, source):
aWxEvent = event.Event.decorate(self, aWxEvent, source)
aWxEvent.item = aWxEvent.GetItem()
return aWxEvent
class MultiColumnListSelectEvent(MultiColumnListEvent, event.CommandTypeEvent):
name = 'select'
binding = wx.EVT_LIST_ITEM_SELECTED
id = wx.wxEVT_COMMAND_LIST_ITEM_SELECTED
class MultiColumnListItemActivatedEvent(MultiColumnListEvent):
name = 'itemActivated'
binding = wx.EVT_LIST_ITEM_ACTIVATED
id = wx.wxEVT_COMMAND_LIST_ITEM_ACTIVATED
class MultiColumnListItemFocusedEvent(MultiColumnListEvent):
name = 'itemFocused'
binding = wx.EVT_LIST_ITEM_FOCUSED
id = wx.wxEVT_COMMAND_LIST_ITEM_FOCUSED
class MultiColumnListMouseContextClickEvent(MultiColumnListEvent):
name = 'mouseContextClick'
binding = wx.EVT_LIST_ITEM_RIGHT_CLICK
id = wx.wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
class MultiColumnListKeyDownEvent(MultiColumnListEvent):
name = 'keyDown'
binding = wx.EVT_LIST_KEY_DOWN
id = wx.wxEVT_COMMAND_LIST_KEY_DOWN
def decorate(self, aWxEvent, source):
aWxEvent = MultiColumnListEvent.decorate(self, aWxEvent, source)
aWxEvent.keyCode = aWxEvent.GetKeyCode()
return aWxEvent
class MultiColumnListColumnClickEvent(MultiColumnListEvent):
name = 'columnClick'
binding = wx.EVT_LIST_COL_CLICK
id = wx.wxEVT_COMMAND_LIST_COL_CLICK
MultiColumnListEvents =(MultiColumnListSelectEvent,
MultiColumnListItemActivatedEvent,
MultiColumnListItemFocusedEvent,
MultiColumnListMouseContextClickEvent,
MultiColumnListKeyDownEvent,
MultiColumnListColumnClickEvent)
class MultiColumnListSpec(widget.WidgetSpec):
def __init__(self):
## events =[event.SelectEvent,
## event.ItemActivatedEvent,
## event.ItemFocusedEvent,
## event.MouseContextClickEvent,
## event.ListColumnClickEvent,
## event.KeyDownEvent]
events = list(MultiColumnListEvents)
attributes ={
'items' : { 'presence' : 'optional', 'default' : [] },
#'selected' : { 'presence' : 'optional', 'default' : None },
'maxColumns' : { 'presence' : 'optional', 'default' : 20 },
'columnHeadings' : { 'presence' : 'optional', 'default' : [] },
'rules' : { 'presence' : 'optional', 'default' : 1 },
#'style' : { 'presence' : 'optional', 'default' : [], 'values' : [ 'horizontal', 'vertical' ] },
'size' : { 'presence' : 'optional', 'default' : [ 50, 50 ] },
}
widget.WidgetSpec.__init__( self, 'MultiColumnList', 'Widget', events, attributes )
class MultiColumnList(widget.Widget, wx.ListCtrl, ColumnSorterMixin, ListCtrlAutoWidthMixin):
"""
A multi-column list.
"""
_spec = MultiColumnListSpec()
def __init__( self, aParent, aResource ) :
if aResource.rules:
rules = wx.LC_HRULES | wx.LC_VRULES
else:
rules = 0
self._rules = aResource.rules
self._maxColumns = aResource.maxColumns
self._autoresize = 1
wx.ListCtrl.__init__(
self,
aParent,
widget.makeNewId(aResource.id),
aResource.position,
aResource.size,
#aResource.items,
style = rules | wx.LC_REPORT | \
wx.NO_FULL_REPAINT_ON_RESIZE | wx.CLIP_SIBLINGS,
name = aResource.name
)
widget.Widget.__init__( self, aParent, aResource )
self.itemDataMap = {}
# Now that the list exists we can init the other base class,
# see wxPython/lib/mixins/listctrl.py
ColumnSorterMixin.__init__(self, self._maxColumns)
# Perform init for AutoWidth (resizes the last column to take up
# the remaining display width)
ListCtrlAutoWidthMixin.__init__(self)
#if aResource.selected != "" and aResource.selected :
# self._setSelection( aResource.selected )
# After creation we can set the headings
self._setColumnHeadings(aResource.columnHeadings)
# And load the list
self._setItems(aResource.items)
self._bindEvents(event.WIDGET_EVENTS + MultiColumnListEvents)
# Emulate some listBox methods
def Clear(self):
self.DeleteAllItems()
self.itemDataMap = {}
# Emulate some listBox methods
def GetCount(self):
return self.GetItemCount()
# Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
def GetListCtrl(self):
return self
"""
# KEA 2003-09-04
# workaround typo bug in wxPython 2.4.1.2 controls2.py
def GetColumn(self, *_args, **_kwargs):
val = controls2c.ListCtrl_GetColumn(self, *_args, **_kwargs)
if val is not None: val.thisown = 1
return val
"""
def _getColumnHeadings(self):
return self._columnHeadings
def GetColumnHeadingInfo(self):
numcols = self.GetColumnCount()
result = [None] * numcols
if self._autoresize:
for i in xrange(numcols):
listItem = self.GetColumn(i)
result[i] = [listItem.GetText(), wx.LIST_AUTOSIZE, listItem.GetAlign()]
else:
for i in xrange(numcols):
listItem = self.GetColumn(i)
result[i] = [listItem.GetText(), listItem.GetWidth(), listItem.GetAlign()]
return result
def GetSelectedItems(self):
numcols = self.GetColumnCount()
numitems = self.GetSelectedItemCount()
items = [None] * numitems
GetNextItem = self.GetNextItem
if numcols == 1:
GetItemText = self.GetItemText
itemidx = -1
for i in xrange( numitems ):
itemidx = GetNextItem(itemidx, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if itemidx == -1:
#Odd, selection changed?
break
items[i] = GetItemText(itemidx)
else:
GetItem = self.GetItem
cols = range(numcols)
itemidx = -1
for i in xrange(numitems) :
itemidx = GetNextItem(itemidx, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if itemidx == -1:
#Odd, selection changed?
break
items[i] = map(lambda x: GetItem(itemidx, x).GetText(), cols)
return items
# Emulate some listBox methods
def getStringSelection(self):
return self.GetSelectedItems()
# Emulate some listBox methods
def Append(self, aList):
self.InsertItems(aList, self.GetItemCount())
# Emulate some listBox methods
def InsertItems(self, aList, position):
if not isinstance(aList, ListType) and not isinstance(aList, TupleType):
raise AttributeError, "unsupported type, list expected"
if len(aList) == 0:
return
numcols = self.GetColumnCount()
numitems = self.GetItemCount()
# If the list is empty or uninitialized fake an assignment and return
if numitems == 0 or numcols == 0:
self._setItems(aList)
return
# Convert our input into a list of list entries of the appropriate
# number of columns.
if isinstance(aList[0], ListType) or isinstance(aList[0], TupleType):
if isinstance(aList[0], TupleType):
aList = list(aList)
if numcols == len(aList[0]):
pass
elif numcols > len(aList[0]):
blanks = [''] * (numcols - len(aList[0]))
aList = map(lambda x:x + blanks, aList)
else:
aList = map(lambda x:x[:numcols], aList)
elif isinstance(aList[0], StringTypes):
blanks = [''] * (numcols - 1)
aList = map(lambda x:[x] + blanks, aList)
else:
raise AttributeError, "unsupported type, list or string expected"
# Allow negative indexing to mean from the end of the list
if position < 0:
position = numitems + position
# But only allow from the start of the list on
if position < 0:
postiion = 0
# If inserting within the current span of the list, we have
# to copy the portion below the insertion point
if position < numitems:
currentitems = self._getItems()[position:]
if isinstance(currentitems[0], StringTypes):
currentitems = map(lambda x:[x], currentitems)
aList = aList + currentitems
datamap = self.itemDataMap
max = [0] * numcols
for i in xrange(len(aList)):
offset = position + i
l = len(aList[i][0])
if l > max[0]:
max[0] = l
if offset >= numitems:
self.InsertStringItem(offset, aList[i][0])
else:
self.SetStringItem(offset, 0, aList[i][0])
for j in range(1,numcols):
l = len(aList[i][j])
if l > max[j]:
max[j] = l
self.SetStringItem(offset, j, aList[i][j])
self.SetItemData(offset, offset)
datamap[offset] = aList[i]
if self._autoresize:
charwidth = self.GetCharWidth()
maxwidth = self.GetBestVirtualSize()[0]*2
for i in range(numcols):
hdrwidth = (len(self._columnHeadings[i])+1) * charwidth
colwidth = (max[i]+2) * charwidth
curcolwidth = self.GetColumnWidth(i)
if colwidth < curcolwidth:
colwidth = curcolwidth
if colwidth < hdrwidth:
colwidth = hdrwidth
if colwidth < 20:
colwidth = 20
elif colwidth > maxwidth:
colwidth = maxwidth
self.SetColumnWidth(i, colwidth)
self.resizeLastColumn(self.GetColumnWidth(numcols-1))
def _setColumnHeadings(self, aList):
if isinstance(aList, ListType) or isinstance(aList, TupleType) or isinstance(aList, StringTypes):
pass
else:
raise Exception( 'invalid MultiColumnList.SetHeading value: %s' % aList )
self.ClearAll()
self.itemDataMap = {}
self._autoresize = 1
if isinstance(aList, StringTypes):
self.InsertColumn(0,aList,width=self.GetBestVirtualSize()[0])
self._columnHeadings = [aList]
return
elif isinstance(aList, TupleType):
aList = list(aList)
self._columnHeadings = aList
numcols = len(aList)
if numcols == 0:
return
elif numcols > self._maxColumns:
numcols = self._maxColumns
self._columnHeadings = aList[:numcols]
if isinstance(aList[0], StringTypes):
for i in xrange(numcols):
self.InsertColumn(i, aList[i], width=wx.LIST_AUTOSIZE)
elif isinstance(aList[0], ListType) or isinstance(aList[0], TupleType):
w = len(aList[0])
if w == 2 and isinstance(aList[0][0], StringTypes) and isinstance(aList[0][1], IntType):
flag = 0
for i in xrange(numcols):
if aList[i][1] != wx.LIST_AUTOSIZE:
flag = 1
self.InsertColumn(i, aList[i][0], width=aList[i][1])
if flag:
self._autoresize = 0
elif w == 3 and \
isinstance(aList[0][0], StringTypes) and \
isinstance(aList[0][1], IntType) and \
isinstance(aList[0][2], IntType):
flag = 0
for i in xrange(numcols):
if aList[i][1] != wx.LIST_AUTOSIZE:
flag = 1
self.InsertColumn(i, aList[i][0], format=aList[i][2], width=aList[i][1])
if flag:
self._autoresize = 0
elif w == 1 and isinstance(aList[0][0], StringTypes):
for i in xrange(numcols):
self.InsertColumn(i, aList[i][0], width=wx.LIST_AUTOSIZE)
self._autoresize = 1
else:
raise Exception( 'invalid MultiColumnList.SetHeading value: %s' %
aList )
else:
raise Exception( 'invalid MultiColumnList.SetHeading value: %s' %
aList )
if numcols == 1:
self.SetColumnWidth(0, self.GetBestVirtualSize()[0])
def GetItemDataMap(self, aDict):
return self.itemDataMap
def SetItemDataMap(self, aDict):
self.itemDataMap = aDict
def SetSelection(self, itemidx, select=1):
numitems = self.GetItemCount()
if numitems == 0:
return
if itemidx < 0:
itemidx = numitems + itemidx
if itemidx < 0:
itemidx = 0
elif itemidx >= numitems:
itemidx = numitems - 1
if select:
self.SetItemState(itemidx, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
else:
self.SetItemState(itemidx, 0, wx.LIST_STATE_SELECTED)
def SetStringSelection(self, item, select=1):
numitems = self.GetItemCount()
if numitems == 0:
return -1
#TODO: Expand search to all columns, for now it adds no functionality
itemidx = self.FindItem(-1, item, 1)
if itemidx < 0:
return itemidx
if select:
self.SetItemState(itemidx, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
else:
self.SetItemState(itemidx, 0, wx.LIST_STATE_SELECTED)
return itemidx
def _getRules(self):
return self._rules
def _setRules(self, aString):
raise AttributeError, "rules attribute is read-only"
def _getMaxColumns(self):
return self._maxColumns
def _setMaxColumns(self, aString):
# Could perhaps call the mixin __init__ method again, doesn't look
# like it would cause harm. For now however leave this a restriction.
raise AttributeError, "maxColumns attribute is read-only"
def _getItems( self ) :
numitems = self.GetItemCount()
numcols = self.GetColumnCount()
items = [None] * numitems
if numcols == 1:
GetItemText = self.GetItemText
for i in xrange(numitems ) :
items[i] = GetItemText(i)
else:
GetItem = self.GetItem
cols = range(numcols)
for i in xrange(numitems) :
items[i] = map(lambda x: GetItem(i, x).GetText(), cols)
return items
def _setItems( self, aList ) :
if isinstance(aList, NoneType):
aList = []
elif not isinstance(aList, ListType) and not isinstance(aList, TupleType):
raise AttributeError, "unsupported type, list expected"
numitems = len(aList)
if numitems == 0:
self.DeleteAllItems()
self.itemDataMap = {}
return
# If just simple list of strings convert it to a single column list
if isinstance(aList[0], StringTypes):
aList = map(lambda x:[x], aList)
elif isinstance(aList[0], ListType) or isinstance(aList[0], TupleType):
pass
else:
raise AttributeError, "unsupported element type"
# Here we have a list of a list of values.
# If the number of values is greater than the maximum number
# of columns allowed, truncate it. Similarly remove or add
# columns as necessary to accomodate the data up to the maximum
# allowed. It could be thought to just throw an exception
# since the programmer flubbed it however I chose the
# 'do what you can' approach. Note that we depend on the
# first item in the list setting the number of columns for
# all remaining items in the list.
numcols = len(aList[0])
if numcols > self._maxColumns:
numcols = self._maxColumns
if numcols != self.GetColumnCount():
if numcols == 1:
self.ClearAll()
self.InsertColumn(0,'List')
self._columnHeadings = ['List']
else:
c = self.GetColumnCount()
if c > numcols:
for i in range(c-1,numcols-1,-1):
self.DeleteColumn(i)
self._columnHeadings = self._columnHeadings[:-1]
else:
for i in range(c,numcols):
colname = 'Col %d' % (i+1,)
self.InsertColumn(i,colname)
self._columnHeadings.append(colname)
self.DeleteAllItems()
datamap = {}
max = [0] * numcols
blanks = [''] * numcols
columnlist = range(1,numcols)
for i in xrange(numitems):
aItem = aList[i]
if len(aItem) < numcols:
# Not the same number of columns in entry.
# truncation is automatic, padding with
# blanks is done here.
aItem = aItem + blanks
l = len(aItem[0])
if l > max[0]:
max[0] = l
self.InsertStringItem(i, aItem[0])
for j in columnlist:
l = len(aItem[j])
if l > max[j]:
max[j] = l
self.SetStringItem(i, j, aItem[j])
self.SetItemData(i, i)
datamap[i] = aItem
if self._autoresize:
charwidth = self.GetCharWidth()
maxwidth = self.GetBestVirtualSize()[0]*2
for i in range(numcols):
hdrwidth = (len(self._columnHeadings[i])+1) * charwidth
colwidth = int((max[i]+1) * charwidth)
if colwidth < hdrwidth:
colwidth = hdrwidth
if colwidth < 20:
colwidth = 20
elif colwidth > maxwidth:
colwidth = maxwidth
self.SetColumnWidth(i, colwidth)
self.resizeLastColumn(self.GetColumnWidth(numcols-1))
self.itemDataMap = datamap
# KEA 2004-04-16
# theoretically this is fixed, need to check then delete this commented block
"""
def _getFont(self):
if self._font is None:
# workaround for GetFont() bug with ListCtrl with
# wxPython 2.4.1.2 and earlier on wxMSW
f = self.GetFont()
if f.Ok():
desc = font.fontDescription(f)
else:
desc = font.fontDescription(self._parent.GetFont())
self._font = font.Font(desc)
return self._font
"""
items = property(_getItems, _setItems)
columnHeadings = property(_getColumnHeadings, _setColumnHeadings)
maxColumns = property(_getMaxColumns, _setMaxColumns)
rules = property(_getRules, _setRules)
import sys
from PythonCard import registry
registry.Registry.getInstance().register(sys.modules[__name__].MultiColumnList)
|