]> git.saurik.com Git - wxWidgets.git/blame - src/generic/htmllbox.cpp
Removed unnecessary casts
[wxWidgets.git] / src / generic / htmllbox.cpp
CommitLineData
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>
0a53b9b8 9// License: wxWindows license
e0c6027b
VZ
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
461dae94
VZ
31#if wxUSE_HTML
32
e0c6027b
VZ
33#include "wx/htmllbox.h"
34
35#include "wx/html/htmlcell.h"
36#include "wx/html/winpars.h"
37
5ecdc7ab
VZ
38// this hack forces the linker to always link in m_* files
39#include "wx/html/forcelnk.h"
40FORCE_WXHTML_MODULES()
41
9a9b4940 42// ============================================================================
e0c6027b 43// private classes
9a9b4940
VZ
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxHtmlListBoxCache
e0c6027b
VZ
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
e0c6027b
VZ
52class wxHtmlListBoxCache
53{
aead8a4e
VZ
54private:
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
e0c6027b 63public:
5ecdc7ab
VZ
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 }
e0c6027b 71
5ecdc7ab
VZ
72 m_next = 0;
73 }
e0c6027b 74
5ecdc7ab 75 ~wxHtmlListBoxCache()
e0c6027b 76 {
5ecdc7ab
VZ
77 for ( size_t n = 0; n < SIZE; n++ )
78 {
79 delete m_cells[n];
80 }
81 }
e0c6027b 82
5ecdc7ab
VZ
83 // completely invalidate the cache
84 void Clear()
85 {
86 for ( size_t n = 0; n < SIZE; n++ )
87 {
aead8a4e 88 InvalidateItem(n);
5ecdc7ab 89 }
e0c6027b
VZ
90 }
91
92 // return the cached cell for this index or NULL if none
5ecdc7ab
VZ
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)
e0c6027b 109 {
5ecdc7ab
VZ
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;
e0c6027b
VZ
117 }
118
aead8a4e
VZ
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
e0c6027b 131private:
5ecdc7ab
VZ
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
e0c6027b 138 // the parsed representation of the cached item or NULL
5ecdc7ab 139 wxHtmlCell *m_cells[SIZE];
e0c6027b 140
5ecdc7ab
VZ
141 // the index of the currently cached item (only valid if m_cells != NULL)
142 size_t m_items[SIZE];
e0c6027b
VZ
143};
144
9a9b4940
VZ
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
151class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle
152{
153public:
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
166private:
167 const wxHtmlListBox& m_hlbox;
27d0dcd0
VZ
168
169 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle)
9a9b4940
VZ
170};
171
172
5ecdc7ab
VZ
173// ----------------------------------------------------------------------------
174// event tables
175// ----------------------------------------------------------------------------
176
177BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox)
178 EVT_SIZE(wxHtmlListBox::OnSize)
179END_EVENT_TABLE()
180
e0c6027b
VZ
181// ============================================================================
182// implementation
183// ============================================================================
184
0c8392ca
RD
185IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox)
186
187
e0c6027b
VZ
188// ----------------------------------------------------------------------------
189// wxHtmlListBox creation
190// ----------------------------------------------------------------------------
191
192void wxHtmlListBox::Init()
193{
194 m_htmlParser = NULL;
9a9b4940 195 m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
e0c6027b
VZ
196 m_cache = new wxHtmlListBoxCache;
197}
198
199bool wxHtmlListBox::Create(wxWindow *parent,
200 wxWindowID id,
201 const wxPoint& pos,
202 const wxSize& size,
e0c6027b
VZ
203 long style,
204 const wxString& name)
205{
43e319a3 206 return wxVListBox::Create(parent, id, pos, size, style, name);
e0c6027b
VZ
207}
208
209wxHtmlListBox::~wxHtmlListBox()
210{
211 delete m_cache;
9a9b4940 212
e0c6027b
VZ
213 if ( m_htmlParser )
214 {
215 delete m_htmlParser->GetDC();
216 delete m_htmlParser;
217 }
9a9b4940
VZ
218
219 delete m_htmlRendStyle;
220}
221
222// ----------------------------------------------------------------------------
223// wxHtmlListBox appearance
224// ----------------------------------------------------------------------------
225
226wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
227{
228 return m_htmlRendStyle->
229 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg);
230}
231
232wxColour
233wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const
234{
235 return GetSelectionBackground();
e0c6027b
VZ
236}
237
238// ----------------------------------------------------------------------------
239// wxHtmlListBox items markup
240// ----------------------------------------------------------------------------
241
242wxString 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
5ecdc7ab
VZ
250// ----------------------------------------------------------------------------
251// wxHtmlListBox cache handling
252// ----------------------------------------------------------------------------
253
e0c6027b
VZ
254void 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));
2d814c19 264 m_htmlParser->SetFS(&self->m_filesystem);
f987cbb1
VS
265
266 // use system's default GUI font by default:
267 m_htmlParser->SetStandardFonts();
e0c6027b
VZ
268 }
269
270 wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser->
271 Parse(OnGetItemMarkup(n));
272 wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") );
273
5ecdc7ab 274 cell->Layout(GetClientSize().x - 2*GetMargins().x);
e0c6027b
VZ
275
276 m_cache->Store(n, cell);
277 }
278}
279
5ecdc7ab
VZ
280void wxHtmlListBox::OnSize(wxSizeEvent& event)
281{
282 // we need to relayout all the cached cells
283 m_cache->Clear();
284
285 event.Skip();
286}
287
aead8a4e
VZ
288void wxHtmlListBox::RefreshLine(size_t line)
289{
290 m_cache->InvalidateRange(line, line);
291
292 wxVListBox::RefreshLine(line);
293}
294
295void wxHtmlListBox::RefreshLines(size_t from, size_t to)
296{
297 m_cache->InvalidateRange(from, to);
298
299 wxVListBox::RefreshLines(from, to);
300}
301
5ecdc7ab
VZ
302void wxHtmlListBox::RefreshAll()
303{
304 m_cache->Clear();
305
306 wxVListBox::RefreshAll();
307}
308
03495767
VZ
309void 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
e0c6027b
VZ
317// ----------------------------------------------------------------------------
318// wxHtmlListBox implementation of wxVListBox pure virtuals
319// ----------------------------------------------------------------------------
320
321void 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
901b583c 328 wxHtmlRenderingInfo htmlRendInfo;
5ecdc7ab 329
e0c6027b
VZ
330 // draw the selected cell in selected state
331 if ( IsSelected(n) )
332 {
333 wxHtmlSelection htmlSel;
c47addef 334 htmlSel.Set(wxPoint(0,0), cell, wxPoint(INT_MAX, INT_MAX), cell);
901b583c 335 htmlRendInfo.SetSelection(&htmlSel);
9a9b4940
VZ
336 if ( m_htmlRendStyle )
337 htmlRendInfo.SetStyle(m_htmlRendStyle);
338 htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN);
e0c6027b 339 }
5ecdc7ab
VZ
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
d3017584 344 cell->Draw(dc, rect.x+2, rect.y+2, 0, INT_MAX, htmlRendInfo);
e0c6027b
VZ
345}
346
347wxCoord 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
d3017584 354 return cell->GetHeight() + cell->GetDescent() + 4;
e0c6027b
VZ
355}
356
aead8a4e
VZ
357#endif // wxUSE_HTML
358