]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
2 | # | |
3 | # o Updated for wx namespace | |
4 | # | |
cf694132 | 5 | |
8fa876ca | 6 | import wx |
cf694132 | 7 | |
185d7c3e RD |
8 | #--------------------------------------------------------------------------- |
9 | ||
8fa876ca RD |
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) | |
185d7c3e | 16 | self.typedText = '' |
1e4a197e | 17 | self.log = parent.log |
8fa876ca | 18 | self.Bind(wx.EVT_KEY_DOWN, self.OnKey) |
185d7c3e RD |
19 | |
20 | ||
21 | def FindPrefix(self, prefix): | |
1e4a197e | 22 | self.log.WriteText('Looking for prefix: %s\n' % prefix) |
8fa876ca | 23 | |
185d7c3e | 24 | if prefix: |
1e4a197e | 25 | prefix = prefix.lower() |
185d7c3e | 26 | length = len(prefix) |
8fa876ca RD |
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()): | |
185d7c3e | 31 | text = self.GetString(x) |
1e4a197e | 32 | text = text.lower() |
8fa876ca | 33 | |
185d7c3e | 34 | if text[:length] == prefix: |
1e4a197e | 35 | self.log.WriteText('Prefix %s is found.\n' % prefix) |
185d7c3e | 36 | return x |
8fa876ca | 37 | |
1e4a197e | 38 | self.log.WriteText('Prefix %s is not found.\n' % prefix) |
185d7c3e RD |
39 | return -1 |
40 | ||
41 | ||
42 | def OnKey(self, evt): | |
43 | key = evt.GetKeyCode() | |
8fa876ca | 44 | |
185d7c3e RD |
45 | if key >= 32 and key <= 127: |
46 | self.typedText = self.typedText + chr(key) | |
47 | item = self.FindPrefix(self.typedText) | |
8fa876ca | 48 | |
185d7c3e RD |
49 | if item != -1: |
50 | self.SetSelection(item) | |
51 | ||
8fa876ca | 52 | elif key == wx.WXK_BACK: # backspace removes one character and backs up |
185d7c3e | 53 | self.typedText = self.typedText[:-1] |
8fa876ca | 54 | |
185d7c3e RD |
55 | if not self.typedText: |
56 | self.SetSelection(0) | |
57 | else: | |
58 | item = self.FindPrefix(self.typedText) | |
8fa876ca | 59 | |
185d7c3e RD |
60 | if item != -1: |
61 | self.SetSelection(item) | |
185d7c3e | 62 | else: |
1e4a197e | 63 | self.typedText = '' |
185d7c3e RD |
64 | evt.Skip() |
65 | ||
1e4a197e RD |
66 | def OnKeyDown(self, evt): |
67 | pass | |
68 | ||
185d7c3e | 69 | |
cf694132 RD |
70 | #--------------------------------------------------------------------------- |
71 | ||
8fa876ca | 72 | class TestListBox(wx.Panel): |
cf694132 RD |
73 | def __init__(self, parent, log): |
74 | self.log = log | |
8fa876ca | 75 | wx.Panel.__init__(self, parent, -1) |
cf694132 RD |
76 | |
77 | sampleList = ['zero', 'one', 'two', 'three', 'four', 'five', | |
78 | 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', | |
79 | 'twelve', 'thirteen', 'fourteen'] | |
80 | ||
8fa876ca RD |
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) | |
1b62f00d | 87 | self.lb1.SetSelection(3) |
900d9886 RD |
88 | self.lb1.Append("with data", "This one has data"); |
89 | self.lb1.SetClientData(2, "This one has data"); | |
cf694132 RD |
90 | |
91 | ||
8fa876ca RD |
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) | |
bb0054cd | 96 | self.lb2.SetSelection(0) |
cf694132 | 97 | |
185d7c3e RD |
98 | sampleList = sampleList + ['test a', 'test aa', 'test aab', |
99 | 'test ab', 'test abc', 'test abcc', | |
100 | 'test abcd' ] | |
101 | sampleList.sort() | |
8fa876ca RD |
102 | wx.StaticText(self, -1, "Find Prefix:", (15, 250)) |
103 | fp = FindPrefixListBox(self, -1, (80, 250), (80, 120), sampleList, wx.LB_SINGLE) | |
185d7c3e RD |
104 | fp.SetSelection(0) |
105 | ||
106 | ||
cf694132 | 107 | def EvtListBox(self, event): |
1e4a197e RD |
108 | self.log.WriteText('EvtListBox: %s, %s, %s\n' % |
109 | (event.GetString(), event.IsSelection(), event.GetSelection())) | |
110 | ||
900d9886 RD |
111 | lb = event.GetEventObject() |
112 | data = lb.GetClientData(lb.GetSelection()) | |
8fa876ca | 113 | |
900d9886 RD |
114 | if data is not None: |
115 | self.log.WriteText('\tdata: %s\n' % data) | |
116 | ||
cf694132 RD |
117 | |
118 | def EvtListBoxDClick(self, event): | |
bb0054cd | 119 | self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection()) |
5d1bd859 | 120 | self.lb1.Delete(self.lb1.GetSelection()) |
cf694132 RD |
121 | |
122 | def EvtMultiListBox(self, event): | |
bb0054cd | 123 | self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections())) |
cf694132 | 124 | |
bb0054cd RD |
125 | def EvtRightButton(self, event): |
126 | self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition()) | |
8fa876ca | 127 | |
900d9886 RD |
128 | if event.GetEventObject().GetId() == 70: |
129 | selections = list(self.lb2.GetSelections()) | |
130 | selections.reverse() | |
8fa876ca | 131 | |
900d9886 RD |
132 | for index in selections: |
133 | self.lb2.Delete(index) | |
134 | ||
cf694132 RD |
135 | |
136 | #--------------------------------------------------------------------------- | |
137 | ||
138 | def runTest(frame, nb, log): | |
139 | win = TestListBox(nb, log) | |
140 | return win | |
141 | ||
142 | #--------------------------------------------------------------------------- | |
143 | ||
144 | ||
145 | ||
146 | ||
1e4a197e RD |
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 | """ | |
cf694132 RD |
156 | |
157 | ||
1e4a197e RD |
158 | if __name__ == '__main__': |
159 | import sys,os | |
160 | import run | |
161 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 162 |