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