]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/VListBox.py
copy the metadata file into the docs archive too
[wxWidgets.git] / wxPython / demo / VListBox.py
CommitLineData
3628e088 1
8fa876ca 2import wx
3628e088
RD
3
4#----------------------------------------------------------------------
5
95bfd958 6# The wx.VListBox is much like a regular wx.ListBox except you draw the
3628e088 7# items yourself and the items can vary in height.
8fa876ca 8class MyVListBox(wx.VListBox):
3628e088
RD
9
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):
fb67a0b1
RD
14 if self.GetSelection() == n:
15 c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
16 else:
17 c = self.GetForegroundColour()#wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
18 dc.SetTextForeground(c)
3628e088 19 dc.DrawLabel(self._getItemText(n), rect,
8fa876ca 20 wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
3628e088
RD
21
22 # This method must be overridden. It should return the height
23 # required to draw the n'th item.
24 def OnMeasureItem(self, n):
25 height = 0
8fa876ca 26
3628e088
RD
27 for line in self._getItemText(n).split('\n'):
28 w, h = self.GetTextExtent(line)
29 height += h
8fa876ca 30
3628e088
RD
31 return height + 5
32
33
34 # These are also overridable:
35 #
36 # OnDrawSeparator(dc, rect, n)
37 # Draw a separator between items. Note that rect may be reduced
38 # in size if desired so OnDrawItem gets a smaller rect.
39 #
40 # OnDrawBackground(dc, rect, n)
41 # Draw the background and maybe a border if desired.
42
43
44 def _getItemText(self, item):
45 if item % 2 == 0:
46 return "This is item# %d" % item
47 else:
48 return "This is item# %d\n with an extra line" % item
49
50#----------------------------------------------------------------------
51
8fa876ca
RD
52# The wx.HtmlListBox derives from wx.VListBox, but draws each item
53# itself as a wx.HtmlCell.
54class MyHtmlListBox(wx.HtmlListBox):
3628e088
RD
55
56 def OnGetItem(self, n):
57 if n % 2 == 0:
58 return "This is item# <b>%d</b>" % n
59 else:
60 return "This is item# <b>%d</b> <br>Any <font color='RED'>HTML</font> is okay." % n
61
62#----------------------------------------------------------------------
63
8fa876ca 64class TestPanel(wx.Panel):
3628e088
RD
65 def __init__(self, parent, log):
66 self.log = log
8fa876ca 67 wx.Panel.__init__(self, parent, -1)
3628e088
RD
68 spacer = 50
69
8fa876ca 70 vlb = MyVListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
3628e088
RD
71 vlb.SetItemCount(50)
72 vlb.SetSelection(0)
73 vlb.SetFocus()
8fa876ca 74 vlbSizer = wx.BoxSizer(wx.VERTICAL)
3628e088 75 vlbSizer.Add((spacer, spacer))
8fa876ca 76 vlbSizer.Add(wx.StaticText(self, -1, "wx.VListBox"), 0, 5, wx.ALL)
3628e088
RD
77 vlbSizer.Add(vlb)
78
8fa876ca 79 hlb = MyHtmlListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
3628e088
RD
80 hlb.SetItemCount(50)
81 hlb.SetSelection(0)
8fa876ca 82 hlbSizer = wx.BoxSizer(wx.VERTICAL)
3628e088 83 hlbSizer.Add((spacer, spacer))
8fa876ca 84 hlbSizer.Add(wx.StaticText(self, -1, "wx.HtmlListBox"), 0, 5, wx.ALL)
3628e088
RD
85 hlbSizer.Add(hlb)
86
8fa876ca 87 sizer = wx.BoxSizer(wx.HORIZONTAL)
3628e088
RD
88 sizer.Add((spacer, spacer))
89 sizer.Add(vlbSizer)
90 sizer.Add((spacer, spacer))
91 sizer.Add((spacer, spacer))
92 sizer.Add(hlbSizer)
93
94 self.SetSizer(sizer)
95
96
97#----------------------------------------------------------------------
98
99def runTest(frame, nb, log):
100 win = TestPanel(nb, log)
101 return win
102
103#----------------------------------------------------------------------
104
105
106
107overview = """<html><body>
95bfd958 108<h2><center>wx.VListBox and wx.HtmlListBox</center></h2>
3628e088
RD
109<hr>
110
111The "V" in wxVListBox stands for both "virtual" because it can have an
112unlimited number of items since it doesn't store them itself, and
113"variable" since items can vary in height. It has much the same
114interface as wxListBox and also emits the same events so you can use
115the same EVT_LISTBOX function to connect a handler.
116<p>
117
95bfd958
RD
118The wx.HtmlListBox derives from wx.VListBox, but draws each item itself
119as a wx.HtmlCell. This means that you just need to provide a snippet
3628e088
RD
120of HTML for each item when requested.
121
122</body></html>
123"""
124
125
126
127if __name__ == '__main__':
128 import sys,os
129 import run
130 run.main(['', os.path.basename(sys.argv[0])])
131