| 1 | import sys |
| 2 | import wx |
| 3 | from wx.lib.mixins.listctrl import CheckListCtrlMixin |
| 4 | |
| 5 | from ListCtrl import musicdata |
| 6 | |
| 7 | #---------------------------------------------------------------------- |
| 8 | |
| 9 | class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin): |
| 10 | def __init__(self, parent, log): |
| 11 | wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT) |
| 12 | CheckListCtrlMixin.__init__(self) |
| 13 | self.log = log |
| 14 | self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated) |
| 15 | |
| 16 | |
| 17 | def OnItemActivated(self, evt): |
| 18 | self.ToggleItem(evt.m_itemIndex) |
| 19 | |
| 20 | |
| 21 | # this is called by the base class when an item is checked/unchecked |
| 22 | def OnCheckItem(self, index, flag): |
| 23 | data = self.GetItemData(index) |
| 24 | title = musicdata[data][1] |
| 25 | if flag: |
| 26 | what = "checked" |
| 27 | else: |
| 28 | what = "unchecked" |
| 29 | self.log.write('item "%s", at index %d was %s\n' % (title, index, what)) |
| 30 | |
| 31 | |
| 32 | |
| 33 | class TestPanel(wx.Panel): |
| 34 | def __init__(self, parent, log): |
| 35 | self.log = log |
| 36 | wx.Panel.__init__(self, parent, -1) |
| 37 | |
| 38 | self.list = CheckListCtrl(self, log) |
| 39 | sizer = wx.BoxSizer() |
| 40 | sizer.Add(self.list, 1, wx.EXPAND) |
| 41 | self.SetSizer(sizer) |
| 42 | |
| 43 | self.list.InsertColumn(0, "Artist") |
| 44 | self.list.InsertColumn(1, "Title", wx.LIST_FORMAT_RIGHT) |
| 45 | self.list.InsertColumn(2, "Genre") |
| 46 | |
| 47 | for key, data in musicdata.iteritems(): |
| 48 | index = self.list.InsertStringItem(sys.maxint, data[0]) |
| 49 | self.list.SetStringItem(index, 1, data[1]) |
| 50 | self.list.SetStringItem(index, 2, data[2]) |
| 51 | self.list.SetItemData(index, key) |
| 52 | |
| 53 | self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE) |
| 54 | self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE) |
| 55 | self.list.SetColumnWidth(2, 100) |
| 56 | |
| 57 | self.list.CheckItem(4) |
| 58 | self.list.CheckItem(7) |
| 59 | |
| 60 | self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list) |
| 61 | self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list) |
| 62 | |
| 63 | |
| 64 | def OnItemSelected(self, evt): |
| 65 | self.log.write('item selected: %s\n' % evt.m_itemIndex) |
| 66 | |
| 67 | def OnItemDeselected(self, evt): |
| 68 | self.log.write('item deselected: %s\n' % evt.m_itemIndex) |
| 69 | |
| 70 | |
| 71 | #---------------------------------------------------------------------- |
| 72 | |
| 73 | def runTest(frame, nb, log): |
| 74 | win = TestPanel(nb, log) |
| 75 | return win |
| 76 | |
| 77 | #---------------------------------------------------------------------- |
| 78 | |
| 79 | |
| 80 | |
| 81 | overview = """<html><body> |
| 82 | <h2><centerCheckListCtrlMixin></center></h2> |
| 83 | |
| 84 | CheckListCtrlMixin is a simple mixin class that can add a checkbox to |
| 85 | the first column of a wx.ListCtrl. |
| 86 | |
| 87 | </body></html> |
| 88 | """ |
| 89 | |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | import sys,os |
| 94 | import run |
| 95 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 96 | |