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