]>
Commit | Line | Data |
---|---|---|
e0c6027b VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: generic/htmllbox.cpp | |
3 | // Purpose: implementation of wxHtmlListBox | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 31.05.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
bb178b29 | 28 | #include "wx/dcclient.h" |
e0c6027b VZ |
29 | #endif //WX_PRECOMP |
30 | ||
31 | #include "wx/htmllbox.h" | |
32 | ||
33 | #include "wx/html/htmlcell.h" | |
34 | #include "wx/html/winpars.h" | |
35 | ||
5ecdc7ab VZ |
36 | // this hack forces the linker to always link in m_* files |
37 | #include "wx/html/forcelnk.h" | |
38 | FORCE_WXHTML_MODULES() | |
39 | ||
9a9b4940 | 40 | // ============================================================================ |
e0c6027b | 41 | // private classes |
9a9b4940 VZ |
42 | // ============================================================================ |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // wxHtmlListBoxCache | |
e0c6027b VZ |
46 | // ---------------------------------------------------------------------------- |
47 | ||
48 | // this class is used by wxHtmlListBox to cache the parsed representation of | |
49 | // the items to avoid doing it anew each time an item must be drawn | |
e0c6027b VZ |
50 | class wxHtmlListBoxCache |
51 | { | |
52 | public: | |
5ecdc7ab VZ |
53 | wxHtmlListBoxCache() |
54 | { | |
55 | for ( size_t n = 0; n < SIZE; n++ ) | |
56 | { | |
57 | m_items[n] = (size_t)-1; | |
58 | m_cells[n] = NULL; | |
59 | } | |
e0c6027b | 60 | |
5ecdc7ab VZ |
61 | m_next = 0; |
62 | } | |
e0c6027b | 63 | |
5ecdc7ab | 64 | ~wxHtmlListBoxCache() |
e0c6027b | 65 | { |
5ecdc7ab VZ |
66 | for ( size_t n = 0; n < SIZE; n++ ) |
67 | { | |
68 | delete m_cells[n]; | |
69 | } | |
70 | } | |
e0c6027b | 71 | |
5ecdc7ab VZ |
72 | // completely invalidate the cache |
73 | void Clear() | |
74 | { | |
75 | for ( size_t n = 0; n < SIZE; n++ ) | |
76 | { | |
77 | m_items[n] = (size_t)-1; | |
78 | delete m_cells[n]; | |
79 | m_cells[n] = NULL; | |
80 | } | |
e0c6027b VZ |
81 | } |
82 | ||
83 | // return the cached cell for this index or NULL if none | |
5ecdc7ab VZ |
84 | wxHtmlCell *Get(size_t item) const |
85 | { | |
86 | for ( size_t n = 0; n < SIZE; n++ ) | |
87 | { | |
88 | if ( m_items[n] == item ) | |
89 | return m_cells[n]; | |
90 | } | |
91 | ||
92 | return NULL; | |
93 | } | |
94 | ||
95 | // returns true if we already have this item cached | |
96 | bool Has(size_t item) const { return Get(item) != NULL; } | |
97 | ||
98 | // ensure that the item is cached | |
99 | void Store(size_t item, wxHtmlCell *cell) | |
e0c6027b | 100 | { |
5ecdc7ab VZ |
101 | delete m_cells[m_next]; |
102 | m_cells[m_next] = cell; | |
103 | m_items[m_next] = item; | |
104 | ||
105 | // advance to the next item wrapping around if there are no more | |
106 | if ( ++m_next == SIZE ) | |
107 | m_next = 0; | |
e0c6027b VZ |
108 | } |
109 | ||
110 | private: | |
5ecdc7ab VZ |
111 | // the max number of the items we cache |
112 | enum { SIZE = 50 }; | |
113 | ||
114 | // the index of the LRU (oldest) cell | |
115 | size_t m_next; | |
116 | ||
e0c6027b | 117 | // the parsed representation of the cached item or NULL |
5ecdc7ab | 118 | wxHtmlCell *m_cells[SIZE]; |
e0c6027b | 119 | |
5ecdc7ab VZ |
120 | // the index of the currently cached item (only valid if m_cells != NULL) |
121 | size_t m_items[SIZE]; | |
e0c6027b VZ |
122 | }; |
123 | ||
9a9b4940 VZ |
124 | // ---------------------------------------------------------------------------- |
125 | // wxHtmlListBoxStyle | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | // just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that | |
129 | // they could be overridden by the user code | |
130 | class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle | |
131 | { | |
132 | public: | |
133 | wxHtmlListBoxStyle(wxHtmlListBox& hlbox) : m_hlbox(hlbox) { } | |
134 | ||
135 | virtual wxColour GetSelectedTextColour(const wxColour& colFg) | |
136 | { | |
137 | return m_hlbox.GetSelectedTextColour(colFg); | |
138 | } | |
139 | ||
140 | virtual wxColour GetSelectedTextBgColour(const wxColour& colBg) | |
141 | { | |
142 | return m_hlbox.GetSelectedTextBgColour(colBg); | |
143 | } | |
144 | ||
145 | private: | |
146 | const wxHtmlListBox& m_hlbox; | |
27d0dcd0 VZ |
147 | |
148 | DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle) | |
9a9b4940 VZ |
149 | }; |
150 | ||
151 | ||
5ecdc7ab VZ |
152 | // ---------------------------------------------------------------------------- |
153 | // event tables | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
156 | BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox) | |
157 | EVT_SIZE(wxHtmlListBox::OnSize) | |
158 | END_EVENT_TABLE() | |
159 | ||
e0c6027b VZ |
160 | // ============================================================================ |
161 | // implementation | |
162 | // ============================================================================ | |
163 | ||
0c8392ca RD |
164 | IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox) |
165 | ||
166 | ||
e0c6027b VZ |
167 | // ---------------------------------------------------------------------------- |
168 | // wxHtmlListBox creation | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | void wxHtmlListBox::Init() | |
172 | { | |
173 | m_htmlParser = NULL; | |
9a9b4940 | 174 | m_htmlRendStyle = new wxHtmlListBoxStyle(*this); |
e0c6027b VZ |
175 | m_cache = new wxHtmlListBoxCache; |
176 | } | |
177 | ||
178 | bool wxHtmlListBox::Create(wxWindow *parent, | |
179 | wxWindowID id, | |
180 | const wxPoint& pos, | |
181 | const wxSize& size, | |
e0c6027b VZ |
182 | long style, |
183 | const wxString& name) | |
184 | { | |
43e319a3 | 185 | return wxVListBox::Create(parent, id, pos, size, style, name); |
e0c6027b VZ |
186 | } |
187 | ||
188 | wxHtmlListBox::~wxHtmlListBox() | |
189 | { | |
190 | delete m_cache; | |
9a9b4940 | 191 | |
e0c6027b VZ |
192 | if ( m_htmlParser ) |
193 | { | |
194 | delete m_htmlParser->GetDC(); | |
195 | delete m_htmlParser; | |
196 | } | |
9a9b4940 VZ |
197 | |
198 | delete m_htmlRendStyle; | |
199 | } | |
200 | ||
201 | // ---------------------------------------------------------------------------- | |
202 | // wxHtmlListBox appearance | |
203 | // ---------------------------------------------------------------------------- | |
204 | ||
205 | wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const | |
206 | { | |
207 | return m_htmlRendStyle-> | |
208 | wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg); | |
209 | } | |
210 | ||
211 | wxColour | |
212 | wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const | |
213 | { | |
214 | return GetSelectionBackground(); | |
e0c6027b VZ |
215 | } |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // wxHtmlListBox items markup | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const | |
222 | { | |
223 | // we don't even need to wrap the value returned by OnGetItem() inside | |
224 | // "<html><body>" and "</body></html>" because wxHTML can parse it even | |
225 | // without these tags | |
226 | return OnGetItem(n); | |
227 | } | |
228 | ||
5ecdc7ab VZ |
229 | // ---------------------------------------------------------------------------- |
230 | // wxHtmlListBox cache handling | |
231 | // ---------------------------------------------------------------------------- | |
232 | ||
e0c6027b VZ |
233 | void wxHtmlListBox::CacheItem(size_t n) const |
234 | { | |
235 | if ( !m_cache->Has(n) ) | |
236 | { | |
237 | if ( !m_htmlParser ) | |
238 | { | |
239 | wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox); | |
240 | ||
241 | self->m_htmlParser = new wxHtmlWinParser; | |
242 | m_htmlParser->SetDC(new wxClientDC(self)); | |
243 | } | |
244 | ||
245 | wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser-> | |
246 | Parse(OnGetItemMarkup(n)); | |
247 | wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") ); | |
248 | ||
5ecdc7ab | 249 | cell->Layout(GetClientSize().x - 2*GetMargins().x); |
e0c6027b VZ |
250 | |
251 | m_cache->Store(n, cell); | |
252 | } | |
253 | } | |
254 | ||
5ecdc7ab VZ |
255 | void wxHtmlListBox::OnSize(wxSizeEvent& event) |
256 | { | |
257 | // we need to relayout all the cached cells | |
258 | m_cache->Clear(); | |
259 | ||
260 | event.Skip(); | |
261 | } | |
262 | ||
263 | void wxHtmlListBox::RefreshAll() | |
264 | { | |
265 | m_cache->Clear(); | |
266 | ||
267 | wxVListBox::RefreshAll(); | |
268 | } | |
269 | ||
03495767 VZ |
270 | void wxHtmlListBox::SetItemCount(size_t count) |
271 | { | |
272 | // the items are going to change, forget the old ones | |
273 | m_cache->Clear(); | |
274 | ||
275 | wxVListBox::SetItemCount(count); | |
276 | } | |
277 | ||
e0c6027b VZ |
278 | // ---------------------------------------------------------------------------- |
279 | // wxHtmlListBox implementation of wxVListBox pure virtuals | |
280 | // ---------------------------------------------------------------------------- | |
281 | ||
282 | void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const | |
283 | { | |
284 | CacheItem(n); | |
285 | ||
286 | wxHtmlCell *cell = m_cache->Get(n); | |
287 | wxCHECK_RET( cell, _T("this cell should be cached!") ); | |
288 | ||
901b583c | 289 | wxHtmlRenderingInfo htmlRendInfo; |
5ecdc7ab | 290 | |
e0c6027b VZ |
291 | // draw the selected cell in selected state |
292 | if ( IsSelected(n) ) | |
293 | { | |
294 | wxHtmlSelection htmlSel; | |
295 | htmlSel.Set(wxPoint(0, 0), cell, wxPoint(INT_MAX, INT_MAX), cell); | |
901b583c | 296 | htmlRendInfo.SetSelection(&htmlSel); |
9a9b4940 VZ |
297 | if ( m_htmlRendStyle ) |
298 | htmlRendInfo.SetStyle(m_htmlRendStyle); | |
299 | htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN); | |
e0c6027b | 300 | } |
5ecdc7ab VZ |
301 | |
302 | // note that we can't stop drawing exactly at the window boundary as then | |
303 | // even the visible cells part could be not drawn, so always draw the | |
304 | // entire cell | |
d3017584 | 305 | cell->Draw(dc, rect.x+2, rect.y+2, 0, INT_MAX, htmlRendInfo); |
e0c6027b VZ |
306 | } |
307 | ||
308 | wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const | |
309 | { | |
310 | CacheItem(n); | |
311 | ||
312 | wxHtmlCell *cell = m_cache->Get(n); | |
313 | wxCHECK_MSG( cell, 0, _T("this cell should be cached!") ); | |
314 | ||
d3017584 | 315 | return cell->GetHeight() + cell->GetDescent() + 4; |
e0c6027b VZ |
316 | } |
317 |