-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathComboCtrlPopup.py
76 lines (57 loc) · 2.3 KB
/
ComboCtrlPopup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# All works in this code have been curated by ECCC and licensed under the GNU General Public License v3.0.
# Read more: https://www.gnu.org/licenses/gpl-3.0.en.html
import wx.combo as cb
import wx
##This class is taken from wxPython demo
##https://github.com/wxWidgets/wxPython/blob/master/demo/ComboCtrl.py
class ComboCtrlPopup(wx.ListCtrl, cb.ComboPopup):
def __init__(self):
self.PostCreate(wx.PreListCtrl())
cb.ComboPopup.__init__(self)
def AddItem(self, txt):
self.InsertStringItem(self.GetItemCount(), txt)
def AddItems(self, lis):
for txt in lis:
self.AddItem(txt)
def OnMotion(self, evt):
item, flags = self.HitTest(evt.GetPosition())
if item >= 0:
self.Select(item)
self.curitem = item
def OnLeftDown(self, evt):
self.value = self.curitem
self.Dismiss()
# The following methods are those that are overridable from the
# ComboPopup base class. Most of them are not required, but all
# are shown here for demonstration purposes.
# This is called immediately after construction finishes. You can
# use self.GetCombo if needed to get to the ComboCtrl instance.
def Init(self):
self.value = -1
self.curitem = -1
# Create the popup child control. Return true for success.
def Create(self, parent):
wx.ListCtrl.Create(self, parent,
style=wx.LC_LIST|wx.LC_SINGLE_SEL|wx.SIMPLE_BORDER)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
return True
# Return the widget that is to be used for the popup
def GetControl(self):
#self.log.write("ListCtrlComboPopup.GetControl")
return self
# Called just prior to displaying the popup, you can use it to
# 'select' the current item.
def SetStringValue(self, val):
idx = self.FindItem(-1, val)
if idx != wx.NOT_FOUND:
self.Select(idx)
# Return a string representation of the current item.
def GetStringValue(self):
if self.value >= 0:
return self.GetItemText(self.value)
return ""
# Called immediately after the popup is shown
def OnPopup(self):
cb.ComboPopup.OnPopup(self)
self.SetFocus()