]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/mixins/listctrl.py
dd57fc851e717c548e549a2da8fb3549fa917c7b
[wxWidgets.git] / wxPython / wxPython / lib / mixins / listctrl.py
1 #----------------------------------------------------------------------------
2 # Name: wxPython.lib.mixins.listctrl
3 # Purpose: Helpful mix-in classes for wxListCtrl
4 #
5 # Author: Robin Dunn
6 #
7 # Created: 15-May-2001
8 # RCS-ID: $Id$
9 # Copyright: (c) 2001 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12
13 from wxPython.wx import *
14
15 #----------------------------------------------------------------------------
16
17 class wxColumnSorterMixin:
18 """
19 A mixin class that handles sorting of a wxListCtrl in REPORT mode when
20 the column header is clicked on.
21
22 There are a few requirments needed in order for this to work genericly:
23
24 1. The combined class must have a GetListCtrl method that
25 returns the wxListCtrl to be sorted, and the list control
26 must exist at the time the wxColumnSorterMixin.__init__
27 method is called because it uses GetListCtrl.
28
29 2. Items in the list control must have a unique data value set
30 with list.SetItemData.
31
32 3. The combined class must have an attribute named itemDataMap
33 that is a dictionary mapping the data values to a sequence of
34 objects representing the values in each column. These values
35 are compared in the column sorter to determine sort order.
36
37 Interesting methods to override are GetColumnSorter,
38 GetSecondarySortValues, and GetSortImages. See below for details.
39 """
40
41 def __init__(self, numColumns):
42 self._colSortFlag = [0] * numColumns
43 self._col = -1
44
45 list = self.GetListCtrl()
46 if not list:
47 raise ValueError, "No wxListCtrl available"
48 EVT_LIST_COL_CLICK(list, list.GetId(), self.__OnColClick)
49
50
51 def SortListItems(self, col=-1, ascending=1):
52 """Sort the list on demand. Can also be used to set the sort column and order."""
53 oldCol = self._col
54 if col != -1:
55 self._col = col
56 self._colSortFlag[col] = ascending
57 self.GetListCtrl().SortItems(self.GetColumnSorter())
58 self.__updateImages(oldCol)
59
60
61 def GetColumnWidths(self):
62 """
63 Returns a list of column widths. Can be used to help restore the current
64 view later.
65 """
66 list = self.GetListCtrl()
67 rv = []
68 for x in range(len(self._colSortFlag)):
69 rv.append(list.GetColumnWidth(x))
70 return rv
71
72
73 def GetSortImages(self):
74 """
75 Returns a tuple of image list indexesthe indexes in the image list for an image to be put on the column
76 header when sorting in descending order.
77 """
78 return (-1, -1) # (decending, ascending) image IDs
79
80
81 def GetColumnSorter(self):
82 """Returns a callable object to be used for comparing column values when sorting."""
83 return self.__ColumnSorter
84
85
86 def GetSecondarySortValues(self, col, key1, key2):
87 """Returns a tuple of 2 values to use for secondary sort values when the
88 items in the selected column match equal. The default just returns the
89 item data values."""
90 return (key1, key2)
91
92
93 def __OnColClick(self, evt):
94 oldCol = self._col
95 self._col = col = evt.GetColumn()
96 self._colSortFlag[col] = not self._colSortFlag[col]
97 self.GetListCtrl().SortItems(self.GetColumnSorter())
98 self.__updateImages(oldCol)
99 evt.Skip()
100
101
102 def __ColumnSorter(self, key1, key2):
103 col = self._col
104 ascending = self._colSortFlag[col]
105 item1 = self.itemDataMap[key1][col]
106 item2 = self.itemDataMap[key2][col]
107
108 cmpVal = cmp(item1, item2)
109
110 # If the items are equal then pick something else to make the sort value unique
111 if cmpVal == 0:
112 cmpVal = apply(cmp, self.GetSecondarySortValues(col, key1, key2))
113
114 if ascending:
115 return cmpVal
116 else:
117 return -cmpVal
118
119
120 def __updateImages(self, oldCol):
121 sortImages = self.GetSortImages()
122 if self._col != -1 and sortImages[0] != -1:
123 img = sortImages[self._colSortFlag[self._col]]
124 list = self.GetListCtrl()
125 if oldCol != -1:
126 list.ClearColumnImage(oldCol)
127 list.SetColumnImage(self._col, img)
128
129
130 #----------------------------------------------------------------------------
131 #----------------------------------------------------------------------------
132
133 class wxListCtrlAutoWidthMixin:
134 """ A mix-in class that automatically resizes the last column to take up
135 the remaining width of the wxListCtrl.
136
137 This causes the wxListCtrl to automatically take up the full width of
138 the list, without either a horizontal scroll bar (unless absolutely
139 necessary) or empty space to the right of the last column.
140
141 NOTE: This only works for report-style lists.
142
143 WARNING: If you override the EVT_SIZE event in your wxListCtrl, make
144 sure you call event.Skip() to ensure that the mixin's
145 _OnResize method is called.
146
147 This mix-in class was written by Erik Westra <ewestra@wave.co.nz>
148 """
149 def __init__(self):
150 """ Standard initialiser.
151 """
152 EVT_SIZE(self, self._onResize)
153 self._lastColMinWidth = None
154
155
156 def resizeLastColumn(self, minWidth):
157 """ Resize the last column appropriately.
158
159 If the list's columns are too wide to fit within the window, we use
160 a horizontal scrollbar. Otherwise, we expand the right-most column
161 to take up the remaining free space in the list.
162
163 This method is called automatically when the wxListCtrl is resized;
164 you can also call it yourself whenever you want the last column to
165 be resized appropriately (eg, when adding, removing or resizing
166 columns).
167
168 'minWidth' is the preferred minimum width for the last column.
169 """
170 self.lastColMinWidth = minWidth
171 self._doResize()
172
173 # =====================
174 # == Private Methods ==
175 # =====================
176
177 def _onResize(self, event):
178 """ Respond to the wxListCtrl being resized.
179
180 We automatically resize the last column in the list.
181 """
182 self._doResize()
183
184
185 def _doResize(self):
186 """ Resize the last column as appropriate.
187
188 If the list's columns are too wide to fit within the window, we use
189 a horizontal scrollbar. Otherwise, we expand the right-most column
190 to take up the remaining free space in the list.
191
192 We remember the current size of the last column, before resizing,
193 as the preferred minimum width if we haven't previously been given
194 or calculated a minimum width. This ensure that repeated calls to
195 _doResize() don't cause the last column to size itself too large.
196 """
197 numCols = self.GetColumnCount()
198 if numCols == 0: return # Nothing to resize.
199
200 if self._lastColMinWidth == None:
201 self._lastColMinWidth = self.GetColumnWidth(numCols - 1)
202
203 listWidth = self.GetSize().width
204 if self.GetItemCount() > self.GetCountPerPage():
205 # We're showing the vertical scrollbar -> allow for scrollbar width
206 scrollWidth = wxSystemSettings_GetSystemMetric(wxSYS_VSCROLL_X)
207 listWidth = listWidth - scrollWidth
208
209 totColWidth = 0 # Width of all columns except last one.
210 for col in range(numCols-1):
211 totColWidth = totColWidth + self.GetColumnWidth(col)
212
213 lastColWidth = self.GetColumnWidth(numCols - 1)
214
215 margin = 6 # NOTE: This is the extra number of pixels required to make
216 # the wxListCtrl size correctly, at least under
217 # Windows 2000. Unfortunately, different OSs and
218 # even different versions of the same OS may implement
219 # the wxListCtrl differently, so different margins may
220 # be needed to get the columns resized correctly. No
221 # doubt the margin could be calculated in a more
222 # intelligent manner...
223
224 if totColWidth + self._lastColMinWidth > listWidth - margin:
225 # We haven't got the width to show the last column at its minimum
226 # width -> set it to its minimum width and allow the horizontal
227 # scrollbar to show.
228 self.SetColumnWidth(numCols-1, self._lastColMinWidth)
229 return
230
231 # Resize the last column to take up the remaining available space.
232
233 self.SetColumnWidth(numCols-1, listWidth - totColWidth - margin)
234
235
236
237 #----------------------------------------------------------------------------