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