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