1 #----------------------------------------------------------------------------
3 # Purpose: Testing lots of stuff, controls, window types, etc.
5 # Author: Robin Dunn & Gary Dumer
9 # Copyright: (c) 1998 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
15 import wx
.lib
.mixins
.listctrl
as listmix
19 #---------------------------------------------------------------------------
22 1 : ("Bad English", "The Price Of Love", "Rock"),
23 2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
24 3 : ("George Michael", "Praying For Time", "Rock"),
25 4 : ("Gloria Estefan", "Here We Are", "Rock"),
26 5 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
27 6 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
28 7 : ("Paul Young", "Oh Girl", "Rock"),
29 8 : ("Paula Abdul", "Opposites Attract", "Rock"),
30 9 : ("Richard Marx", "Should've Known Better", "Rock"),
31 10: ("Rod Stewart", "Forever Young", "Rock"),
32 11: ("Roxette", "Dangerous", "Rock"),
33 12: ("Sheena Easton", "The Lover In Me", "Rock"),
34 13: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
35 14: ("Stevie B.", "Because I Love You", "Rock"),
36 15: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
37 16: ("The Bangles", "Eternal Flame", "Rock"),
38 17: ("Wilson Phillips", "Release Me", "Rock"),
39 18: ("Billy Joel", "Blonde Over Blue", "Rock"),
40 19: ("Billy Joel", "Famous Last Words", "Rock"),
41 20: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
42 21: ("Billy Joel", "The River Of Dreams", "Rock"),
43 22: ("Billy Joel", "Two Thousand Years", "Rock"),
44 23: ("Janet Jackson", "Alright", "Rock"),
45 24: ("Janet Jackson", "Black Cat", "Rock"),
46 25: ("Janet Jackson", "Come Back To Me", "Rock"),
47 26: ("Janet Jackson", "Escapade", "Rock"),
48 27: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
49 28: ("Janet Jackson", "Miss You Much", "Rock"),
50 29: ("Janet Jackson", "Rhythm Nation", "Rock"),
51 30: ("Janet Jackson", "State Of The World", "Rock"),
52 31: ("Janet Jackson", "The Knowledge", "Rock"),
53 32: ("Spyro Gyra", "End of Romanticism", "Jazz"),
54 33: ("Spyro Gyra", "Heliopolis", "Jazz"),
55 34: ("Spyro Gyra", "Jubilee", "Jazz"),
56 35: ("Spyro Gyra", "Little Linda", "Jazz"),
57 36: ("Spyro Gyra", "Morning Dance", "Jazz"),
58 37: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
59 38: ("Yes", "Owner Of A Lonely Heart", "Rock"),
60 39: ("Yes", "Rhythm Of Love", "Rock"),
61 40: ("Cusco", "Dream Catcher", "New Age"),
62 41: ("Cusco", "Geronimos Laughter", "New Age"),
63 42: ("Cusco", "Ghost Dance", "New Age"),
64 43: ("Blue Man Group", "Drumbone", "New Age"),
65 44: ("Blue Man Group", "Endless Column", "New Age"),
66 45: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
67 46: ("Kenny G", "Silhouette", "Jazz"),
68 47: ("Sade", "Smooth Operator", "Jazz"),
69 48: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
70 49: ("David Arkenstone", "Stepping Stars", "New Age"),
71 50: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
72 51: ("David Lanz", "Behind The Waterfall", "New Age"),
73 52: ("David Lanz", "Cristofori's Dream", "New Age"),
74 53: ("David Lanz", "Heartsounds", "New Age"),
75 54: ("David Lanz", "Leaves on the Seine", "New Age"),
78 #---------------------------------------------------------------------------
80 class TestListCtrl(wx
.ListCtrl
, listmix
.ListCtrlAutoWidthMixin
):
81 def __init__(self
, parent
, ID
, pos
=wx
.DefaultPosition
,
82 size
=wx
.DefaultSize
, style
=0):
83 wx
.ListCtrl
.__init
__(self
, parent
, ID
, pos
, size
, style
)
84 listmix
.ListCtrlAutoWidthMixin
.__init
__(self
)
87 class TestListCtrlPanel(wx
.Panel
, listmix
.ColumnSorterMixin
):
88 def __init__(self
, parent
, log
):
89 wx
.Panel
.__init
__(self
, parent
, -1, style
=wx
.WANTS_CHARS
)
94 self
.il
= wx
.ImageList(16, 16)
96 self
.idx1
= self
.il
.Add(images
.getSmilesBitmap())
97 self
.sm_up
= self
.il
.Add(images
.getSmallUpArrowBitmap())
98 self
.sm_dn
= self
.il
.Add(images
.getSmallDnArrowBitmap())
100 self
.list = TestListCtrl(self
, tID
,
105 | wx
.LC_SORT_ASCENDING
111 self
.list.SetImageList(self
.il
, wx
.IMAGE_LIST_SMALL
)
115 # Now that the list exists we can init the other base class,
116 # see wxPython/lib/mixins/listctrl.py
117 self
.itemDataMap
= musicdata
118 listmix
.ColumnSorterMixin
.__init
__(self
, 3)
119 #self.SortListItems(0, True)
121 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
123 self
.Bind(wx
.EVT_LIST_ITEM_SELECTED
, self
.OnItemSelected
, self
.list)
124 self
.Bind(wx
.EVT_LIST_ITEM_DESELECTED
, self
.OnItemDeselected
, self
.list)
125 self
.Bind(wx
.EVT_LIST_ITEM_ACTIVATED
, self
.OnItemActivated
, self
.list)
126 self
.Bind(wx
.EVT_LIST_DELETE_ITEM
, self
.OnItemDelete
, self
.list)
127 self
.Bind(wx
.EVT_LIST_COL_CLICK
, self
.OnColClick
, self
.list)
128 self
.Bind(wx
.EVT_LIST_COL_RIGHT_CLICK
, self
.OnColRightClick
, self
.list)
129 self
.Bind(wx
.EVT_LIST_COL_BEGIN_DRAG
, self
.OnColBeginDrag
, self
.list)
130 self
.Bind(wx
.EVT_LIST_COL_DRAGGING
, self
.OnColDragging
, self
.list)
131 self
.Bind(wx
.EVT_LIST_COL_END_DRAG
, self
.OnColEndDrag
, self
.list)
132 self
.Bind(wx
.EVT_LIST_BEGIN_LABEL_EDIT
, self
.OnBeginEdit
, self
.list)
134 self
.list.Bind(wx
.EVT_LEFT_DCLICK
, self
.OnDoubleClick
)
135 self
.list.Bind(wx
.EVT_RIGHT_DOWN
, self
.OnRightDown
)
138 self
.list.Bind(wx
.EVT_COMMAND_RIGHT_CLICK
, self
.OnRightClick
)
141 self
.list.Bind(wx
.EVT_RIGHT_UP
, self
.OnRightClick
)
144 def PopulateList(self
):
146 # for normal, simple columns, you can add them like this:
147 self
.list.InsertColumn(0, "Artist")
148 self
.list.InsertColumn(1, "Title", wx
.LIST_FORMAT_RIGHT
)
149 self
.list.InsertColumn(2, "Genre")
151 # but since we want images on the column header we have to do it the hard way:
153 info
.m_mask
= wx
.LIST_MASK_TEXT | wx
.LIST_MASK_IMAGE | wx
.LIST_MASK_FORMAT
156 info
.m_text
= "Artist"
157 self
.list.InsertColumnInfo(0, info
)
159 info
.m_format
= wx
.LIST_FORMAT_RIGHT
160 info
.m_text
= "Title"
161 self
.list.InsertColumnInfo(1, info
)
164 info
.m_text
= "Genre"
165 self
.list.InsertColumnInfo(2, info
)
167 items
= musicdata
.items()
168 for key
, data
in items
:
169 index
= self
.list.InsertImageStringItem(sys
.maxint
, data
[0], self
.idx1
)
170 self
.list.SetStringItem(index
, 1, data
[1])
171 self
.list.SetStringItem(index
, 2, data
[2])
172 self
.list.SetItemData(index
, key
)
174 self
.list.SetColumnWidth(0, wx
.LIST_AUTOSIZE
)
175 self
.list.SetColumnWidth(1, wx
.LIST_AUTOSIZE
)
176 self
.list.SetColumnWidth(2, 100)
178 # show how to select an item
179 self
.list.SetItemState(5, wx
.LIST_STATE_SELECTED
, wx
.LIST_STATE_SELECTED
)
181 # show how to change the colour of a couple items
182 item
= self
.list.GetItem(1)
183 item
.SetTextColour(wx
.BLUE
)
184 self
.list.SetItem(item
)
185 item
= self
.list.GetItem(4)
186 item
.SetTextColour(wx
.RED
)
187 self
.list.SetItem(item
)
192 # Used by the ColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
193 def GetListCtrl(self
):
196 # Used by the ColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
197 def GetSortImages(self
):
198 return (self
.sm_dn
, self
.sm_up
)
201 def OnRightDown(self
, event
):
202 self
.x
= event
.GetX()
203 self
.y
= event
.GetY()
204 self
.log
.WriteText("x, y = %s\n" % str((self
.x
, self
.y
)))
205 item
, flags
= self
.list.HitTest((self
.x
, self
.y
))
207 if flags
& wx
.LIST_HITTEST_ONITEM
:
208 self
.list.Select(item
)
213 def getColumnText(self
, index
, col
):
214 item
= self
.list.GetItem(index
, col
)
215 return item
.GetText()
218 def OnItemSelected(self
, event
):
219 ##print event.GetItem().GetTextColour()
220 self
.currentItem
= event
.m_itemIndex
221 self
.log
.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
223 self
.list.GetItemText(self
.currentItem
),
224 self
.getColumnText(self
.currentItem
, 1),
225 self
.getColumnText(self
.currentItem
, 2)))
227 if self
.currentItem
== 10:
228 self
.log
.WriteText("OnItemSelected: Veto'd selection\n")
229 #event.Veto() # doesn't work
231 self
.list.SetItemState(10, 0, wx
.LIST_STATE_SELECTED
)
236 def OnItemDeselected(self
, evt
):
238 self
.log
.WriteText("OnItemDeselected: %d" % evt
.m_itemIndex
)
240 # Show how to reselect something we don't want deselected
241 if evt
.m_itemIndex
== 11:
242 wx
.CallAfter(self
.list.SetItemState
, 11, wx
.LIST_STATE_SELECTED
, wx
.LIST_STATE_SELECTED
)
245 def OnItemActivated(self
, event
):
246 self
.currentItem
= event
.m_itemIndex
247 self
.log
.WriteText("OnItemActivated: %s\nTopItem: %s" %
248 (self
.list.GetItemText(self
.currentItem
), self
.list.GetTopItem()))
250 def OnBeginEdit(self
, event
):
251 self
.log
.WriteText("OnBeginEdit")
254 def OnItemDelete(self
, event
):
255 self
.log
.WriteText("OnItemDelete\n")
257 def OnColClick(self
, event
):
258 self
.log
.WriteText("OnColClick: %d\n" % event
.GetColumn())
261 def OnColRightClick(self
, event
):
262 item
= self
.list.GetColumn(event
.GetColumn())
263 self
.log
.WriteText("OnColRightClick: %d %s\n" %
264 (event
.GetColumn(), (item
.GetText(), item
.GetAlign(),
265 item
.GetWidth(), item
.GetImage())))
267 def OnColBeginDrag(self
, event
):
268 self
.log
.WriteText("OnColBeginDrag\n")
269 ## Show how to not allow a column to be resized
270 #if event.GetColumn() == 0:
274 def OnColDragging(self
, event
):
275 self
.log
.WriteText("OnColDragging\n")
277 def OnColEndDrag(self
, event
):
278 self
.log
.WriteText("OnColEndDrag\n")
280 def OnDoubleClick(self
, event
):
281 self
.log
.WriteText("OnDoubleClick item %s\n" % self
.list.GetItemText(self
.currentItem
))
284 def OnRightClick(self
, event
):
285 self
.log
.WriteText("OnRightClick %s\n" % self
.list.GetItemText(self
.currentItem
))
287 # only do this part the first time so the events are only bound once
288 if not hasattr(self
, "popupID1"):
289 self
.popupID1
= wx
.NewId()
290 self
.popupID2
= wx
.NewId()
291 self
.popupID3
= wx
.NewId()
292 self
.popupID4
= wx
.NewId()
293 self
.popupID5
= wx
.NewId()
294 self
.popupID6
= wx
.NewId()
296 self
.Bind(wx
.EVT_MENU
, self
.OnPopupOne
, id=self
.popupID1
)
297 self
.Bind(wx
.EVT_MENU
, self
.OnPopupTwo
, id=self
.popupID2
)
298 self
.Bind(wx
.EVT_MENU
, self
.OnPopupThree
, id=self
.popupID3
)
299 self
.Bind(wx
.EVT_MENU
, self
.OnPopupFour
, id=self
.popupID4
)
300 self
.Bind(wx
.EVT_MENU
, self
.OnPopupFive
, id=self
.popupID5
)
301 self
.Bind(wx
.EVT_MENU
, self
.OnPopupSix
, id=self
.popupID6
)
306 menu
.Append(self
.popupID1
, "FindItem tests")
307 menu
.Append(self
.popupID2
, "Iterate Selected")
308 menu
.Append(self
.popupID3
, "ClearAll and repopulate")
309 menu
.Append(self
.popupID4
, "DeleteAllItems")
310 menu
.Append(self
.popupID5
, "GetItem")
311 menu
.Append(self
.popupID6
, "Edit")
313 # Popup the menu. If an item is selected then its handler
314 # will be called before PopupMenu returns.
315 self
.PopupMenu(menu
, (self
.x
, self
.y
))
319 def OnPopupOne(self
, event
):
320 self
.log
.WriteText("Popup one\n")
321 print "FindItem:", self
.list.FindItem(-1, "Roxette")
322 print "FindItemData:", self
.list.FindItemData(-1, 11)
324 def OnPopupTwo(self
, event
):
325 self
.log
.WriteText("Selected items:\n")
326 index
= self
.list.GetFirstSelected()
329 self
.log
.WriteText(" %s: %s\n" % (self
.list.GetItemText(index
), self
.getColumnText(index
, 1)))
330 index
= self
.list.GetNextSelected(index
)
332 def OnPopupThree(self
, event
):
333 self
.log
.WriteText("Popup three\n")
335 wx
.CallAfter(self
.PopulateList
)
337 def OnPopupFour(self
, event
):
338 self
.list.DeleteAllItems()
340 def OnPopupFive(self
, event
):
341 item
= self
.list.GetItem(self
.currentItem
)
342 print item
.m_text
, item
.m_itemId
, self
.list.GetItemData(self
.currentItem
)
344 def OnPopupSix(self
, event
):
345 self
.list.EditLabel(self
.currentItem
)
348 def OnSize(self
, event
):
349 w
,h
= self
.GetClientSizeTuple()
350 self
.list.SetDimensions(0, 0, w
, h
)
354 #---------------------------------------------------------------------------
356 def runTest(frame
, nb
, log
):
357 win
= TestListCtrlPanel(nb
, log
)
360 #---------------------------------------------------------------------------
366 A list control presents lists in a number of formats: list view, report view,
367 icon view and small icon view. In any case, elements are numbered from zero.
368 For all these modes (but not for virtual list controls), the items are stored
369 in the control and must be added to it using InsertItem method.
371 <p>To intercept events from a list control, use the event table macros described in
372 <code>wxListEvent.</code>
375 This example demonstrates how to use mixins. The following mixins are available.
377 <h4>ColumnSorterMixin</h4>
379 <code><b>ColumnSorterMixin(numColumns)</b></code>
381 <p>A mixin class that handles sorting of a wxListCtrl in REPORT mode when the column
382 header is clicked on.
384 <p>There are a few requirments needed in order for this to work genericly:
386 <li>The combined class must have a <code>GetListCtrl</code> method that returns
387 the ListCtrl to be sorted, and the list control must exist at the time the
388 <code>ColumnSorterMixin.__init__()</code>method is called because it uses
389 <code>GetListCtrl</code>.
391 <li>Items in the list control must have a unique data value set with
392 <code>list.SetItemData</code>.
394 <li>The combined class must have an attribute named <code>itemDataMap</code>
395 that is a dictionary mapping the data values to a sequence of objects
396 representing the values in each column. These valuesare compared in
397 the column sorter to determine sort order.
400 <p>Interesting methods to override are <code>GetColumnSorter</code>,
401 <code>GetSecondarySortValues</code>, and <code>GetSortImages</code>.
405 <dt><code>SetColumnCount(newNumColumns)</code>
406 <dd>Informs the mixin as to the number of columns in the control. When it is
407 set, it also sets up an event handler for <code>EVT_LIST_COL_CLICK</code> events.
409 <dt><code>SortListItems(col=-1, ascending=1)</code>
410 <dd>Sort the list on demand. Can also be used to set the sort column and order.
412 <dt><code>GetColumnWidths()</code>
413 <dd>Returns a list of column widths. Can be used to help restore the current
416 <dt><code>GetSortImages()</code>
417 <dd>Returns a tuple of image list indexes the indexes in the image list for an
418 image to be put on the column header when sorting in descending order
420 <dt><code>GetColumnSorter()</code>
421 <dd>Returns a callable object to be used for comparing column values when sorting.
423 <dt><code>GetSecondarySortValues(col, key1, key2)</code>
424 <dd>Returns a tuple of 2 values to use for secondary sort values when the
425 items in the selected column match equal. The default just returns the
430 <h4>ListCtrlAutoWidthMixin</h4>
432 <code><b>ListCtrlAutoWidthMixin()</b></code>
434 <p>A mix-in class that automatically resizes the last column to take up the
435 remaining width of the ListCtrl.
437 <p>This causes the ListCtrl to automatically take up the full width of the list,
438 without either a horizontal scroll bar (unless absolutely necessary) or empty
439 space to the right of the last column.
441 <p><b>NOTE:</b> This only works for report-style lists.
443 <p><b>WARNING:</b> If you override the <code>EVT_SIZE</code> event in your ListCtrl,
444 make sure you call event.Skip() to ensure that the mixin's _OnResize method is
447 <p>This mix-in class was written by <a href='mailto:ewestra@wave.co.nz'>Erik Westra </a>
452 <dt><code>resizeLastColumn(minWidth)</code>
453 <dd>Resize the last column appropriately. If the list's columns are too wide to
454 fit within the window, we use a horizontal scrollbar. Otherwise, we expand the
455 right-most column to take up the remaining free space in the list. This method is
456 called automatically when the ListCtrl is resized; you can also call it yourself
457 whenever you want the last column to be resized appropriately (eg, when adding,
458 removing or resizing columns). 'minWidth' is the preferred minimum width for
464 <h4>ListCtrlSelectionManagerMix</h4>
466 <code><b>ListCtrlSelectionManagerMix()</b></code>
468 <p>Mixin that defines a platform independent selection policy
470 <p>As selection single and multi-select list return the item index or a
471 list of item indexes respectively.
476 <dt><code>getPopupMenu()</code>
477 <dd>Override to implement dynamic menus (create)
479 <dt><code>setPopupMenu(menu)</code>
480 <dd>Must be set for default behaviour.
482 <dt><code>afterPopupMenu()</code>
483 <dd>Override to implement dynamic menus (destroy).
485 <dt><code>getSelection()</code>
486 <dd>Returns the current selection (or selections as a Python list if extended
487 selection is enabled)
495 if __name__
== '__main__':
498 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])