]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/mixins/listctrl.py
e31a871cbcfa89434f2476d59bfe5dedf17d7228
[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 coilumn 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 """
38 def __init__(self, numColumns):
39 self._colSortFlag = [0] * numColumns
40 self._col = 0
41 self._colSortFlag[self._col] = 1
42
43 list = self.GetListCtrl()
44 if not list:
45 raise ValueError, "No wxListCtrl available"
46 EVT_LIST_COL_CLICK(list, list.GetId(), self.OnColClick)
47
48
49 def SortListItems(self, col=-1, ascending=1):
50 """Sort the list on demand. Can also be used to set the sort column and order."""
51 if col != -1:
52 self._col = col
53 self._colSortFlag[col] = ascending
54 self.GetListCtrl().SortItems(self.ColumnSorter)
55
56
57 def OnColClick(self, evt):
58 self._col = col = evt.m_col
59 self._colSortFlag[col] = not self._colSortFlag[col]
60 self.GetListCtrl().SortItems(self.ColumnSorter)
61
62
63 def ColumnSorter(self, key1, key2):
64 col = self._col
65 sortFlag = self._colSortFlag[col]
66 item1 = self.itemDataMap[key1][col]
67 item2 = self.itemDataMap[key2][col]
68 if item1 == item2: return 0
69 if sortFlag:
70 if item1 < item2: return -1
71 else: return 1
72 else:
73 if item1 > item2: return -1
74 else: return 1
75
76
77 def GetColumnWidths(self):
78 list = self.GetListCtrl()
79 rv = []
80 for x in range(len(self._colSortFlag)):
81 rv.append(list.GetColumnWidth(x))
82 return rv
83
84 #----------------------------------------------------------------------------
85
86
87
88
89
90