invalidate cache when RefreshLine[s]() is called
[wxWidgets.git] / src / generic / htmllbox.cpp
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
28 #include "wx/dcclient.h"
29 #endif //WX_PRECOMP
30
31 #if wxUSE_HTML
32
33 #include "wx/htmllbox.h"
34
35 #include "wx/html/htmlcell.h"
36 #include "wx/html/winpars.h"
37
38 // this hack forces the linker to always link in m_* files
39 #include "wx/html/forcelnk.h"
40 FORCE_WXHTML_MODULES()
41
42 // ============================================================================
43 // private classes
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxHtmlListBoxCache
48 // ----------------------------------------------------------------------------
49
50 // this class is used by wxHtmlListBox to cache the parsed representation of
51 // the items to avoid doing it anew each time an item must be drawn
52 class wxHtmlListBoxCache
53 {
54 private:
55 // invalidate a single item, used by Clear() and InvalidateRange()
56 void InvalidateItem(size_t n)
57 {
58 m_items[n] = (size_t)-1;
59 delete m_cells[n];
60 m_cells[n] = NULL;
61 }
62
63 public:
64 wxHtmlListBoxCache()
65 {
66 for ( size_t n = 0; n < SIZE; n++ )
67 {
68 m_items[n] = (size_t)-1;
69 m_cells[n] = NULL;
70 }
71
72 m_next = 0;
73 }
74
75 ~wxHtmlListBoxCache()
76 {
77 for ( size_t n = 0; n < SIZE; n++ )
78 {
79 delete m_cells[n];
80 }
81 }
82
83 // completely invalidate the cache
84 void Clear()
85 {
86 for ( size_t n = 0; n < SIZE; n++ )
87 {
88 InvalidateItem(n);
89 }
90 }
91
92 // return the cached cell for this index or NULL if none
93 wxHtmlCell *Get(size_t item) const
94 {
95 for ( size_t n = 0; n < SIZE; n++ )
96 {
97 if ( m_items[n] == item )
98 return m_cells[n];
99 }
100
101 return NULL;
102 }
103
104 // returns true if we already have this item cached
105 bool Has(size_t item) const { return Get(item) != NULL; }
106
107 // ensure that the item is cached
108 void Store(size_t item, wxHtmlCell *cell)
109 {
110 delete m_cells[m_next];
111 m_cells[m_next] = cell;
112 m_items[m_next] = item;
113
114 // advance to the next item wrapping around if there are no more
115 if ( ++m_next == SIZE )
116 m_next = 0;
117 }
118
119 // forget the cached value of the item(s) between the given ones (inclusive)
120 void InvalidateRange(size_t from, size_t to)
121 {
122 for ( size_t n = 0; n < SIZE; n++ )
123 {
124 if ( m_items[n] >= from && m_items[n] <= to )
125 {
126 InvalidateItem(n);
127 }
128 }
129 }
130
131 private:
132 // the max number of the items we cache
133 enum { SIZE = 50 };
134
135 // the index of the LRU (oldest) cell
136 size_t m_next;
137
138 // the parsed representation of the cached item or NULL
139 wxHtmlCell *m_cells[SIZE];
140
141 // the index of the currently cached item (only valid if m_cells != NULL)
142 size_t m_items[SIZE];
143 };
144
145 // ----------------------------------------------------------------------------
146 // wxHtmlListBoxStyle
147 // ----------------------------------------------------------------------------
148
149 // just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that
150 // they could be overridden by the user code
151 class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle
152 {
153 public:
154 wxHtmlListBoxStyle(wxHtmlListBox& hlbox) : m_hlbox(hlbox) { }
155
156 virtual wxColour GetSelectedTextColour(const wxColour& colFg)
157 {
158 return m_hlbox.GetSelectedTextColour(colFg);
159 }
160
161 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg)
162 {
163 return m_hlbox.GetSelectedTextBgColour(colBg);
164 }
165
166 private:
167 const wxHtmlListBox& m_hlbox;
168
169 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle)
170 };
171
172
173 // ----------------------------------------------------------------------------
174 // event tables
175 // ----------------------------------------------------------------------------
176
177 BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox)
178 EVT_SIZE(wxHtmlListBox::OnSize)
179 END_EVENT_TABLE()
180
181 // ============================================================================
182 // implementation
183 // ============================================================================
184
185 IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox)
186
187
188 // ----------------------------------------------------------------------------
189 // wxHtmlListBox creation
190 // ----------------------------------------------------------------------------
191
192 void wxHtmlListBox::Init()
193 {
194 m_htmlParser = NULL;
195 m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
196 m_cache = new wxHtmlListBoxCache;
197 }
198
199 bool wxHtmlListBox::Create(wxWindow *parent,
200 wxWindowID id,
201 const wxPoint& pos,
202 const wxSize& size,
203 long style,
204 const wxString& name)
205 {
206 return wxVListBox::Create(parent, id, pos, size, style, name);
207 }
208
209 wxHtmlListBox::~wxHtmlListBox()
210 {
211 delete m_cache;
212
213 if ( m_htmlParser )
214 {
215 delete m_htmlParser->GetDC();
216 delete m_htmlParser;
217 }
218
219 delete m_htmlRendStyle;
220 }
221
222 // ----------------------------------------------------------------------------
223 // wxHtmlListBox appearance
224 // ----------------------------------------------------------------------------
225
226 wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
227 {
228 return m_htmlRendStyle->
229 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg);
230 }
231
232 wxColour
233 wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const
234 {
235 return GetSelectionBackground();
236 }
237
238 // ----------------------------------------------------------------------------
239 // wxHtmlListBox items markup
240 // ----------------------------------------------------------------------------
241
242 wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const
243 {
244 // we don't even need to wrap the value returned by OnGetItem() inside
245 // "<html><body>" and "</body></html>" because wxHTML can parse it even
246 // without these tags
247 return OnGetItem(n);
248 }
249
250 // ----------------------------------------------------------------------------
251 // wxHtmlListBox cache handling
252 // ----------------------------------------------------------------------------
253
254 void wxHtmlListBox::CacheItem(size_t n) const
255 {
256 if ( !m_cache->Has(n) )
257 {
258 if ( !m_htmlParser )
259 {
260 wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox);
261
262 self->m_htmlParser = new wxHtmlWinParser;
263 m_htmlParser->SetDC(new wxClientDC(self));
264 m_htmlParser->SetFS(&self->m_filesystem);
265
266 // use system's default GUI font by default:
267 m_htmlParser->SetStandardFonts();
268 }
269
270 wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser->
271 Parse(OnGetItemMarkup(n));
272 wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") );
273
274 cell->Layout(GetClientSize().x - 2*GetMargins().x);
275
276 m_cache->Store(n, cell);
277 }
278 }
279
280 void wxHtmlListBox::OnSize(wxSizeEvent& event)
281 {
282 // we need to relayout all the cached cells
283 m_cache->Clear();
284
285 event.Skip();
286 }
287
288 void wxHtmlListBox::RefreshLine(size_t line)
289 {
290 m_cache->InvalidateRange(line, line);
291
292 wxVListBox::RefreshLine(line);
293 }
294
295 void wxHtmlListBox::RefreshLines(size_t from, size_t to)
296 {
297 m_cache->InvalidateRange(from, to);
298
299 wxVListBox::RefreshLines(from, to);
300 }
301
302 void wxHtmlListBox::RefreshAll()
303 {
304 m_cache->Clear();
305
306 wxVListBox::RefreshAll();
307 }
308
309 void wxHtmlListBox::SetItemCount(size_t count)
310 {
311 // the items are going to change, forget the old ones
312 m_cache->Clear();
313
314 wxVListBox::SetItemCount(count);
315 }
316
317 // ----------------------------------------------------------------------------
318 // wxHtmlListBox implementation of wxVListBox pure virtuals
319 // ----------------------------------------------------------------------------
320
321 void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
322 {
323 CacheItem(n);
324
325 wxHtmlCell *cell = m_cache->Get(n);
326 wxCHECK_RET( cell, _T("this cell should be cached!") );
327
328 wxHtmlRenderingInfo htmlRendInfo;
329
330 // draw the selected cell in selected state
331 if ( IsSelected(n) )
332 {
333 wxHtmlSelection htmlSel;
334 htmlSel.Set(wxPoint(0, 0), cell, wxPoint(INT_MAX, INT_MAX), cell);
335 htmlRendInfo.SetSelection(&htmlSel);
336 if ( m_htmlRendStyle )
337 htmlRendInfo.SetStyle(m_htmlRendStyle);
338 htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN);
339 }
340
341 // note that we can't stop drawing exactly at the window boundary as then
342 // even the visible cells part could be not drawn, so always draw the
343 // entire cell
344 cell->Draw(dc, rect.x+2, rect.y+2, 0, INT_MAX, htmlRendInfo);
345 }
346
347 wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const
348 {
349 CacheItem(n);
350
351 wxHtmlCell *cell = m_cache->Get(n);
352 wxCHECK_MSG( cell, 0, _T("this cell should be cached!") );
353
354 return cell->GetHeight() + cell->GetDescent() + 4;
355 }
356
357 #endif // wxUSE_HTML
358