1999-04-30 03:29:54 +00:00
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
import wx
|
1999-04-30 03:29:54 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
# This listbox subclass lets you type the starting letters of what you want to
|
|
|
|
# select, and scrolls the list to the match if it is found.
|
|
|
|
class FindPrefixListBox(wx.ListBox):
|
|
|
|
def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize,
|
|
|
|
choices=[], style=0, validator=wx.DefaultValidator):
|
|
|
|
wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator)
|
2000-11-21 03:44:14 +00:00
|
|
|
self.typedText = ''
|
2003-03-25 06:35:27 +00:00
|
|
|
self.log = parent.log
|
2003-12-09 01:23:28 +00:00
|
|
|
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
|
2000-11-21 03:44:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def FindPrefix(self, prefix):
|
2003-03-25 06:35:27 +00:00
|
|
|
self.log.WriteText('Looking for prefix: %s\n' % prefix)
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if prefix:
|
2003-03-25 06:35:27 +00:00
|
|
|
prefix = prefix.lower()
|
2000-11-21 03:44:14 +00:00
|
|
|
length = len(prefix)
|
2003-12-09 01:23:28 +00:00
|
|
|
|
|
|
|
# Changed in 2.5 because ListBox.Number() is no longer supported.
|
|
|
|
# ListBox.GetCount() is now the appropriate way to go.
|
|
|
|
for x in range(self.GetCount()):
|
2000-11-21 03:44:14 +00:00
|
|
|
text = self.GetString(x)
|
2003-03-25 06:35:27 +00:00
|
|
|
text = text.lower()
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if text[:length] == prefix:
|
2003-03-25 06:35:27 +00:00
|
|
|
self.log.WriteText('Prefix %s is found.\n' % prefix)
|
2000-11-21 03:44:14 +00:00
|
|
|
return x
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2003-03-25 06:35:27 +00:00
|
|
|
self.log.WriteText('Prefix %s is not found.\n' % prefix)
|
2000-11-21 03:44:14 +00:00
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
def OnKey(self, evt):
|
|
|
|
key = evt.GetKeyCode()
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if key >= 32 and key <= 127:
|
|
|
|
self.typedText = self.typedText + chr(key)
|
|
|
|
item = self.FindPrefix(self.typedText)
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if item != -1:
|
|
|
|
self.SetSelection(item)
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
elif key == wx.WXK_BACK: # backspace removes one character and backs up
|
2000-11-21 03:44:14 +00:00
|
|
|
self.typedText = self.typedText[:-1]
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if not self.typedText:
|
|
|
|
self.SetSelection(0)
|
|
|
|
else:
|
|
|
|
item = self.FindPrefix(self.typedText)
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
if item != -1:
|
|
|
|
self.SetSelection(item)
|
|
|
|
else:
|
2003-03-25 06:35:27 +00:00
|
|
|
self.typedText = ''
|
2000-11-21 03:44:14 +00:00
|
|
|
evt.Skip()
|
|
|
|
|
2003-03-25 06:35:27 +00:00
|
|
|
def OnKeyDown(self, evt):
|
|
|
|
pass
|
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-12-09 01:23:28 +00:00
|
|
|
class TestListBox(wx.Panel):
|
1999-04-30 03:29:54 +00:00
|
|
|
def __init__(self, parent, log):
|
|
|
|
self.log = log
|
2003-12-09 01:23:28 +00:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
|
|
|
|
'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
|
|
|
|
'twelve', 'thirteen', 'fourteen']
|
|
|
|
|
2004-01-13 03:17:17 +00:00
|
|
|
wx.StaticText(self, -1, "This example uses the wx.ListBox control.", (45, 10))
|
2006-01-06 07:05:15 +00:00
|
|
|
wx.StaticText(self, -1, "Select one:", (15, 50))
|
|
|
|
self.lb1 = wx.ListBox(self, 60, (100, 50), (90, 120), sampleList, wx.LB_SINGLE)
|
2003-12-09 01:23:28 +00:00
|
|
|
self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.lb1)
|
|
|
|
self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.lb1)
|
|
|
|
self.lb1.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton)
|
2001-02-16 08:19:50 +00:00
|
|
|
self.lb1.SetSelection(3)
|
2001-10-03 03:32:37 +00:00
|
|
|
self.lb1.Append("with data", "This one has data");
|
|
|
|
self.lb1.SetClientData(2, "This one has data");
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
|
2006-01-06 07:05:15 +00:00
|
|
|
wx.StaticText(self, -1, "Select many:", (220, 50))
|
|
|
|
self.lb2 = wx.ListBox(self, 70, (320, 50), (90, 120), sampleList, wx.LB_EXTENDED)
|
2003-12-09 01:23:28 +00:00
|
|
|
self.Bind(wx.EVT_LISTBOX, self.EvtMultiListBox, self.lb2)
|
|
|
|
self.lb2.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton)
|
1999-06-22 07:03:29 +00:00
|
|
|
self.lb2.SetSelection(0)
|
1999-04-30 03:29:54 +00:00
|
|
|
|
2000-11-21 03:44:14 +00:00
|
|
|
sampleList = sampleList + ['test a', 'test aa', 'test aab',
|
|
|
|
'test ab', 'test abc', 'test abcc',
|
|
|
|
'test abcd' ]
|
|
|
|
sampleList.sort()
|
2003-12-09 01:23:28 +00:00
|
|
|
wx.StaticText(self, -1, "Find Prefix:", (15, 250))
|
2006-01-06 07:05:15 +00:00
|
|
|
fp = FindPrefixListBox(self, -1, (100, 250), (90, 120), sampleList, wx.LB_SINGLE)
|
2000-11-21 03:44:14 +00:00
|
|
|
fp.SetSelection(0)
|
|
|
|
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
def EvtListBox(self, event):
|
2006-03-10 00:29:03 +00:00
|
|
|
self.log.WriteText('EvtListBox: %s, %s, %s, %s\n' %
|
|
|
|
(event.GetString(),
|
|
|
|
event.IsSelection(),
|
|
|
|
event.GetSelection(),
|
|
|
|
event.GetClientData()))
|
2003-03-25 06:35:27 +00:00
|
|
|
|
2001-10-03 03:32:37 +00:00
|
|
|
lb = event.GetEventObject()
|
|
|
|
data = lb.GetClientData(lb.GetSelection())
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2001-10-03 03:32:37 +00:00
|
|
|
if data is not None:
|
|
|
|
self.log.WriteText('\tdata: %s\n' % data)
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
def EvtListBoxDClick(self, event):
|
1999-06-22 07:03:29 +00:00
|
|
|
self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
|
1999-12-03 19:36:47 +00:00
|
|
|
self.lb1.Delete(self.lb1.GetSelection())
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
def EvtMultiListBox(self, event):
|
1999-06-22 07:03:29 +00:00
|
|
|
self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))
|
1999-04-30 03:29:54 +00:00
|
|
|
|
1999-06-22 07:03:29 +00:00
|
|
|
def EvtRightButton(self, event):
|
|
|
|
self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2001-10-03 03:32:37 +00:00
|
|
|
if event.GetEventObject().GetId() == 70:
|
|
|
|
selections = list(self.lb2.GetSelections())
|
|
|
|
selections.reverse()
|
2003-12-09 01:23:28 +00:00
|
|
|
|
2001-10-03 03:32:37 +00:00
|
|
|
for index in selections:
|
|
|
|
self.lb2.Delete(index)
|
|
|
|
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
|
|
|
win = TestListBox(nb, log)
|
|
|
|
return win
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-25 06:35:27 +00:00
|
|
|
overview = """<html><body>
|
|
|
|
A listbox is used to select one or more of a list of
|
|
|
|
strings. The strings are displayed in a scrolling box, with the
|
|
|
|
selected string(s) marked in reverse video. A listbox can be single
|
|
|
|
selection (if an item is selected, the previous selection is removed)
|
|
|
|
or multiple selection (clicking an item toggles the item on or off
|
|
|
|
independently of other selections).
|
|
|
|
</body></html>
|
|
|
|
"""
|
1999-04-30 03:29:54 +00:00
|
|
|
|
|
|
|
|
2003-03-25 06:35:27 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-05 00:06:33 +00:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
1999-04-30 03:29:54 +00:00
|
|
|
|