]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/ListCtrl.py
Little tweaks to match recent wxWidgets updates
[wxWidgets.git] / wxPython / demo / ListCtrl.py
... / ...
CommitLineData
1#----------------------------------------------------------------------------
2# Name: ListCtrl.py
3# Purpose: Testing lots of stuff, controls, window types, etc.
4#
5# Author: Robin Dunn & Gary Dumer
6#
7# Created:
8# RCS-ID: $Id$
9# Copyright: (c) 1998 by Total Control Software
10# Licence: wxWindows license
11#----------------------------------------------------------------------------
12
13import sys
14import wx
15import wx.lib.mixins.listctrl as listmix
16
17import images
18
19#---------------------------------------------------------------------------
20
21musicdata = {
221 : ("Bad English", "The Price Of Love", "Rock"),
232 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
243 : ("George Michael", "Praying For Time", "Rock"),
254 : ("Gloria Estefan", "Here We Are", "Rock"),
265 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
276 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
287 : ("Paul Young", "Oh Girl", "Rock"),
298 : ("Paula Abdul", "Opposites Attract", "Rock"),
309 : ("Richard Marx", "Should've Known Better", "Rock"),
3110: ("Rod Stewart", "Forever Young", "Rock"),
3211: ("Roxette", "Dangerous", "Rock"),
3312: ("Sheena Easton", "The Lover In Me", "Rock"),
3413: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
3514: ("Stevie B.", "Because I Love You", "Rock"),
3615: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
3716: ("The Bangles", "Eternal Flame", "Rock"),
3817: ("Wilson Phillips", "Release Me", "Rock"),
3918: ("Billy Joel", "Blonde Over Blue", "Rock"),
4019: ("Billy Joel", "Famous Last Words", "Rock"),
4120: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
4221: ("Billy Joel", "The River Of Dreams", "Rock"),
4322: ("Billy Joel", "Two Thousand Years", "Rock"),
4423: ("Janet Jackson", "Alright", "Rock"),
4524: ("Janet Jackson", "Black Cat", "Rock"),
4625: ("Janet Jackson", "Come Back To Me", "Rock"),
4726: ("Janet Jackson", "Escapade", "Rock"),
4827: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
4928: ("Janet Jackson", "Miss You Much", "Rock"),
5029: ("Janet Jackson", "Rhythm Nation", "Rock"),
5130: ("Janet Jackson", "State Of The World", "Rock"),
5231: ("Janet Jackson", "The Knowledge", "Rock"),
5332: ("Spyro Gyra", "End of Romanticism", "Jazz"),
5433: ("Spyro Gyra", "Heliopolis", "Jazz"),
5534: ("Spyro Gyra", "Jubilee", "Jazz"),
5635: ("Spyro Gyra", "Little Linda", "Jazz"),
5736: ("Spyro Gyra", "Morning Dance", "Jazz"),
5837: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
5938: ("Yes", "Owner Of A Lonely Heart", "Rock"),
6039: ("Yes", "Rhythm Of Love", "Rock"),
6140: ("Cusco", "Dream Catcher", "New Age"),
6241: ("Cusco", "Geronimos Laughter", "New Age"),
6342: ("Cusco", "Ghost Dance", "New Age"),
6443: ("Blue Man Group", "Drumbone", "New Age"),
6544: ("Blue Man Group", "Endless Column", "New Age"),
6645: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
6746: ("Kenny G", "Silhouette", "Jazz"),
6847: ("Sade", "Smooth Operator", "Jazz"),
6948: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
7049: ("David Arkenstone", "Stepping Stars", "New Age"),
7150: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
7251: ("David Lanz", "Behind The Waterfall", "New Age"),
7352: ("David Lanz", "Cristofori's Dream", "New Age"),
7453: ("David Lanz", "Heartsounds", "New Age"),
7554: ("David Lanz", "Leaves on the Seine", "New Age"),
76}
77
78#---------------------------------------------------------------------------
79
80class 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)
85
86
87class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):
88 def __init__(self, parent, log):
89 wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
90
91 self.log = log
92 tID = wx.NewId()
93
94 self.il = wx.ImageList(16, 16)
95
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())
99
100 self.list = TestListCtrl(self, tID,
101 style=wx.LC_REPORT
102 #| wx.BORDER_SUNKEN
103 | wx.BORDER_NONE
104 | wx.LC_EDIT_LABELS
105 | wx.LC_SORT_ASCENDING
106 #| wx.LC_NO_HEADER
107 #| wx.LC_VRULES
108 #| wx.LC_HRULES
109 #| wx.LC_SINGLE_SEL
110 )
111
112 self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
113
114 self.PopulateList()
115
116 # Now that the list exists we can init the other base class,
117 # see wx/lib/mixins/listctrl.py
118 self.itemDataMap = musicdata
119 listmix.ColumnSorterMixin.__init__(self, 3)
120 #self.SortListItems(0, True)
121
122 self.Bind(wx.EVT_SIZE, self.OnSize)
123
124 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
125 self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list)
126 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
127 self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnItemDelete, self.list)
128 self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list)
129 self.Bind(wx.EVT_LIST_COL_RIGHT_CLICK, self.OnColRightClick, self.list)
130 self.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColBeginDrag, self.list)
131 self.Bind(wx.EVT_LIST_COL_DRAGGING, self.OnColDragging, self.list)
132 self.Bind(wx.EVT_LIST_COL_END_DRAG, self.OnColEndDrag, self.list)
133 self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.list)
134
135 self.list.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
136 self.list.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
137
138 # for wxMSW
139 self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
140
141 # for wxGTK
142 self.list.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
143
144
145 def PopulateList(self):
146 if 0:
147 # for normal, simple columns, you can add them like this:
148 self.list.InsertColumn(0, "Artist")
149 self.list.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
150 self.list.InsertColumn(2, "Genre")
151 else:
152 # but since we want images on the column header we have to do it the hard way:
153 info = wx.ListItem()
154 info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
155 info.m_image = -1
156 info.m_format = 0
157 info.m_text = "Artist"
158 self.list.InsertColumnInfo(0, info)
159
160 info.m_format = wx.LIST_FORMAT_RIGHT
161 info.m_text = "Title"
162 self.list.InsertColumnInfo(1, info)
163
164 info.m_format = 0
165 info.m_text = "Genre"
166 self.list.InsertColumnInfo(2, info)
167
168 items = musicdata.items()
169 for key, data in items:
170 index = self.list.InsertImageStringItem(sys.maxint, data[0], self.idx1)
171 self.list.SetStringItem(index, 1, data[1])
172 self.list.SetStringItem(index, 2, data[2])
173 self.list.SetItemData(index, key)
174
175 self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
176 self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
177 self.list.SetColumnWidth(2, 100)
178
179 # show how to select an item
180 self.list.SetItemState(5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
181
182 # show how to change the colour of a couple items
183 item = self.list.GetItem(1)
184 item.SetTextColour(wx.BLUE)
185 self.list.SetItem(item)
186 item = self.list.GetItem(4)
187 item.SetTextColour(wx.RED)
188 self.list.SetItem(item)
189
190 self.currentItem = 0
191
192
193 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
194 def GetListCtrl(self):
195 return self.list
196
197 # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
198 def GetSortImages(self):
199 return (self.sm_dn, self.sm_up)
200
201
202 def OnRightDown(self, event):
203 self.x = event.GetX()
204 self.y = event.GetY()
205 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
206 item, flags = self.list.HitTest((self.x, self.y))
207
208 if flags & wx.LIST_HITTEST_ONITEM:
209 self.list.Select(item)
210
211 event.Skip()
212
213
214 def getColumnText(self, index, col):
215 item = self.list.GetItem(index, col)
216 return item.GetText()
217
218
219 def OnItemSelected(self, event):
220 ##print event.GetItem().GetTextColour()
221 self.currentItem = event.m_itemIndex
222 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
223 (self.currentItem,
224 self.list.GetItemText(self.currentItem),
225 self.getColumnText(self.currentItem, 1),
226 self.getColumnText(self.currentItem, 2)))
227
228 if self.currentItem == 10:
229 self.log.WriteText("OnItemSelected: Veto'd selection\n")
230 #event.Veto() # doesn't work
231 # this does
232 self.list.SetItemState(10, 0, wx.LIST_STATE_SELECTED)
233
234 event.Skip()
235
236
237 def OnItemDeselected(self, evt):
238 item = evt.GetItem()
239 self.log.WriteText("OnItemDeselected: %d" % evt.m_itemIndex)
240
241 # Show how to reselect something we don't want deselected
242 if evt.m_itemIndex == 11:
243 wx.CallAfter(self.list.SetItemState, 11, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
244
245
246 def OnItemActivated(self, event):
247 self.currentItem = event.m_itemIndex
248 self.log.WriteText("OnItemActivated: %s\nTopItem: %s" %
249 (self.list.GetItemText(self.currentItem), self.list.GetTopItem()))
250
251 def OnBeginEdit(self, event):
252 self.log.WriteText("OnBeginEdit")
253 event.Allow()
254
255 def OnItemDelete(self, event):
256 self.log.WriteText("OnItemDelete\n")
257
258 def OnColClick(self, event):
259 self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
260 event.Skip()
261
262 def OnColRightClick(self, event):
263 item = self.list.GetColumn(event.GetColumn())
264 self.log.WriteText("OnColRightClick: %d %s\n" %
265 (event.GetColumn(), (item.GetText(), item.GetAlign(),
266 item.GetWidth(), item.GetImage())))
267
268 def OnColBeginDrag(self, event):
269 self.log.WriteText("OnColBeginDrag\n")
270 ## Show how to not allow a column to be resized
271 #if event.GetColumn() == 0:
272 # event.Veto()
273
274
275 def OnColDragging(self, event):
276 self.log.WriteText("OnColDragging\n")
277
278 def OnColEndDrag(self, event):
279 self.log.WriteText("OnColEndDrag\n")
280
281 def OnDoubleClick(self, event):
282 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
283 event.Skip()
284
285 def OnRightClick(self, event):
286 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
287
288 # only do this part the first time so the events are only bound once
289 if not hasattr(self, "popupID1"):
290 self.popupID1 = wx.NewId()
291 self.popupID2 = wx.NewId()
292 self.popupID3 = wx.NewId()
293 self.popupID4 = wx.NewId()
294 self.popupID5 = wx.NewId()
295 self.popupID6 = wx.NewId()
296
297 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
298 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
299 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
300 self.Bind(wx.EVT_MENU, self.OnPopupFour, id=self.popupID4)
301 self.Bind(wx.EVT_MENU, self.OnPopupFive, id=self.popupID5)
302 self.Bind(wx.EVT_MENU, self.OnPopupSix, id=self.popupID6)
303
304 # make a menu
305 menu = wx.Menu()
306 # add some items
307 menu.Append(self.popupID1, "FindItem tests")
308 menu.Append(self.popupID2, "Iterate Selected")
309 menu.Append(self.popupID3, "ClearAll and repopulate")
310 menu.Append(self.popupID4, "DeleteAllItems")
311 menu.Append(self.popupID5, "GetItem")
312 menu.Append(self.popupID6, "Edit")
313
314 # Popup the menu. If an item is selected then its handler
315 # will be called before PopupMenu returns.
316 self.PopupMenu(menu, (self.x, self.y))
317 menu.Destroy()
318
319
320 def OnPopupOne(self, event):
321 self.log.WriteText("Popup one\n")
322 print "FindItem:", self.list.FindItem(-1, "Roxette")
323 print "FindItemData:", self.list.FindItemData(-1, 11)
324
325 def OnPopupTwo(self, event):
326 self.log.WriteText("Selected items:\n")
327 index = self.list.GetFirstSelected()
328
329 while index != -1:
330 self.log.WriteText(" %s: %s\n" % (self.list.GetItemText(index), self.getColumnText(index, 1)))
331 index = self.list.GetNextSelected(index)
332
333 def OnPopupThree(self, event):
334 self.log.WriteText("Popup three\n")
335 self.list.ClearAll()
336 wx.CallAfter(self.PopulateList)
337
338 def OnPopupFour(self, event):
339 self.list.DeleteAllItems()
340
341 def OnPopupFive(self, event):
342 item = self.list.GetItem(self.currentItem)
343 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
344
345 def OnPopupSix(self, event):
346 self.list.EditLabel(self.currentItem)
347
348
349 def OnSize(self, event):
350 w,h = self.GetClientSizeTuple()
351 self.list.SetDimensions(0, 0, w, h)
352
353
354
355#---------------------------------------------------------------------------
356
357def runTest(frame, nb, log):
358 win = TestListCtrlPanel(nb, log)
359 return win
360
361#---------------------------------------------------------------------------
362
363
364overview = """\
365<html>
366<body>
367A list control presents lists in a number of formats: list view, report view,
368icon view and small icon view. In any case, elements are numbered from zero.
369For all these modes (but not for virtual list controls), the items are stored
370in the control and must be added to it using InsertItem method.
371
372<p>To intercept events from a list control, use the event table macros described in
373<code>wxListEvent.</code>
374
375<h3>Mix-ins</h3>
376This example demonstrates how to use mixins. The following mixins are available.
377
378<h4>ColumnSorterMixin</h4>
379
380<code><b>ColumnSorterMixin(numColumns)</b></code>
381
382<p>A mixin class that handles sorting of a wxListCtrl in REPORT mode when the column
383header is clicked on.
384
385<p>There are a few requirments needed in order for this to work genericly:
386<p><ol>
387 <li>The combined class must have a <code>GetListCtrl</code> method that returns
388 the ListCtrl to be sorted, and the list control must exist at the time the
389 <code>ColumnSorterMixin.__init__()</code>method is called because it uses
390 <code>GetListCtrl</code>.
391
392 <li>Items in the list control must have a unique data value set with
393 <code>list.SetItemData</code>.
394
395 <li>The combined class must have an attribute named <code>itemDataMap</code>
396 that is a dictionary mapping the data values to a sequence of objects
397 representing the values in each column. These valuesare compared in
398 the column sorter to determine sort order.
399</ol>
400
401<p>Interesting methods to override are <code>GetColumnSorter</code>,
402<code>GetSecondarySortValues</code>, and <code>GetSortImages</code>.
403
404<h5>Methods</h5>
405<dl>
406<dt><code>SetColumnCount(newNumColumns)</code>
407<dd>Informs the mixin as to the number of columns in the control. When it is
408set, it also sets up an event handler for <code>EVT_LIST_COL_CLICK</code> events.
409
410<dt><code>SortListItems(col=-1, ascending=1)</code>
411<dd>Sort the list on demand. Can also be used to set the sort column and order.
412
413<dt><code>GetColumnWidths()</code>
414<dd>Returns a list of column widths. Can be used to help restore the current
415view later.
416
417<dt><code>GetSortImages()</code>
418<dd>Returns a tuple of image list indexes the indexes in the image list for an
419image to be put on the column header when sorting in descending order
420
421<dt><code>GetColumnSorter()</code>
422<dd>Returns a callable object to be used for comparing column values when sorting.
423
424<dt><code>GetSecondarySortValues(col, key1, key2)</code>
425<dd>Returns a tuple of 2 values to use for secondary sort values when the
426items in the selected column match equal. The default just returns the
427item data values.
428
429</dl>
430
431<h4>ListCtrlAutoWidthMixin</h4>
432
433<code><b>ListCtrlAutoWidthMixin()</b></code>
434
435<p>A mix-in class that automatically resizes the last column to take up the
436remaining width of the ListCtrl.
437
438<p>This causes the ListCtrl to automatically take up the full width of the list,
439without either a horizontal scroll bar (unless absolutely necessary) or empty
440space to the right of the last column.
441
442<p><b>NOTE:</b> This only works for report-style lists.
443
444<p><b>WARNING:</b> If you override the <code>EVT_SIZE</code> event in your ListCtrl,
445make sure you call event.Skip() to ensure that the mixin's _OnResize method is
446called.
447
448<p>This mix-in class was written by <a href='mailto:ewestra@wave.co.nz'>Erik Westra </a>
449
450<h5>Methods</h5>
451<dl>
452
453<dt><code>resizeLastColumn(minWidth)</code>
454<dd>Resize the last column appropriately. If the list's columns are too wide to
455fit within the window, we use a horizontal scrollbar. Otherwise, we expand the
456right-most column to take up the remaining free space in the list. This method is
457called automatically when the ListCtrl is resized; you can also call it yourself
458whenever you want the last column to be resized appropriately (eg, when adding,
459removing or resizing columns). 'minWidth' is the preferred minimum width for
460the last column.
461
462</dl>
463
464
465<h4>ListCtrlSelectionManagerMix</h4>
466
467<code><b>ListCtrlSelectionManagerMix()</b></code>
468
469<p>Mixin that defines a platform independent selection policy
470
471<p>As selection single and multi-select list return the item index or a
472list of item indexes respectively.
473
474<h5>Methods</h5>
475<dl>
476
477<dt><code>getPopupMenu()</code>
478<dd>Override to implement dynamic menus (create)
479
480<dt><code>setPopupMenu(menu)</code>
481<dd>Must be set for default behaviour.
482
483<dt><code>afterPopupMenu()</code>
484<dd>Override to implement dynamic menus (destroy).
485
486<dt><code>getSelection()</code>
487<dd>Returns the current selection (or selections as a Python list if extended
488selection is enabled)
489
490
491</body>
492</html>
493"""
494
495
496if __name__ == '__main__':
497 import sys,os
498 import run
499 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
500