]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ListCtrl_virtual.py
The great wxVScrolledWindow refactoring: allow using it both horizontal and
[wxWidgets.git] / wxPython / demo / ListCtrl_virtual.py
1
2 import wx
3 import images
4
5 #----------------------------------------------------------------------
6
7 class TestVirtualList(wx.ListCtrl):
8 def __init__(self, parent, log):
9 wx.ListCtrl.__init__(
10 self, parent, -1,
11 style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES
12 )
13
14 self.log = log
15
16 self.il = wx.ImageList(16, 16)
17 self.idx1 = self.il.Add(images.getSmilesBitmap())
18 self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
19
20
21 self.InsertColumn(0, "First")
22 self.InsertColumn(1, "Second")
23 self.InsertColumn(2, "Third")
24 self.SetColumnWidth(0, 175)
25 self.SetColumnWidth(1, 175)
26 self.SetColumnWidth(2, 175)
27
28 self.SetItemCount(1000000)
29
30 self.attr1 = wx.ListItemAttr()
31 self.attr1.SetBackgroundColour("yellow")
32
33 self.attr2 = wx.ListItemAttr()
34 self.attr2.SetBackgroundColour("light blue")
35
36 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
37 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
38 self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
39
40
41 def OnItemSelected(self, event):
42 self.currentItem = event.m_itemIndex
43 self.log.WriteText('OnItemSelected: "%s", "%s", "%s", "%s"\n' %
44 (self.currentItem,
45 self.GetItemText(self.currentItem),
46 self.getColumnText(self.currentItem, 1),
47 self.getColumnText(self.currentItem, 2)))
48
49 def OnItemActivated(self, event):
50 self.currentItem = event.m_itemIndex
51 self.log.WriteText("OnItemActivated: %s\nTopItem: %s\n" %
52 (self.GetItemText(self.currentItem), self.GetTopItem()))
53
54 def getColumnText(self, index, col):
55 item = self.GetItem(index, col)
56 return item.GetText()
57
58 def OnItemDeselected(self, evt):
59 self.log.WriteText("OnItemDeselected: %s" % evt.m_itemIndex)
60
61
62 #---------------------------------------------------
63 # These methods are callbacks for implementing the
64 # "virtualness" of the list... Normally you would
65 # determine the text, attributes and/or image based
66 # on values from some external data source, but for
67 # this demo we'll just calculate them
68 def OnGetItemText(self, item, col):
69 return "Item %d, column %d" % (item, col)
70
71 def OnGetItemImage(self, item):
72 if item % 3 == 0:
73 return self.idx1
74 else:
75 return -1
76
77 def OnGetItemAttr(self, item):
78 if item % 3 == 1:
79 return self.attr1
80 elif item % 3 == 2:
81 return self.attr2
82 else:
83 return None
84
85
86 class TestVirtualListPanel(wx.Panel):
87 def __init__(self, parent, log):
88 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
89
90 self.log = log
91 sizer = wx.BoxSizer(wx.VERTICAL)
92
93 if wx.Platform == "__WXMAC__" and \
94 hasattr(wx.GetApp().GetTopWindow(), "LoadDemo"):
95 self.useNative = wx.CheckBox(self, -1, "Use native listctrl")
96 self.useNative.SetValue(
97 not wx.SystemOptions.GetOptionInt("mac.listctrl.always_use_generic") )
98 self.Bind(wx.EVT_CHECKBOX, self.OnUseNative, self.useNative)
99 sizer.Add(self.useNative, 0, wx.ALL | wx.ALIGN_RIGHT, 4)
100
101 self.list = TestVirtualList(self, self.log)
102 sizer.Add(self.list, 1, wx.EXPAND)
103
104 self.SetSizer(sizer)
105 self.SetAutoLayout(True)
106
107 def OnUseNative(self, event):
108 wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", not event.IsChecked())
109 wx.GetApp().GetTopWindow().LoadDemo("ListCtrl_virtual")
110
111 #----------------------------------------------------------------------
112
113 def runTest(frame, nb, log):
114 win = TestVirtualListPanel(nb, log)
115 return win
116
117 #----------------------------------------------------------------------
118
119
120 overview = """\
121 This example demonstrates the ListCtrl's Virtual List features. A Virtual list
122 can contain any number of cells, but data is not loaded into the control itself.
123 It is loaded on demand via virtual methods <code>OnGetItemText(), OnGetItemImage()</code>,
124 and <code>OnGetItemAttr()</code>. This greatly reduces the amount of memory required
125 without limiting what can be done with the list control itself.
126 """
127
128
129
130 if __name__ == '__main__':
131 import sys,os
132 import run
133 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
134