]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/demo/wxListCtrl.py
New wxDesigner-less version of the MimeTypesManager demo
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
index cbe8cd0958d87ba5a319b1b1cda0a8c7dd762494..879bc1baa0e535e603c97a9ded0f24ebbe2ba24b 100644 (file)
@@ -1,4 +1,3 @@
-#!/bin/env python
 #----------------------------------------------------------------------------
 # Name:         ListCtrl.py
 # Purpose:      Testing lots of stuff, controls, window types, etc.
@@ -10,8 +9,31 @@
 # Copyright:    (c) 1998 by Total Control Software
 # Licence:      wxWindows license
 #----------------------------------------------------------------------------
+#
+# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o Updated for wx namespace
+# 
+# 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o listctrl mixin needs wx renamer.
+# o wx.ListItem.GetText() returns a wxString pointer, not the text.
+# 
+# 12/14/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o ColumnSorterMixin implementation was broke - added event.Skip()
+#   to column click event to allow event to fall through to mixin.
+#
+# 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o wxColumnSorterMixin -> ColumnSorterMixin
+# o wxListCtrlAutoWidthMixin -> ListCtrlAutoWidthMixin 
+#
+
+import  wx
+import  wx.lib.mixins.listctrl  as  listmix
 
-from wxPython.wx import *
+import  images
 
 #---------------------------------------------------------------------------
 
@@ -55,74 +77,153 @@ musicdata = {
 37: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
 38: ("Yes", "Owner Of A Lonely Heart", "Rock"),
 39: ("Yes", "Rhythm Of Love", "Rock"),
+40: ("Cusco", "Dream Catcher", "New Age"),
+41: ("Cusco", "Geronimos Laughter", "New Age"),
+42: ("Cusco", "Ghost Dance", "New Age"),
+43: ("Blue Man Group", "Drumbone", "New Age"),
+44: ("Blue Man Group", "Endless Column", "New Age"),
+45: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
+46: ("Kenny G", "Silhouette", "Jazz"),
+47: ("Sade", "Smooth Operator", "Jazz"),
+48: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
+49: ("David Arkenstone", "Stepping Stars", "New Age"),
+50: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
+51: ("David Lanz", "Behind The Waterfall", "New Age"),
+52: ("David Lanz", "Cristofori's Dream", "New Age"),
+53: ("David Lanz", "Heartsounds", "New Age"),
+54: ("David Lanz", "Leaves on the Seine", "New Age"),
 }
 
-import images
+#---------------------------------------------------------------------------
+
+class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
+    def __init__(self, parent, ID, pos=wx.DefaultPosition,
+                 size=wx.DefaultSize, style=0):
+        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
+        listmix.ListCtrlAutoWidthMixin.__init__(self)
 
-class TestListCtrlPanel(wxPanel):
+
+class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):
     def __init__(self, parent, log):
-        wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
+        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)
 
         self.log = log
-        tID = wxNewId()
+        tID = wx.NewId()
+
+        self.il = wx.ImageList(16, 16)
+
+        self.idx1 = self.il.Add(images.getSmilesBitmap())
+        self.sm_up = self.il.Add(images.getSmallUpArrowBitmap())
+        self.sm_dn = self.il.Add(images.getSmallDnArrowBitmap())
+
+        self.list = TestListCtrl(self, tID,
+                                 style=wx.LC_REPORT 
+                                 | wx.SUNKEN_BORDER
+                                 | wx.LC_EDIT_LABELS
+                                 #| wxLC_NO_HEADER
+                                 #| wxLC_VRULES | wxLC_HRULES
+                                 )
+
+        self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
 
-        self.il = wxImageList(16, 16)
-        bmp = images.getSmilesBitmap()
-        #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
-        idx1 = self.il.Add(bmp)
+        self.PopulateList()
 
-        self.list = wxListCtrl(self, tID,
-                               style=wxLC_REPORT|wxSUNKEN_BORDER)
-        self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
+        # Now that the list exists we can init the other base class,
+        # see wxPython/lib/mixins/listctrl.py
+        self.itemDataMap = musicdata
+        listmix.ColumnSorterMixin.__init__(self, 3)
+        #self.SortListItems(0, True)
 
