2 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
6 # The wxVListBox is much like a regular wxListBox except you draw the
7 # items yourself and the items can vary in height.
8 class MyVListBox(wxVListBox
):
10 # This method must be overridden. When called it should draw the
11 # n'th item on the dc within the rect. How it is drawn, and what
12 # is drawn is entirely up to you.
13 def OnDrawItem(self
, dc
, rect
, n
):
14 dc
.DrawLabel(self
._getItemText
(n
), rect
,
15 wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL
)
17 # This method must be overridden. It should return the height
18 # required to draw the n'th item.
19 def OnMeasureItem(self
, n
):
21 for line
in self
._getItemText
(n
).split('\n'):
22 w
, h
= self
.GetTextExtent(line
)
27 # These are also overridable:
29 # OnDrawSeparator(dc, rect, n)
30 # Draw a separator between items. Note that rect may be reduced
31 # in size if desired so OnDrawItem gets a smaller rect.
33 # OnDrawBackground(dc, rect, n)
34 # Draw the background and maybe a border if desired.
37 def _getItemText(self
, item
):
39 return "This is item# %d" % item
41 return "This is item# %d\n with an extra line" % item
43 #----------------------------------------------------------------------
45 # The wxHtmlListBox derives from wxVListBox, but draws each item
46 # itself as a wxHtmlCell.
47 class MyHtmlListBox(wxHtmlListBox
):
49 def OnGetItem(self
, n
):
51 return "This is item# <b>%d</b>" % n
53 return "This is item# <b>%d</b> <br>Any <font color='RED'>HTML</font> is okay." % n
55 #----------------------------------------------------------------------
57 class TestPanel(wxPanel
):
58 def __init__(self
, parent
, log
):
60 wxPanel
.__init
__(self
, parent
, -1)
63 vlb
= MyVListBox(self
, -1, size
=(150, 250), style
=wxBORDER_SUNKEN
)
67 vlbSizer
= wxBoxSizer(wxVERTICAL
)
68 vlbSizer
.Add((spacer
, spacer
))
69 vlbSizer
.Add(wxStaticText(self
, -1, "wxVListBox"), 0, 5, wxALL
)
72 hlb
= MyHtmlListBox(self
, -1, size
=(150, 250), style
=wxBORDER_SUNKEN
)
75 hlbSizer
= wxBoxSizer(wxVERTICAL
)
76 hlbSizer
.Add((spacer
, spacer
))
77 hlbSizer
.Add(wxStaticText(self
, -1, "wxHtmlListBox"), 0, 5, wxALL
)
80 sizer
= wxBoxSizer(wxHORIZONTAL
)
81 sizer
.Add((spacer
, spacer
))
83 sizer
.Add((spacer
, spacer
))
84 sizer
.Add((spacer
, spacer
))
90 #----------------------------------------------------------------------
92 def runTest(frame
, nb
, log
):
93 win
= TestPanel(nb
, log
)
96 #----------------------------------------------------------------------
100 overview
= """<html><body>
101 <h2><center>wxVListBox and wxHtmlListBox</center></h2>
104 The "V" in wxVListBox stands for both "virtual" because it can have an
105 unlimited number of items since it doesn't store them itself, and
106 "variable" since items can vary in height. It has much the same
107 interface as wxListBox and also emits the same events so you can use
108 the same EVT_LISTBOX function to connect a handler.
111 The wxHtmlListBox derives from wxVListBox, but draws each item itself
112 as a wxHtmlCell. This means that you just need to provide a snippet
113 of HTML for each item when requested.
120 if __name__
== '__main__':
123 run
.main(['', os
.path
.basename(sys
.argv
[0])])