]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListBox.py
warning fix
[wxWidgets.git] / wxPython / demo / wxListBox.py
1
2 from wxPython.wx import *
3
4 import string
5
6 #---------------------------------------------------------------------------
7
8 class wxFindPrefixListBox(wxListBox):
9 def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize,
10 choices=[], style=0, validator=wxDefaultValidator):
11 wxListBox.__init__(self, parent, id, pos, size, choices, style, validator)
12 self.typedText = ''
13 EVT_KEY_UP(self, self.OnKey)
14
15
16 def FindPrefix(self, prefix):
17 if prefix:
18 prefix = string.lower(prefix)
19 length = len(prefix)
20 for x in range(self.Number()):
21 text = self.GetString(x)
22 text = string.lower(text)
23 if text[:length] == prefix:
24 return x
25 return -1
26
27
28 def OnKey(self, evt):
29 key = evt.GetKeyCode()
30 if key >= 32 and key <= 127:
31 self.typedText = self.typedText + chr(key)
32 item = self.FindPrefix(self.typedText)
33 if item != -1:
34 self.SetSelection(item)
35
36 elif key == WXK_BACK: # backspace removes one character and backs up
37 self.typedText = self.typedText[:-1]
38 if not self.typedText:
39 self.SetSelection(0)
40 else:
41 item = self.FindPrefix(self.typedText)
42 if item != -1:
43 self.SetSelection(item)
44
45 else:
46 evt.Skip()
47
48
49 #---------------------------------------------------------------------------
50
51 class TestListBox(wxPanel):
52 def __init__(self, parent, log):
53 self.log = log
54 wxPanel.__init__(self, parent, -1)
55
56 sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
57 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
58 'twelve', 'thirteen', 'fourteen']
59
60 wxStaticText(self, -1, "This example uses the wxListBox control.",
61 wxPoint(45, 10))
62
63 wxStaticText(self, -1, "Select one:", wxPoint(15, 50), wxSize(65, 18))
64 self.lb1 = wxListBox(self, 60, wxPoint(80, 50), wxSize(80, 120),
65 sampleList, wxLB_SINGLE)
66 EVT_LISTBOX(self, 60, self.EvtListBox)
67 EVT_LISTBOX_DCLICK(self, 60, self.EvtListBoxDClick)
68 EVT_RIGHT_UP(self.lb1, self.EvtRightButton)
69 self.lb1.SetSelection(3)
70
71
72 wxStaticText(self, -1, "Select many:", wxPoint(200, 50), wxSize(65, 18))
73 self.lb2 = wxListBox(self, 70, wxPoint(280, 50), wxSize(80, 120),
74 sampleList, wxLB_EXTENDED)
75 EVT_LISTBOX(self, 70, self.EvtMultiListBox)
76 EVT_LISTBOX_DCLICK(self, 70, self.EvtListBoxDClick)
77 self.lb2.SetSelection(0)
78
79
80 sampleList = sampleList + ['test a', 'test aa', 'test aab',
81 'test ab', 'test abc', 'test abcc',
82 'test abcd' ]
83 sampleList.sort()
84 wxStaticText(self, -1, "Find Prefix:", wxPoint(15, 250))
85 fp = wxFindPrefixListBox(self, -1, wxPoint(80, 250), wxSize(80, 120),
86 sampleList, wxLB_SINGLE)
87 fp.SetSelection(0)
88
89
90 def EvtListBox(self, event):
91 self.log.WriteText('EvtListBox: %s\n' % event.GetString())
92
93 def EvtListBoxDClick(self, event):
94 self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
95 self.lb1.Delete(self.lb1.GetSelection())
96
97 def EvtMultiListBox(self, event):
98 self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))
99
100 def EvtRightButton(self, event):
101 self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
102
103 #---------------------------------------------------------------------------
104
105 def runTest(frame, nb, log):
106 win = TestListBox(nb, log)
107 return win
108
109 #---------------------------------------------------------------------------
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 overview = """\
125 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).
126
127 """