]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListBox.py
Refactored Python shadows to use wxControlWithItems where appropriate.
[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 self.lb1.Append("with data", "This one has data");
71 self.lb1.SetClientData(2, "This one has data");
72
73
74 wxStaticText(self, -1, "Select many:", wxPoint(200, 50), wxSize(65, 18))
75 self.lb2 = wxListBox(self, 70, wxPoint(280, 50), wxSize(80, 120),
76 sampleList, wxLB_EXTENDED)
77 EVT_LISTBOX(self, 70, self.EvtMultiListBox)
78 EVT_RIGHT_UP(self.lb2, self.EvtRightButton)
79 self.lb2.SetSelection(0)
80
81
82 sampleList = sampleList + ['test a', 'test aa', 'test aab',
83 'test ab', 'test abc', 'test abcc',
84 'test abcd' ]
85 sampleList.sort()
86 wxStaticText(self, -1, "Find Prefix:", wxPoint(15, 250))
87 fp = wxFindPrefixListBox(self, -1, wxPoint(80, 250), wxSize(80, 120),
88 sampleList, wxLB_SINGLE)
89 fp.SetSelection(0)
90
91
92 def EvtListBox(self, event):
93 self.log.WriteText('EvtListBox: %s\n' % event.GetString())
94 lb = event.GetEventObject()
95 data = lb.GetClientData(lb.GetSelection())
96 if data is not None:
97 self.log.WriteText('\tdata: %s\n' % data)
98
99
100 def EvtListBoxDClick(self, event):
101 self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
102 self.lb1.Delete(self.lb1.GetSelection())
103
104 def EvtMultiListBox(self, event):
105 self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))
106
107 def EvtRightButton(self, event):
108 self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
109 if event.GetEventObject().GetId() == 70:
110 selections = list(self.lb2.GetSelections())
111 selections.reverse()
112 for index in selections:
113 self.lb2.Delete(index)
114
115
116 #---------------------------------------------------------------------------
117
118 def runTest(frame, nb, log):
119 win = TestListBox(nb, log)
120 return win
121
122 #---------------------------------------------------------------------------
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 overview = """\
138 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).
139
140 """