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