]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/VListBox.py
Set the DC font before drawing the label
[wxWidgets.git] / wxPython / demo / VListBox.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5
6 import wx
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.
12 class MyVListBox(wx.VListBox):
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 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)
23 dc.DrawLabel(self._getItemText(n), rect,
24 wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
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
30
31 for line in self._getItemText(n).split('\n'):
32 w, h = self.GetTextExtent(line)
33 height += h
34
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
56 # The wx.HtmlListBox derives from wx.VListBox, but draws each item
57 # itself as a wx.HtmlCell.
58 class MyHtmlListBox(wx.HtmlListBox):
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
68 class TestPanel(wx.Panel):
69 def __init__(self, parent, log):
70 self.log = log
71 wx.Panel.__init__(self, parent, -1)
72 spacer = 50
73
74 vlb = MyVListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
75 vlb.SetItemCount(50)
76 vlb.SetSelection(0)
77 vlb.SetFocus()
78 vlbSizer = wx.BoxSizer(wx.VERTICAL)
79 vlbSizer.Add((spacer, spacer))
80 vlbSizer.Add(wx.StaticText(self, -1, "wx.VListBox"), 0, 5, wx.ALL)
81 vlbSizer.Add(vlb)
82
83 hlb = MyHtmlListBox(self, -1, size=(150, 250), style=wx.BORDER_SUNKEN)
84 hlb.SetItemCount(50)
85 hlb.SetSelection(0)
86 hlbSizer = wx.BoxSizer(wx.VERTICAL)
87 hlbSizer.Add((spacer, spacer))
88 hlbSizer.Add(wx.StaticText(self, -1, "wx.HtmlListBox"), 0, 5, wx.ALL)
89 hlbSizer.Add(hlb)
90
91 sizer = wx.BoxSizer(wx.HORIZONTAL)
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
103 def runTest(frame, nb, log):
104 win = TestPanel(nb, log)
105 return win
106
107 #----------------------------------------------------------------------
108
109
110
111 overview = """<html><body>
112 <h2><center>wxVListBox and wxHtmlListBox</center></h2>
113 <hr>
114
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.
120 <p>
121
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.
125
126 </body></html>
127 """
128
129
130
131 if __name__ == '__main__':
132 import sys,os
133 import run
134 run.main(['', os.path.basename(sys.argv[0])])
135