File: wxListBox.py

package info (click to toggle)
wxwindows2.4 2.4.5.1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 59,920 kB
  • ctags: 97,489
  • sloc: cpp: 610,307; ansic: 111,957; python: 103,357; makefile: 3,676; sh: 3,391; lex: 192; yacc: 128; xml: 95; pascal: 74
file content (153 lines) | stat: -rw-r--r-- 5,296 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

from wxPython.wx import *

#---------------------------------------------------------------------------

class wxFindPrefixListBox(wxListBox):
    def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize,
                 choices=[], style=0, validator=wxDefaultValidator):
        wxListBox.__init__(self, parent, id, pos, size, choices, style, validator)
        self.typedText = ''
        self.log = parent.log
        EVT_KEY_DOWN(self, self.OnKey)


    def FindPrefix(self, prefix):
        self.log.WriteText('Looking for prefix: %s\n' % prefix)
        if prefix:
            prefix = prefix.lower()
            length = len(prefix)
            for x in range(self.Number()):
                text = self.GetString(x)
                text = text.lower()
                if text[:length] == prefix:
                    self.log.WriteText('Prefix %s is found.\n' % prefix)
                    return x
        self.log.WriteText('Prefix %s is not found.\n' % prefix)
        return -1


    def OnKey(self, evt):
        key = evt.GetKeyCode()
        if key >= 32 and key <= 127:
            self.typedText = self.typedText + chr(key)
            item = self.FindPrefix(self.typedText)
            if item != -1:
                self.SetSelection(item)

        elif key == WXK_BACK:   # backspace removes one character and backs up
            self.typedText = self.typedText[:-1]
            if not self.typedText:
                self.SetSelection(0)
            else:
                item = self.FindPrefix(self.typedText)
                if item != -1:
                    self.SetSelection(item)

        else:
            self.typedText = ''
            evt.Skip()

    def OnKeyDown(self, evt):
        pass


#---------------------------------------------------------------------------

class TestListBox(wxPanel):
    def __init__(self, parent, log):
        self.log = log
        wxPanel.__init__(self, parent, -1)

        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                      'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
                      'twelve', 'thirteen', 'fourteen']

        wxStaticText(self, -1, "This example uses the wxListBox control.",
                               wxPoint(45, 10))

        wxStaticText(self, -1, "Select one:", wxPoint(15, 50), wxSize(65, 18))
        self.lb1 = wxListBox(self, 60, wxPoint(80, 50), wxSize(80, 120),
                       sampleList, wxLB_SINGLE)
        EVT_LISTBOX(self, 60, self.EvtListBox)
        EVT_LISTBOX_DCLICK(self, 60, self.EvtListBoxDClick)
        EVT_RIGHT_UP(self.lb1, self.EvtRightButton)
        self.lb1.SetSelection(3)
        self.lb1.Append("with data", "This one has data");
        self.lb1.SetClientData(2, "This one has data");


        wxStaticText(self, -1, "Select many:", wxPoint(200, 50), wxSize(65, 18))
        self.lb2 = wxListBox(self, 70, wxPoint(280, 50), wxSize(80, 120),
                       sampleList, wxLB_EXTENDED)
        EVT_LISTBOX(self, 70, self.EvtMultiListBox)
        EVT_RIGHT_UP(self.lb2, self.EvtRightButton)
        self.lb2.SetSelection(0)


        sampleList = sampleList + ['test a', 'test aa', 'test aab',
                                   'test ab', 'test abc', 'test abcc',
                                   'test abcd' ]
        sampleList.sort()
        wxStaticText(self, -1, "Find Prefix:", wxPoint(15, 250))
        fp = wxFindPrefixListBox(self, -1, wxPoint(80, 250), wxSize(80, 120),
                                 sampleList, wxLB_SINGLE)
        fp.SetSelection(0)


    def EvtListBox(self, event):
        self.log.WriteText('EvtListBox: %s, %s, %s\n' %
                           (event.GetString(), event.IsSelection(), event.GetSelection()))

        lb = event.GetEventObject()
        data = lb.GetClientData(lb.GetSelection())
        if data is not None:
            self.log.WriteText('\tdata: %s\n' % data)


    def EvtListBoxDClick(self, event):
        self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection())
        self.lb1.Delete(self.lb1.GetSelection())

    def EvtMultiListBox(self, event):
        self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections()))

    def EvtRightButton(self, event):
        self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition())
        if event.GetEventObject().GetId() == 70:
            selections = list(self.lb2.GetSelections())
            selections.reverse()
            for index in selections:
                self.lb2.Delete(index)


#---------------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestListBox(nb, log)
    return win

#---------------------------------------------------------------------------







overview = """<html><body>
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).
</body></html>
"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])])