-        #  Why doesn't this show up on MSW???
-        self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
+        self.Bind(wx.EVT_SIZE, self.OnSize)
+
+        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
+        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list)
+        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
+        self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnItemDelete, self.list)
+        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list)
+        self.Bind(wx.EVT_LIST_COL_RIGHT_CLICK, self.OnColRightClick, self.list)
+        self.Bind(wx.EVT_LIST_COL_BEGIN_DRAG, self.OnColBeginDrag, self.list)
+        self.Bind(wx.EVT_LIST_COL_DRAGGING, self.OnColDragging, self.list)
+        self.Bind(wx.EVT_LIST_COL_END_DRAG, self.OnColEndDrag, self.list)
+        self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.list)
+
+        self.list.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)
+        self.list.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
+
+        # for wxMSW
+        self.list.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightClick)
+
+        # for wxGTK
+        self.list.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
+
+
+    def PopulateList(self):
+        if 0:
+            # for normal, simple columns, you can add them like this:
+            self.list.InsertColumn(0, "Artist")
+            self.list.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT)
+            self.list.InsertColumn(2, "Genre")
+        else:
+            # but since we want images on the column header we have to do it the hard way:
+            info = wx.ListItem()
+            info.m_mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
+            info.m_image = -1
+            info.m_format = 0
+            info.m_text = "Artist"
+            self.list.InsertColumnInfo(0, info)
+
+            info.m_format = wx.LIST_FORMAT_RIGHT
+            info.m_text = "Title"
+            self.list.InsertColumnInfo(1, info)
+
+            info.m_format = 0
+            info.m_text = "Genre"
+            self.list.InsertColumnInfo(2, info)
 
-        self.list.InsertColumn(0, "Artist")
-        self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
-        self.list.InsertColumn(2, "Genre")
         items = musicdata.items()
         for x in range(len(items)):
             key, data = items[x]
-            self.list.InsertImageStringItem(x, data[0], idx1)
+            self.list.InsertImageStringItem(x, data[0], self.idx1)
             self.list.SetStringItem(x, 1, data[1])
             self.list.SetStringItem(x, 2, data[2])
             self.list.SetItemData(x, key)
 
-        self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
-        self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
-        ##self.list.SetColumnWidth(2, wxLIST_AUTOSIZE)
-
-        self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
+        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE)
+        self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
+        self.list.SetColumnWidth(2, 100)
 
-        #self.list.SetItemState(25, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
-        #self.list.EnsureVisible(25)
+        # show how to select an item
+        self.list.SetItemState(5, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
 
-        # show how to change the colour of an item
+        # show how to change the colour of a couple items
         item = self.list.GetItem(1)
-        item.SetTextColour(wxBLUE)
+        item.SetTextColour(wx.BLUE)
+        self.list.SetItem(item)
+        item = self.list.GetItem(4)
+        item.SetTextColour(wx.RED)
         self.list.SetItem(item)
 
         self.currentItem = 0
-        EVT_SIZE(self, self.OnSize)
-        EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
-        EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
-        EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
-        EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
-        EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
-        EVT_RIGHT_DOWN(self.list, self.OnRightDown)
 
-        # for wxMSW
-        EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
 
-        # for wxGTK
-        EVT_RIGHT_UP(self.list, self.OnRightClick)
+    # Used by the ColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
+    def GetListCtrl(self):
+        return self.list
+
+    # Used by the ColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
+    def GetSortImages(self):
+        return (self.sm_dn, self.sm_up)
 
 
     def OnRightDown(self, event):
         self.x = event.GetX()
         self.y = event.GetY()
         self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
+        item, flags = self.list.HitTest((self.x, self.y))
+
+        if flags & wx.LIST_HITTEST_ONITEM:
+            self.list.Select(item)
+
         event.Skip()
 
 
@@ -132,37 +233,66 @@ class TestListCtrlPanel(wxPanel):
 
 
     def OnItemSelected(self, event):
+        ##print event.GetItem().GetTextColour()
         self.currentItem = event.m_itemIndex
         self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
                            (self.currentItem,
                             self.list.GetItemText(self.currentItem),
                             self.getColumnText(self.currentItem, 1),
                             self.getColumnText(self.currentItem, 2)))
