]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/mixins/listctrl.py
db3c5d658ac80339aeb677c9396b6aa070914e90
[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 self._needResize = false
153 self._lastColMinWidth = None
154
155 EVT_SIZE(self, self._onResize)
156 EVT_LIST_COL_END_DRAG(self, self.GetId(), self._onEndColDrag)
157 EVT_IDLE(self, self._onIdle)
158
159
160 def resizeLastColumn(self, minWidth):
161 """ Resize the last column appropriately.
162
163 If the list's columns are too wide to fit within the window, we use
164 a horizontal scrollbar. Otherwise, we expand the right-most column
165 to take up the remaining free space in the list.
166
167 This method is called automatically when the wxListCtrl is resized;
168 you can also call it yourself whenever you want the last column to
169 be resized appropriately (eg, when adding, removing or resizing
170 columns).
171
172 'minWidth' is the preferred minimum width for the last column.
173 """
174 self._lastColMinWidth = minWidth
175 self._doResize()
176
177 # =====================
178 # == Private Methods ==
179 # =====================
180
181 def _onResize(self, event):
182 """ Respond to the wxListCtrl being resized.
183
184 We automatically resize the last column in the list.
185 """
186 self._doResize()
187
188
189 def _onEndColDrag(self, event):
190 """ Respond to the user resizing one of our columns.
191
192 We resize the last column in the list to match. Note that, because
193 of a quirk in the way columns are resized under MS Windows, we
194 actually have to do the column resize in idle time.
195 """
196 self._needResize = true
197
198
199 def _onIdle(self, event):
200 """ Respond to an idle event.
201
202 We resize the last column, if we've been asked to do so.
203 """
204 if self._needResize:
205 self._doResize()
206 self.Refresh() # Fixes redraw problem under MS Windows.
207 self._needResize = false
208
209
210 def _doResize(self):
211 """ Resize the last column as appropriate.
212
213 If the list's columns are too wide to fit within the window, we use
214 a horizontal scrollbar. Otherwise, we expand the right-most column
215 to take up the remaining free space in the list.
216
217 We remember the current size of the last column, before resizing,
218 as the preferred minimum width if we haven't previously been given
219 or calculated a minimum width. This ensure that repeated calls to
220 _doResize() don't cause the last column to size itself too large.
221 """
222 numCols = self.GetColumnCount()
223 if numCols == 0: return # Nothing to resize.
224
225 if self._lastColMinWidth == None:
226 self._lastColMinWidth = self.GetColumnWidth(numCols - 1)
227
228 listWidth = self.GetSize().width
229 if self.GetItemCount() > self.GetCountPerPage():
230 # We're showing the vertical scrollbar -> allow for scrollbar width
231 scrollWidth = wxSystemSettings_GetSystemMetric(wxSYS_VSCROLL_X)
232 listWidth = listWidth - scrollWidth
233
234 totColWidth = 0 # Width of all columns except last one.
235 for col in range(numCols-1):
236 totColWidth = totColWidth + self.GetColumnWidth(col)
237
238 lastColWidth = self.GetColumnWidth(numCols - 1)
239
240 margin = 6 # NOTE: This is the extra number of pixels required to make
241 # the wxListCtrl size correctly, at least under
242 # Windows 2000. Unfortunately, different OSs and
243 # even different versions of the same OS may implement
244 # the wxListCtrl differently, so different margins may
245 # be needed to get the columns resized correctly. No
246 # doubt the margin could be calculated in a more
247 # intelligent manner...
248
249 if totColWidth + self._lastColMinWidth > listWidth - margin:
250 # We haven't got the width to show the last column at its minimum
251 # width -> set it to its minimum width and allow the horizontal
252 # scrollbar to show.
253 self.SetColumnWidth(numCols-1, self._lastColMinWidth)
254 return
255
256 # Resize the last column to take up the remaining available space.
257
258 self.SetColumnWidth(numCols-1, listWidth - totColWidth - margin)
259
260
261
262 #----------------------------------------------------------------------------