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