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