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