]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/mixins/listctrl.py
e31a871cbcfa89434f2476d59bfe5dedf17d7228
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 coilumn 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.
38 def __init__(self
, numColumns
):
39 self
._colSortFlag
= [0] * numColumns
41 self
._colSortFlag
[self
._col
] = 1
43 list = self
.GetListCtrl()
45 raise ValueError, "No wxListCtrl available"
46 EVT_LIST_COL_CLICK(list, list.GetId(), self
.OnColClick
)
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."""
53 self
._colSortFlag
[col
] = ascending
54 self
.GetListCtrl().SortItems(self
.ColumnSorter
)
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
)
63 def ColumnSorter(self
, key1
, key2
):
65 sortFlag
= self
._colSortFlag
[col
]
66 item1
= self
.itemDataMap
[key1
][col
]
67 item2
= self
.itemDataMap
[key2
][col
]
68 if item1
== item2
: return 0
70 if item1
< item2
: return -1
73 if item1
> item2
: return -1
77 def GetColumnWidths(self
):
78 list = self
.GetListCtrl()
80 for x
in range(len(self
._colSortFlag
)):
81 rv
.append(list.GetColumnWidth(x
))
84 #----------------------------------------------------------------------------