X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..61940a7061d0b5d5122b1e1e939cfa2d72596999:/wxPython/demo/wxListBox.py diff --git a/wxPython/demo/wxListBox.py b/wxPython/demo/wxListBox.py index 3cba2b0fa2..c3a69d6919 100644 --- a/wxPython/demo/wxListBox.py +++ b/wxPython/demo/wxListBox.py @@ -1,103 +1,162 @@ +# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) +# +# o Updated for wx namespace +# -from wxPython.wx import * +import wx #--------------------------------------------------------------------------- -class TestListBox(wxPanel): - def __init__(self, parent, log): - self.log = log - wxPanel.__init__(self, parent, -1) +# This listbox subclass lets you type the starting letters of what you want to +# select, and scrolls the list to the match if it is found. +class FindPrefixListBox(wx.ListBox): + def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize, + choices=[], style=0, validator=wx.DefaultValidator): + wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator) + self.typedText = '' + self.log = parent.log + self.Bind(wx.EVT_KEY_DOWN, self.OnKey) - 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)) + def FindPrefix(self, prefix): + self.log.WriteText('Looking for prefix: %s\n' % prefix) - 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(0) + if prefix: + prefix = prefix.lower() + length = len(prefix) + # Changed in 2.5 because ListBox.Number() is no longer supported. + # ListBox.GetCount() is now the appropriate way to go. + for x in range(self.GetCount()): + text = self.GetString(x) + text = text.lower() - 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_LISTBOX_DCLICK(self, 70, self.EvtListBoxDClick) - self.lb2.SetSelection(0) + 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 EvtListBox(self, event): - self.log.WriteText('EvtListBox: %s\n' % event.GetString()) - def EvtListBoxDClick(self, event): - self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection()) - self.lb1.Delete(self.lb1.GetSelection()) + def OnKey(self, evt): + key = evt.GetKeyCode() - def EvtMultiListBox(self, event): - self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections())) + if key >= 32 and key <= 127: + self.typedText = self.typedText + chr(key) + item = self.FindPrefix(self.typedText) - def EvtRightButton(self, event): - self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition()) - -#--------------------------------------------------------------------------- + if item != -1: + self.SetSelection(item) -def runTest(frame, nb, log): - win = TestListBox(nb, log) - return win + elif key == wx.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(wx.Panel): + def __init__(self, parent, log): + self.log = log + wx.Panel.__init__(self, parent, -1) + sampleList = ['zero', 'one', 'two', 'three', 'four', 'five', + 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', + 'twelve', 'thirteen', 'fourteen'] + wx.StaticText(self, -1, "This example uses the wxListBox control.", (45, 10)) + wx.StaticText(self, -1, "Select one:", (15, 50), (65, 18)) + self.lb1 = wx.ListBox(self, 60, (80, 50), (80, 120), sampleList, wx.LB_SINGLE) + self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.lb1) + self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.lb1) + self.lb1.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton) + self.lb1.SetSelection(3) + self.lb1.Append("with data", "This one has data"); + self.lb1.SetClientData(2, "This one has data"); + + + wx.StaticText(self, -1, "Select many:", (200, 50), (65, 18)) + self.lb2 = wx.ListBox(self, 70, (280, 50), (80, 120), sampleList, wx.LB_EXTENDED) + self.Bind(wx.EVT_LISTBOX, self.EvtMultiListBox, self.lb2) + self.lb2.Bind(wx.EVT_RIGHT_UP, self.EvtRightButton) + self.lb2.SetSelection(0) + sampleList = sampleList + ['test a', 'test aa', 'test aab', + 'test ab', 'test abc', 'test abcc', + 'test abcd' ] + sampleList.sort() + wx.StaticText(self, -1, "Find Prefix:", (15, 250)) + fp = FindPrefixListBox(self, -1, (80, 250), (80, 120), sampleList, wx.LB_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) -overview = """\ -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). + def EvtListBoxDClick(self, event): + self.log.WriteText('EvtListBoxDClick: %s\n' % self.lb1.GetSelection()) + self.lb1.Delete(self.lb1.GetSelection()) -List box elements are numbered from zero. + def EvtMultiListBox(self, event): + self.log.WriteText('EvtMultiListBox: %s\n' % str(self.lb2.GetSelections())) -wxListBox() ---------------------- + def EvtRightButton(self, event): + self.log.WriteText('EvtRightButton: %s\n' % event.GetPosition()) -Default constructor. + if event.GetEventObject().GetId() == 70: + selections = list(self.lb2.GetSelections()) + selections.reverse() -wxListBox(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, int n, const wxString choices[] = NULL, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listBox") + for index in selections: + self.lb2.Delete(index) -Constructor, creating and showing a list box. -Parameters -------------------- +#--------------------------------------------------------------------------- -parent = Parent window. Must not be NULL. +def runTest(frame, nb, log): + win = TestListBox(nb, log) + return win -id = Window identifier. A value of -1 indicates a default value. +#--------------------------------------------------------------------------- -pos = Window position. -size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately. -n = Number of strings with which to initialise the control. -choices = An array of strings with which to initialise the control. +overview = """ +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). + +""" -style = Window style. See wxListBox. -validator = Window validator. +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])]) -name = Window name. -"""