1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """This module contains custom wxListCtrl subclasses.
16 AutoWidthListCtrl - A wxListCtrl object that automatically adjusts the width of
17 its columns.
18 ColumnSortListCtrl - An AutoWidthListCtrl that sorts its entries when the column
19 header is clicked.
20 KeyEventsListCtrl - A ColumnSortListCtrl that selects and item as you type its
21 name.
22 """
23
24 __id__ = "$Id: listctrls.py 2980 2009-04-02 00:14:33Z juhas $"
25
26 import wx
27 import wx.lib.mixins.listctrl as listmix
28
30 """wxListCtrl subclass that automatically adjusts its column width."""
31 - def __init__(self, parent, ID, pos=wx.DefaultPosition,
32 size=wx.DefaultSize, style=0, *args, **kwargs):
33 wx.ListCtrl.__init__(self, parent, ID, pos, size, style, *args, **kwargs)
34 listmix.ListCtrlAutoWidthMixin.__init__(self)
35
37 """Clear all selections in the list."""
38 for item in range( self.GetItemCount() ):
39 self.Select(item, on=0)
40 return
41
43 """Convenience function for simple selection of a list item by label.
44
45 itemtext -- The label of the item to select. If itemtext is None
46 (default) then all items will be deselected.
47 """
48
49 self.clearSelections()
50
51
52 item = 0
53 if itemtext:
54 item = self.FindItem(-1, itemtext)
55 self.Select(item)
56 self.Focus(item)
57 return item
58
59
61 """AutoWidthListCtrl subclass that sorts its columns when the column header
62 is pressed.
63
64 This ListCtrl requires an itemDataMap member dictionary to be initialized
65 before the sorting capabilites can be realized. This dictionary simply references
66 the ListCtrl's entries by a unique number. This number must be stored as the
67 ItemData (with SetItemData) of the entry. The member data must be in the
68 form of a tuple, where the tuple has a number of entries as the ListCtrl has
69 columns. The sorting routine sorts the items in the ListCtrl by the entries
70 in this tuple.
71 """
72 - def __init__(self, parent, ID, pos=wx.DefaultPosition,
73 size=wx.DefaultSize, style=0, *args, **kwargs):
77
79 """This method is required by the sorter mixin."""
80 return self
81
83 """Initialize the column sorter mixin after the ListCtrl is filled.
84
85 This method must be called whenever the itemDataMap is altered.
86 """
87 numcol = self.GetColumnCount()
88 listmix.ColumnSorterMixin.__init__(self, numcol)
89 return
90
92 """This method automatically sets up the itemDataMap.
93
94 The itemDataMap gets filled with the current ListCtrl entries. The
95 itemDataMap does not update automatically when the list is changed. To
96 update the itemDataMap this method must be called again.
97 initializeSorter should be called after a call to this method.
98 """
99 numcol = self.GetColumnCount()
100 numrow = self.GetItemCount()
101 self.itemDataMap = {}
102 for i in range(numrow):
103 infolist = []
104 for j in range(numcol):
105 infolist.append( self.GetItem(i,j).GetText() )
106 self.itemDataMap[i+1] = tuple(infolist)
107 self.SetItemData(i, i+1)
108 return
109
110
111
113 """ColumnSortListCtrl that catches key events and selects the item that
114 matches.
115
116 It only searches for items in the first column.
117 """
118 - def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize,
119 style=0, *args, **kwargs):
120 ColumnSortListCtrl.__init__(self, parent, id, pos, size, style, *args,
121 **kwargs)
122 self.typedText = ''
123 self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
124
126 if prefix:
127 prefix = prefix.lower()
128 length = len(prefix)
129
130 for x in range(self.GetItemCount()):
131 text = self.GetItemText(x)
132 text = text.lower()
133
134 if text[:length] == prefix:
135 return x
136
137 return -1
138
139
141 key = evt.GetKeyCode()
142
143
144 if evt.ControlDown() and key == 65:
145 for item in range(self.GetItemCount()):
146 self.Select(item)
147 return
148
149
150 if key >= 32 and key <= 127:
151 self.typedText = self.typedText + chr(key)
152 item = self.findPrefix(self.typedText)
153
154 if item != -1:
155 itemtext = self.GetItemText(item)
156 self.setSelection(itemtext)
157
158 elif key == wx.WXK_BACK:
159 self.typedText = self.typedText[:-1]
160
161 if not self.typedText:
162 itemtext = self.GetItemText(0)
163 self.setSelection(itemtext)
164 else:
165 item = self.findPrefix(self.typedText)
166
167 if item != -1:
168 itemtext = self.GetItemText(item)
169 self.setSelection(itemtext)
170
171 else:
172 self.typedText = ''
173 evt.Skip()
174
177
178
179