| 1 | # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
| 2 | # |
| 3 | # o Updated for wx namespace |
| 4 | # |
| 5 | # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
| 6 | # |
| 7 | # o wx.ListItem.GetText() returns a wxString pointer, not the text. |
| 8 | # |
| 9 | |
| 10 | import wx |
| 11 | import images |
| 12 | |
| 13 | #---------------------------------------------------------------------- |
| 14 | |
| 15 | class TestVirtualList(wx.ListCtrl): |
| 16 | def __init__(self, parent, log): |
| 17 | wx.ListCtrl.__init__( |
| 18 | self, parent, -1, |
| 19 | style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES |
| 20 | ) |
| 21 | |
| 22 | self.log = log |
| 23 | |
| 24 | self.il = wx.ImageList(16, 16) |
| 25 | self.idx1 = self.il.Add(images.getSmilesBitmap()) |
| 26 | self.SetImageList(self.il, wx.IMAGE_LIST_SMALL) |
| 27 | |
| 28 | |
| 29 | self.InsertColumn(0, "First") |
| 30 | self.InsertColumn(1, "Second") |
| 31 | self.InsertColumn(2, "Third") |
| 32 | self.SetColumnWidth(0, 175) |
| 33 | self.SetColumnWidth(1, 175) |
| 34 | self.SetColumnWidth(2, 175) |
| 35 | |
| 36 | self.SetItemCount(1000000) |
| 37 | |
| 38 | self.attr1 = wx.ListItemAttr() |
| 39 | self.attr1.SetBackgroundColour("yellow") |
| 40 | |
| 41 | self.attr2 = wx.ListItemAttr() |
| 42 | self.attr2.SetBackgroundColour("light blue") |
| 43 | |
| 44 | self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected) |
| 45 | self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated) |
| 46 | self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected) |
| 47 | |
| 48 | |
| 49 | def OnItemSelected(self, event): |
| 50 | self.currentItem = event.m_itemIndex |
| 51 | self.log.WriteText('OnItemSelected: "%s", "%s", "%s", "%s"\n' % |
| 52 | (self.currentItem, |
| 53 | self.GetItemText(self.currentItem), |
| 54 | self.getColumnText(self.currentItem, 1), |
| 55 | self.getColumnText(self.currentItem, 2))) |
| 56 | |
| 57 | def OnItemActivated(self, event): |
| 58 | self.currentItem = event.m_itemIndex |
| 59 | self.log.WriteText("OnItemActivated: %s\nTopItem: %s\n" % |
| 60 | (self.GetItemText(self.currentItem), self.GetTopItem())) |
| 61 | |
| 62 | def getColumnText(self, index, col): |
| 63 | item = self.GetItem(index, col) |
| 64 | return item.GetText() |
| 65 | |
| 66 | def OnItemDeselected(self, evt): |
| 67 | self.log.WriteText("OnItemDeselected: %s" % evt.m_itemIndex) |
| 68 | |
| 69 | |
| 70 | #--------------------------------------------------- |
| 71 | # These methods are callbacks for implementing the |
| 72 | # "virtualness" of the list... Normally you would |
| 73 | # determine the text, attributes and/or image based |
| 74 | # on values from some external data source, but for |
| 75 | # this demo we'll just calculate them |
| 76 | def OnGetItemText(self, item, col): |
| 77 | return "Item %d, column %d" % (item, col) |
| 78 | |
| 79 | def OnGetItemImage(self, item): |
| 80 | if item % 3 == 0: |
| 81 | return self.idx1 |
| 82 | else: |
| 83 | return -1 |
| 84 | |
| 85 | def OnGetItemAttr(self, item): |
| 86 | if item % 3 == 1: |
| 87 | return self.attr1 |
| 88 | elif item % 3 == 2: |
| 89 | return self.attr2 |
| 90 | else: |
| 91 | return None |
| 92 | |
| 93 | |
| 94 | #---------------------------------------------------------------------- |
| 95 | |
| 96 | def runTest(frame, nb, log): |
| 97 | win = TestVirtualList(nb, log) |
| 98 | return win |
| 99 | |
| 100 | #---------------------------------------------------------------------- |
| 101 | |
| 102 | |
| 103 | overview = """\ |
| 104 | This example demonstrates the ListCtrl's Virtual List features. A Virtual list |
| 105 | can contain any number of cells, but data is not loaded into the control itself. |
| 106 | It is loaded on demand via virtual methods <code>OnGetItemText(), OnGetItemImage()</code>, |
| 107 | and <code>OnGetItemAttr()</code>. This greatly reduces the amount of memory required |
| 108 | without limiting what can be done with the list control itself. |
| 109 | """ |
| 110 | |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | import sys,os |
| 115 | import run |
| 116 | run.main(['', os.path.basename(sys.argv[0])]) |
| 117 | |