ComboCheckbox.py 1.94 KB
Newer Older
1 2
import wx.combo

3
class ComboCheckbox(wx.CheckListBox,wx.combo.ComboPopup):
4 5
    
    def __init__(self, items, maxNumberOfItems=None):
6 7
                
        self.PostCreate(wx.PreCheckListBox())
8 9
        
        wx.combo.ComboPopup.__init__(self)
10
        
11 12
        self._items = items
        self._maxNumberOfItems = maxNumberOfItems
13 14
        self._currentItem = -1
                        
15 16 17 18 19 20 21
    @property
    def items(self):
        return self._items

    @property
    def checklistbox(self):
        
22 23 24 25 26 27 28 29
        return self

    def OnMotion(self, event):
        
        item  = self.HitTest(event.GetPosition())
        if item >= 0:
            self.Select(item)
            self._currentItem = item
30
        
31 32 33 34 35 36 37
    def Create(self, parent): 

        wx.CheckListBox.Create(self,parent, -1, choices=self._items)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_LEFT_DOWN, self.on_check_item)
        if not self.IsEmpty():
            self.Check(0)
38 39 40 41
                
        return True
    
    def GetControl(self):
42
        return self
43 44
        
    def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
45
        return self.GetSize()
46 47
    
    def GetStringValue(self): 
48
        return self.GetCheckedStrings()
49 50
    
    def on_check_item(self, event):
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            
        # Control only if ele;ent is checked
        if not self.IsChecked(self._currentItem):

            # Control max number of items
            if self._maxNumberOfItems is None:
                # Accept the event
                self.Check(self._currentItem, True)        
            else:
                # Control the number of checked items
                nCheckedItems = len(self.GetChecked())
                if nCheckedItems < self._maxNumberOfItems:
                    # Chech the item
                    self.Check(self._currentItem, True)
        else:
            self.Check(self._currentItem, False)