Commit a58a0653 authored by eric pellegrini's avatar eric pellegrini

Fix bug when checking items on checkable combobox on Windows

parent eafedbe8
import wx.combo import wx.combo
class ComboCheckbox(wx.combo.ComboPopup): class ComboCheckbox(wx.CheckListBox,wx.combo.ComboPopup):
def __init__(self, items, maxNumberOfItems=None): def __init__(self, items, maxNumberOfItems=None):
wx.combo.ComboPopup.__init__(self) wx.combo.ComboPopup.__init__(self)
self._items = items self._items = items
self._maxNumberOfItems = maxNumberOfItems self._maxNumberOfItems = maxNumberOfItems
self._currentItem = -1
@property @property
def items(self): def items(self):
return self._items return self._items
...@@ -15,31 +19,48 @@ class ComboCheckbox(wx.combo.ComboPopup): ...@@ -15,31 +19,48 @@ class ComboCheckbox(wx.combo.ComboPopup):
@property @property
def checklistbox(self): def checklistbox(self):
return self._checklistbox return self
def OnMotion(self, event):
item = self.HitTest(event.GetPosition())
if item >= 0:
self._currentItem = item
def Create(self, parent): def Create(self, parent):
self._checklistbox = wx.CheckListBox(parent, -1, choices=self._items)
self._checklistbox.Bind(wx.EVT_CHECKLISTBOX, self.on_check_item) wx.CheckListBox.Create(self,parent, -1, choices=self._items)
if not self._checklistbox.IsEmpty(): self.Bind(wx.EVT_MOTION, self.OnMotion)
self._checklistbox.Check(0) self.Bind(wx.EVT_LEFT_DOWN, self.on_check_item)
if not self.IsEmpty():
return True return True
def GetControl(self): def GetControl(self):
return self._checklistbox return self
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight): def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
return self._checklistbox.GetSize() return self.GetSize()
def GetStringValue(self): def GetStringValue(self):
return self._checklistbox.GetCheckedStrings() return self.GetCheckedStrings()
def on_check_item(self, event): def on_check_item(self, event):
if self._maxNumberOfItems is None: # Control only if ele;ent is checked
return if not self.IsChecked(self._currentItem):
nCheckedItems = len(self._checklistbox.GetChecked()) # Control max number of items
if self._maxNumberOfItems is None:
if nCheckedItems > self._maxNumberOfItems: # Accept the event
self._checklistbox.Check(event.GetInt(), False) self.Check(self._currentItem, True)
\ No newline at end of file else:
# Control the number of checked items
nCheckedItems = len(self.GetChecked())
if nCheckedItems < self._maxNumberOfItems:
# Chech the item
self.Check(self._currentItem, True)
self.Check(self._currentItem, False)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment