]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/VListBox.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
8 #----------------------------------------------------------------------
10 # The wxVListBox is much like a regular wxListBox except you draw the
11 # items yourself and the items can vary in height.
12 class MyVListBox(wx
.VListBox
):
14 # This method must be overridden. When called it should draw the
15 # n'th item on the dc within the rect. How it is drawn, and what
16 # is drawn is entirely up to you.
17 def OnDrawItem(self
, dc
, rect
, n
):
18 if self
.GetSelection() == n
:
19 c
= wx
.SystemSettings
.GetColour(wx
.SYS_COLOUR_HIGHLIGHTTEXT
)
21 c
= self
.GetForegroundColour()#wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
22 dc
.SetTextForeground(c
)
23 dc
.DrawLabel(self
._getItemText
(n
), rect
,
24 wx
.ALIGN_LEFT | wx
.ALIGN_CENTER_VERTICAL
)
26 # This method must be overridden. It should return the height
27 # required to draw the n'th item.
28 def OnMeasureItem(self
, n
):
31 for line
in self
._getItemText
(n
).split('\n'):
32 w
, h
= self
.GetTextExtent(line
)
38 # These are also overridable:
40 # OnDrawSeparator(dc, rect, n)
41 # Draw a separator between items. Note that rect may be reduced
42 # in size if desired so OnDrawItem gets a smaller rect.
44 # OnDrawBackground(dc, rect, n)
45 # Draw the background and maybe a border if desired.
48 def _getItemText(self
, item
):
50 return "This is item# %d" % item
52 return "This is item# %d\n with an extra line" % item
54 #----------------------------------------------------------------------
56 # The wx.HtmlListBox derives from wx.VListBox, but draws each item
57 # itself as a wx.HtmlCell.
58 class MyHtmlListBox(wx
.HtmlListBox
):
60 def OnGetItem(self
, n
):
62 return "This is item# <b>%d</b>" % n
64 return "This is item# <b>%d</b> <br>Any <font color='RED'>HTML</font> is okay." % n
66 #----------------------------------------------------------------------
68 class TestPanel(wx
.Panel
):
69 def __init__(self
, parent
, log
):
71 wx
.Panel
.__init
__(self
, parent
, -1)
74 vlb
= MyVListBox(self
, -1, size
=(150, 250), style
=wx
.BORDER_SUNKEN
)
78 vlbSizer
= wx
.BoxSizer(wx
.VERTICAL
)
79 vlbSizer
.Add((spacer
, spacer
))
80 vlbSizer
.Add(wx
.StaticText(self
, -1, "wx.VListBox"), 0, 5, wx
.ALL
)
83 hlb
= MyHtmlListBox(self
, -1, size
=(150, 250), style
=wx
.BORDER_SUNKEN
)
86 hlbSizer
= wx
.BoxSizer(wx
.VERTICAL
)
87 hlbSizer
.Add((spacer
, spacer
))
88 hlbSizer
.Add(wx
.StaticText(self
, -1, "wx.HtmlListBox"), 0, 5, wx
.ALL
)
91 sizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
92 sizer
.Add((spacer
, spacer
))
94 sizer
.Add((spacer
, spacer
))
95 sizer
.Add((spacer
, spacer
))
101 #----------------------------------------------------------------------
103 def runTest(frame
, nb
, log
):
104 win
= TestPanel(nb
, log
)
107 #----------------------------------------------------------------------
111 overview
= """<html><body>
112 <h2><center>wxVListBox and wxHtmlListBox</center></h2>
115 The "V" in wxVListBox stands for both "virtual" because it can have an
116 unlimited number of items since it doesn't store them itself, and
117 "variable" since items can vary in height. It has much the same
118 interface as wxListBox and also emits the same events so you can use
119 the same EVT_LISTBOX function to connect a handler.
122 The wxHtmlListBox derives from wxVListBox, but draws each item itself
123 as a wxHtmlCell. This means that you just need to provide a snippet
124 of HTML for each item when requested.
131 if __name__
== '__main__':
134 run
.main(['', os
.path
.basename(sys
.argv
[0])])