]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/mixins/listctrl.py
dd57fc851e717c548e549a2da8fb3549fa917c7b
1 #----------------------------------------------------------------------------
2 # Name: wxPython.lib.mixins.listctrl
3 # Purpose: Helpful mix-in classes for wxListCtrl
9 # Copyright: (c) 2001 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 from wxPython
.wx
import *
15 #----------------------------------------------------------------------------
17 class wxColumnSorterMixin
:
19 A mixin class that handles sorting of a wxListCtrl in REPORT mode when
20 the column header is clicked on.
22 There are a few requirments needed in order for this to work genericly:
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.
29 2. Items in the list control must have a unique data value set
30 with list.SetItemData.
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.
37 Interesting methods to override are GetColumnSorter,
38 GetSecondarySortValues, and GetSortImages. See below for details.
41 def __init__(self
, numColumns
):
42 self
._colSortFlag
= [0] * numColumns
45 list = self
.GetListCtrl()
47 raise ValueError, "No wxListCtrl available"
48 EVT_LIST_COL_CLICK(list, list.GetId(), self
.__OnColClick
)
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."""
56 self
._colSortFlag
[col
] = ascending
57 self
.GetListCtrl().SortItems(self
.GetColumnSorter())
58 self
.__updateImages
(oldCol
)
61 def GetColumnWidths(self
):
63 Returns a list of column widths. Can be used to help restore the current
66 list = self
.GetListCtrl()
68 for x
in range(len(self
._colSortFlag
)):
69 rv
.append(list.GetColumnWidth(x
))
73 def GetSortImages(self
):
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.
78 return (-1, -1) # (decending, ascending) image IDs
81 def GetColumnSorter(self
):
82 """Returns a callable object to be used for comparing column values when sorting."""
83 return self
.__ColumnSorter
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
93 def __OnColClick(self
, evt
):
95 self
._col
= col
= evt
.GetColumn()
96 self
._colSortFlag
[col
] = not self
._colSortFlag
[col
]
97 self
.GetListCtrl().SortItems(self
.GetColumnSorter())
98 self
.__updateImages
(oldCol
)
102 def __ColumnSorter(self
, key1
, key2
):
104 ascending
= self
._colSortFlag
[col
]
105 item1
= self
.itemDataMap
[key1
][col
]
106 item2
= self
.itemDataMap
[key2
][col
]
108 cmpVal
= cmp(item1
, item2
)
110 # If the items are equal then pick something else to make the sort value unique
112 cmpVal
= apply(cmp, self
.GetSecondarySortValues(col
, key1
, key2
))
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()
126 list.ClearColumnImage(oldCol
)
127 list.SetColumnImage(self
._col
, img
)
130 #----------------------------------------------------------------------------
131 #----------------------------------------------------------------------------
133 class wxListCtrlAutoWidthMixin
:
134 """ A mix-in class that automatically resizes the last column to take up
135 the remaining width of the wxListCtrl.
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.
141 NOTE: This only works for report-style lists.
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.
147 This mix-in class was written by Erik Westra <ewestra@wave.co.nz>
150 """ Standard initialiser.
152 EVT_SIZE(self
, self
._onResize
)
153 self
._lastColMinWidth
= None
156 def resizeLastColumn(self
, minWidth
):
157 """ Resize the last column appropriately.
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.
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
168 'minWidth' is the preferred minimum width for the last column.
170 self
.lastColMinWidth
= minWidth
173 # =====================
174 # == Private Methods ==
175 # =====================
177 def _onResize(self
, event
):
178 """ Respond to the wxListCtrl being resized.
180 We automatically resize the last column in the list.
186 """ Resize the last column as appropriate.
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.
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.
197 numCols
= self
.GetColumnCount()
198 if numCols
== 0: return # Nothing to resize.
200 if self
._lastColMinWidth
== None:
201 self
._lastColMinWidth
= self
.GetColumnWidth(numCols
- 1)
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
209 totColWidth
= 0 # Width of all columns except last one.
210 for col
in range(numCols
-1):
211 totColWidth
= totColWidth
+ self
.GetColumnWidth(col
)
213 lastColWidth
= self
.GetColumnWidth(numCols
- 1)
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...
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
228 self
.SetColumnWidth(numCols
-1, self
._lastColMinWidth
)
231 # Resize the last column to take up the remaining available space.
233 self
.SetColumnWidth(numCols
-1, listWidth
- totColWidth
- margin
)
237 #----------------------------------------------------------------------------