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