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