]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/CheckListCtrlMixin.py
   3 from wx
.lib
.mixins
.listctrl 
import CheckListCtrlMixin
 
   5 from ListCtrl 
import musicdata
 
   7 #---------------------------------------------------------------------- 
   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
) 
  14         self
.Bind(wx
.EVT_LIST_ITEM_ACTIVATED
, self
.OnItemActivated
) 
  17     def OnItemActivated(self
, evt
): 
  18         self
.ToggleItem(evt
.m_itemIndex
) 
  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] 
  29         self
.log
.write('item "%s", at index %d was %s\n' % (title
, index
, what
)) 
  33 class TestPanel(wx
.Panel
): 
  34     def __init__(self
, parent
, log
): 
  36         wx
.Panel
.__init
__(self
, parent
, -1) 
  38         self
.list = CheckListCtrl(self
, log
) 
  40         sizer
.Add(self
.list, 1, wx
.EXPAND
) 
  43         self
.list.InsertColumn(0, "Artist") 
  44         self
.list.InsertColumn(1, "Title", wx
.LIST_FORMAT_RIGHT
) 
  45         self
.list.InsertColumn(2, "Genre") 
  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
) 
  53         self
.list.SetColumnWidth(0, wx
.LIST_AUTOSIZE
) 
  54         self
.list.SetColumnWidth(1, wx
.LIST_AUTOSIZE
) 
  55         self
.list.SetColumnWidth(2, 100) 
  57         self
.list.CheckItem(4) 
  58         self
.list.CheckItem(7) 
  60         self
.Bind(wx
.EVT_LIST_ITEM_SELECTED
, self
.OnItemSelected
, self
.list) 
  61         self
.Bind(wx
.EVT_LIST_ITEM_DESELECTED
, self
.OnItemDeselected
, self
.list) 
  64     def OnItemSelected(self
, evt
): 
  65         self
.log
.write('item selected: %s\n' % evt
.m_itemIndex
) 
  67     def OnItemDeselected(self
, evt
): 
  68         self
.log
.write('item deselected: %s\n' % evt
.m_itemIndex
) 
  71 #---------------------------------------------------------------------- 
  73 def runTest(frame
, nb
, log
): 
  74     win 
= TestPanel(nb
, log
) 
  77 #---------------------------------------------------------------------- 
  81 overview 
= """<html><body> 
  82 <h2><centerCheckListCtrlMixin></center></h2> 
  84 CheckListCtrlMixin is a simple mixin class that can add a checkbox to 
  85 the first column of a wx.ListCtrl. 
  92 if __name__ 
== '__main__': 
  95     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])