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