+
         if self.currentItem == 10:
             self.log.WriteText("OnItemSelected: Veto'd selection\n")
             #event.Veto()  # doesn't work
             # this does
-            self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
+            self.list.SetItemState(10, 0, wx.LIST_STATE_SELECTED)
+
+        event.Skip()
+
+
+    def OnItemDeselected(self, evt):
+        item = evt.GetItem()
+        self.log.WriteText("OnItemDeselected: %d" % evt.m_itemIndex)
+
+        # Show how to reselect something we don't want deselected
+        if evt.m_itemIndex == 11:
+            wx.CallAfter(self.list.SetItemState, 11, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
+
 
     def OnItemActivated(self, event):
         self.currentItem = event.m_itemIndex
-        self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
+        self.log.WriteText("OnItemActivated: %s\nTopItem: %s" %
+                           (self.list.GetItemText(self.currentItem), self.list.GetTopItem()))
+
+    def OnBeginEdit(self, event):
+        self.log.WriteText("OnBeginEdit")
+        event.Allow()
 
     def OnItemDelete(self, event):
         self.log.WriteText("OnItemDelete\n")
 
     def OnColClick(self, event):
-        self.log.WriteText("OnColClick: %d\n" % event.m_col)
-        self.col = event.m_col
-        self.list.SortItems(self.ColumnSorter)
+        self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
+        event.Skip()
+
+    def OnColRightClick(self, event):
+        item = self.list.GetColumn(event.GetColumn())
+        self.log.WriteText("OnColRightClick: %d %s\n" %
+                           (event.GetColumn(), (item.GetText(), item.GetAlign(),
+                                                item.GetWidth(), item.GetImage())))
+
+    def OnColBeginDrag(self, event):
+        self.log.WriteText("OnColBeginDrag\n")
+        ## Show how to not allow a column to be resized
+        #if event.GetColumn() == 0:
+        #    event.Veto()
 
-    def ColumnSorter(self, key1, key2):
-        item1 = musicdata[key1][self.col]
-        item2 = musicdata[key2][self.col]
-        if item1 == item2:  return 0
-        elif item1 < item2: return -1
-        else:               return 1
 
+    def OnColDragging(self, event):
+        self.log.WriteText("OnColDragging\n")
+
+    def OnColEndDrag(self, event):
+        self.log.WriteText("OnColEndDrag\n")
 
     def OnDoubleClick(self, event):
         self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
@@ -170,37 +300,56 @@ class TestListCtrlPanel(wxPanel):
 
     def OnRightClick(self, event):
         self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
-        menu = wxMenu()
-        tPopupID1 = 0
-        tPopupID2 = 1
-        tPopupID3 = 2
-        tPopupID4 = 3
-        tPopupID5 = 5
-        #menu.Append(tPopupID1, "One")
-        item = wxMenuItem(menu, tPopupID1,"One")
-        item.SetBitmap(images.getSmilesBitmap())
-        menu.AppendItem(item)
-        menu.Append(tPopupID2, "Two")
-        menu.Append(tPopupID3, "Three")
-        menu.Append(tPopupID4, "DeleteAllItems")
-        menu.Append(tPopupID5, "GetItem")
-        EVT_MENU(self, tPopupID1, self.OnPopupOne)
-        EVT_MENU(self, tPopupID2, self.OnPopupTwo)
-        EVT_MENU(self, tPopupID3, self.OnPopupThree)
-        EVT_MENU(self, tPopupID4, self.OnPopupFour)
-        EVT_MENU(self, tPopupID5, self.OnPopupFive)
-        self.PopupMenu(menu, wxPoint(self.x, self.y))
+
+        # only do this part the first time so the events are only bound once
+        if not hasattr(self, "popupID1"):
+            self.popupID1 = wx.NewId()
+            self.popupID2 = wx.NewId()
+            self.popupID3 = wx.NewId()
+            self.popupID4 = wx.NewId()
+            self.popupID5 = wx.NewId()
+            self.popupID6 = wx.NewId()
+
+            self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
+            self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
+            self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
+            self.Bind(wx.EVT_MENU, self.OnPopupFour, id=self.popupID4)
+            self.Bind(wx.EVT_MENU, self.OnPopupFive, id=self.popupID5)
+            self.Bind(wx.EVT_MENU, self.OnPopupSix, id=self.popupID6)
+
+        # make a menu
+        menu = wx.Menu()
+        # add some items
+        menu.Append(self.popupID1, "FindItem tests")
+        menu.Append(self.popupID2, "Iterate Selected")
+        menu.Append(self.popupID3, "ClearAll and repopulate")
+        menu.Append(self.popupID4, "DeleteAllItems")
+        menu.Append(self.popupID5, "GetItem")
+        menu.Append(self.popupID6, "Edit")
+
+        # Popup the menu.  If an item is selected then its handler
+        # will be called before PopupMenu returns.
+        self.PopupMenu(menu, (self.x, self.y))
         menu.Destroy()
-        event.Skip()
+
 
     def OnPopupOne(self, event):
         self.log.WriteText("Popup one\n")
+        print "FindItem:", self.list.FindItem(-1, "Roxette")
+        print "FindItemData:", self.list.FindItemData(-1, 11)
 
     def OnPopupTwo(self, event):
-        self.log.WriteText("Popup two\n")
+        self.log.WriteText("Selected items:\n")
+        index = self.list.GetFirstSelected()
+
+        while index != -1:
+            self.log.WriteText("      %s: %s\n" % (self.list.GetItemText(index), self.getColumnText(index, 1)))
+            index = self.list.GetNextSelected(index)
 
     def OnPopupThree(self, event):
         self.log.WriteText("Popup three\n")
+        self.list.ClearAll()
+        wx.CallAfter(self.PopulateList)
 
     def OnPopupFour(self, event):
         self.list.DeleteAllItems()
@@ -209,15 +358,16 @@ class TestListCtrlPanel(wxPanel):
         item = self.list.GetItem(self.currentItem)
         print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
 
+    def OnPopupSix(self, event):
+        self.list.EditLabel(self.currentItem)
+
+
     def OnSize(self, event):
         w,h = self.GetClientSizeTuple()
         self.list.SetDimensions(0, 0, w, h)
 
 
 
-
-
-
 #---------------------------------------------------------------------------
 
 def runTest(frame, nb, log):
@@ -227,21 +377,140 @@ def runTest(frame, nb, log):
 #---------------------------------------------------------------------------
 
 
+overview = """\
+<html>
+<body>
+A list control presents lists in a number of formats: list view, report view, 
+icon view and small icon view. In any case, elements are numbered from zero. 
+For all these modes (but not for virtual list controls), the items are stored 
+in the control and must be added to it using InsertItem method.
 
+<p>To intercept events from a list control, use the event table macros described in 
+<code>wxListEvent.</code>
 
+<h3>Mix-ins</h3>
+This example demonstrates how to use mixins. The following mixins are available.
 
+<h4>ColumnSorterMixin</h4>
 
+<code><b>ColumnSorterMixin(numColumns)</b></code>
 
+<p>A mixin class that handles sorting of a wxListCtrl in REPORT mode when the column 
+header is clicked on.
 
+<p>There are a few requirments needed in order for this to work genericly:
+<p><ol>
+    <li>The combined class must have a <code>GetListCtrl</code> method that returns 
+    the ListCtrl to be sorted, and the list control must exist at the time the 
+    <code>ColumnSorterMixin.__init__()</code>method is called because it uses 
+    <code>GetListCtrl</code>.
 
+    <li>Items in the list control must have a unique data value set with 
+    <code>list.SetItemData</code>.
 
+    <li>The combined class must have an attribute named <code>itemDataMap</code>
+    that is a dictionary mapping the data values to a sequence of objects 
+    representing the values in each column.  These valuesare compared in 
+    the column sorter to determine sort order.
+</ol>
 
+<p>Interesting methods to override are <code>GetColumnSorter</code>,
+<code>GetSecondarySortValues</code>, and <code>GetSortImages</code>.
 
+<h5>Methods</h5>
+<dl>
+<dt><code>SetColumnCount(newNumColumns)</code>
+<dd>Informs the mixin as to the number of columns in the control. When it is
+set, it also sets up an event handler for <code>EVT_LIST_COL_CLICK</code> events.
 
+<dt><code>SortListItems(col=-1, ascending=1)</code>
+<dd>Sort the list on demand.  Can also be used to set the sort column and order.
 
+<dt><code>GetColumnWidths()</code>
+<dd>Returns a list of column widths.  Can be used to help restore the current 
+view later.
 
+<dt><code>GetSortImages()</code>
+<dd>Returns a tuple of image list indexes the indexes in the image list for an 
+image to be put on the column header when sorting in descending order
 
-overview = """\
-A list control presents lists in a number of formats: list view, report view, icon view and small icon view. Elements are numbered from zero.
+<dt><code>GetColumnSorter()</code>
+<dd>Returns a callable object to be used for comparing column values when sorting.
+
+<dt><code>GetSecondarySortValues(col, key1, key2)</code>
+<dd>Returns a tuple of 2 values to use for secondary sort values when the
+items in the selected column match equal.  The default just returns the 
+item data values.
+
+</dl>
+
+<h4>ListCtrlAutoWidthMixin</h4>
+
+<code><b>ListCtrlAutoWidthMixin()</b></code>
+
+<p>A mix-in class that automatically resizes the last column to take up the 
+remaining width of the ListCtrl.
+
+<p>This causes the ListCtrl to automatically take up the full width of the list, 
+without either a horizontal scroll bar (unless absolutely necessary) or empty 
+space to the right of the last column.
+
+<p><b>NOTE:</b> This only works for report-style lists.
 
+<p><b>WARNING:</b> If you override the <code>EVT_SIZE</code> event in your ListCtrl, 
+make sure you call event.Skip() to ensure that the mixin's _OnResize method is 
+called.
+
+<p>This mix-in class was written by <a href='mailto:ewestra@wave.co.nz'>Erik Westra </a>
+
+<h5>Methods</h5>
+<dl>
+
+<dt><code>resizeLastColumn(minWidth)</code>
+<dd>Resize the last column appropriately. If the list's columns are too wide to 
+fit within the window, we use a horizontal scrollbar.  Otherwise, we expand the 
+right-most column to take up the remaining free space in the list. This method is 
+called automatically when the ListCtrl is resized; you can also call it yourself 
+whenever you want the last column to be resized appropriately (eg, when adding, 
+removing or resizing columns).  'minWidth' is the preferred minimum width for 
+the last column.
+
+</dl>
+
+
+<h4>ListCtrlSelectionManagerMix</h4>
+
+<code><b>ListCtrlSelectionManagerMix()</b></code>
+
+<p>Mixin that defines a platform independent selection policy
+
+<p>As selection single and multi-select list return the item index or a 
+list of item indexes respectively.
+
+<h5>Methods</h5>
+<dl>
+
+<dt><code>getPopupMenu()</code>
+<dd>Override to implement dynamic menus (create)
+
+<dt><code>setPopupMenu(menu)</code>
+<dd>Must be set for default behaviour.
+
+<dt><code>afterPopupMenu()</code>
+<dd>Override to implement dynamic menus (destroy).
+
+<dt><code>getSelection()</code>
+<dd>Returns the current selection (or selections  as a Python list if extended 
+selection is enabled)
+
+
+</body>
+</html>
 """
+
+
+if __name__ == '__main__':
+    import sys,os
+    import run
+    run.main(['', os.path.basename(sys.argv[0])])
+