]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListBox.py
replaced wxAppTraits::GetOSVersion with GetToolkitInfo with richer information that...
[wxWidgets.git] / wxPython / demo / wxListBox.py
1
2 from wxPython.wx import *
3
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 = ''
11 self.log = parent.log
12 EVT_KEY_DOWN(self, self.OnKey)
13
14
15 def FindPrefix(self, prefix):
16 self.log.WriteText('Looking for prefix: %s\n' % prefix)
17 if prefix:
18 prefix = prefix.lower()
19 length = len(prefix)
20 for x in range(self.Number()):
21 text = self.GetString(x)
22 text = text.lower()
23 if text[:length] == prefix:
24 self.log.WriteText('Prefix %s is found.\n' % prefix)
25 return x
26 self.log.WriteText('Prefix %s is not found.\n' % prefix)
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:
48 self.typedText = ''
49 evt.Skip()
50
51 def OnKeyDown(self, evt):
52 pass
53
54
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))
70 self.lb1 = wxListBox(self, 60, wxPoint(80, 50), wxSize(80, 120),
71 sampleList, wxLB_SINGLE)
72 EVT_LISTBOX(self, 60, self.EvtListBox)
73 EVT_LISTBOX_DCLICK(self, 60, self.EvtListBoxDClick)
74 EVT_RIGHT_UP(self.lb1, self.EvtRightButton)
75 self.lb1.SetSelection(3)
76 self.lb1.Append("with data", "This one has data");
77 self.lb1.SetClientData(2, "This one has data");
78
79
80 wxStaticText(self, -1, "Select many:", wxPoint(200, 50), wxSize(65, 18))
81 self.lb2 = wxListBox(self, 70, wxPoint(280, 50), wxSize(80, 120),
82 sampleList, wxLB_EXTENDED)
83 EVT_LISTBOX(self, 70, self.EvtMultiListBox)
84 EVT_RIGHT_UP(self.lb2, self.EvtRightButton)
85 self.lb2.SetSelection(0)
86
87
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
98 def EvtListBox(self, event):
99 self.log.WriteText('EvtListBox: %s, %s, %s\n' %
100 (event.GetString(), event.IsSelection(), event.GetSelection()))
101
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
107
108 def EvtListBoxDClick(self, event):
109 self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
110 self.lb1.Delete(self.lb1.GetSelection())
111
112 def EvtMultiListBox(self, event):
113 self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))
114
115 def EvtRightButton(self, event):
116 self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
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
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
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 """
147
148
149 if __name__ == '__main__':
150 import sys,os
151 import run
152 run.main(['', os.path.basename(sys.argv[0])])
153