]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/ListBox.py
updated to wx namespace by Peter Damoc
[wxWidgets.git] / wxPython / demo / ListBox.py
CommitLineData
cf694132 1
8fa876ca 2import wx
cf694132 3
185d7c3e
RD
4#---------------------------------------------------------------------------
5
8fa876ca
RD
6# This listbox subclass lets you type the starting letters of what you want to
7# select, and scrolls the list to the match if it is found.
8class FindPrefixListBox(wx.ListBox):
9 def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize,
10 choices=[], style=0, validator=wx.DefaultValidator):
11 wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator)
185d7c3e 12 self.typedText = ''
1e4a197e 13 self.log = parent.log
8fa876ca 14 self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
185d7c3e
RD
15
16
17 def FindPrefix(self, prefix):
1e4a197e 18 self.log.WriteText('Looking for prefix: %s\n' % prefix)
8fa876ca 19
185d7c3e 20 if prefix:
1e4a197e 21 prefix = prefix.lower()
185d7c3e 22 length = len(prefix)
8fa876ca
RD
23
24 # Changed in 2.5 because ListBox.Number() is no longer supported.
25 # ListBox.GetCount() is now the appropriate way to go.
26 for x in range(self.GetCount()):
185d7c3e 27 text = self.GetString(x)
1e4a197e 28 text = text.lower()
8fa876ca 29
185d7c3e 30 if text[:length] == prefix:
1e4a197e 31 self.log.WriteText('Prefix %s is found.\n' % prefix)
185d7c3e 32 return x
8fa876ca 33
1e4a197e 34 self.log.WriteText('Prefix %s is not found.\n' % prefix)
185d7c3e
RD
35 return -1
36
37
38 def OnKey(self, evt):
39 key = evt.GetKeyCode()
8fa876ca 40
185d7c3e
RD
41 if key >= 32 and key <= 127:
42 self.typedText = self.typedText + chr(key)
43 item = self.FindPrefix(self.typedText)
8fa876ca 44
185d7c3e
RD
45 if item != -1:
46 self.SetSelection(item)
47
8fa876ca 48 elif key == wx.WXK_BACK: # backspace removes one character and backs up
185d7c3e 49 self.typedText = self.typedText[:-1]
8fa876ca 50
185d7c3e
RD
51 if not self.typedText:
52 self.SetSelection(0)
53 else:
54 item = self.FindPrefix(self.typedText)
8fa876ca 55
185d7c3e
RD
56 if item != -1:
57 self.SetSelection(item)
185d7c3e 58 else:
1e4a197e 59 self.typedText = ''
185d7c3e
RD
60 evt.Skip()
61
1e4a197e
RD
62 def OnKeyDown(self, evt):
63 pass
64
185d7c3e 65
cf694132
RD
66#---------------------------------------------------------------------------
67
8fa876ca 68class TestListBox(wx.Panel):
cf694132
RD
69 def __init__(self, parent, log):
70 self.log = log
8fa876ca 71 wx.Panel.__init__(self, parent, -1)
cf694132
RD
72
73 sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
74 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
75 'twelve', 'thirteen', 'fourteen']
76
95bfd958 77 wx.StaticText(self, -1, "This example uses the wx.ListBox control.", (45, 10))
8fa876ca 78 wx.StaticText(self, -1, "Select one:", (15, 50), (65, 18))
5523df9f 79 self.lb1 = wx.ListBox(self, 60, (80, 50), (90, 120), sampleList, wx.LB_SINGLE)
8fa876ca
RD
80 self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.lb1)
81 self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.lb1)
82 self.lb1.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton)
1b62f00d 83 self.lb1.SetSelection(3)
900d9886
RD
84 self.lb1.Append("with data", "This one has data");
85 self.lb1.SetClientData(2, "This one has data");
cf694132
RD
86
87
8fa876ca 88 wx.StaticText(self, -1, "Select many:", (200, 50), (65, 18))
5523df9f 89 self.lb2 = wx.ListBox(self, 70, (300, 50), (90, 120), sampleList, wx.LB_EXTENDED)
8fa876ca
RD
90 self.Bind(wx.EVT_LISTBOX, self.EvtMultiListBox, self.lb2)
91 self.lb2.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton)
bb0054cd 92 self.lb2.SetSelection(0)
cf694132 93
185d7c3e
RD
94 sampleList = sampleList + ['test a', 'test aa', 'test aab',
95 'test ab', 'test abc', 'test abcc',
96 'test abcd' ]
97 sampleList.sort()
8fa876ca 98 wx.StaticText(self, -1, "Find Prefix:", (15, 250))
5523df9f 99 fp = FindPrefixListBox(self, -1, (80, 250), (90, 120), sampleList, wx.LB_SINGLE)
185d7c3e
RD
100 fp.SetSelection(0)
101
102
cf694132 103 def EvtListBox(self, event):
1e4a197e
RD
104 self.log.WriteText('EvtListBox: %s, %s, %s\n' %
105 (event.GetString(), event.IsSelection(), event.GetSelection()))
106
900d9886
RD
107 lb = event.GetEventObject()
108 data = lb.GetClientData(lb.GetSelection())
8fa876ca 109
900d9886
RD
110 if data is not None:
111 self.log.WriteText('\tdata: %s\n' % data)
112
cf694132
RD
113
114 def EvtListBoxDClick(self, event):
bb0054cd 115 self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
5d1bd859 116 self.lb1.Delete(self.lb1.GetSelection())
cf694132
RD
117
118 def EvtMultiListBox(self, event):
bb0054cd 119 self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))
cf694132 120
bb0054cd
RD
121 def EvtRightButton(self, event):
122 self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
8fa876ca 123
900d9886
RD
124 if event.GetEventObject().GetId() == 70:
125 selections = list(self.lb2.GetSelections())
126 selections.reverse()
8fa876ca 127
900d9886
RD
128 for index in selections:
129 self.lb2.Delete(index)
130
cf694132
RD
131
132#---------------------------------------------------------------------------
133
134def runTest(frame, nb, log):
135 win = TestListBox(nb, log)
136 return win
137
138#---------------------------------------------------------------------------
139
140
141
142
1e4a197e
RD
143overview = """<html><body>
144A listbox is used to select one or more of a list of
145strings. The strings are displayed in a scrolling box, with the
146selected string(s) marked in reverse video. A listbox can be single
147selection (if an item is selected, the previous selection is removed)
148or multiple selection (clicking an item toggles the item on or off
149independently of other selections).
150</body></html>
151"""
cf694132
RD
152
153
1e4a197e
RD
154if __name__ == '__main__':
155 import sys,os
156 import run
8eca4fef 157 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 158