1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/htmllbox.cpp
3 // Purpose: implementation of wxHtmlListBox
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWidgets license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
33 #include "wx/htmllbox.h"
35 #include "wx/html/htmlcell.h"
36 #include "wx/html/winpars.h"
38 // this hack forces the linker to always link in m_* files
39 #include "wx/html/forcelnk.h"
40 FORCE_WXHTML_MODULES()
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
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
57 for ( size_t n
= 0; n
< SIZE
; n
++ )
59 m_items
[n
] = (size_t)-1;
68 for ( size_t n
= 0; n
< SIZE
; n
++ )
74 // completely invalidate the cache
77 for ( size_t n
= 0; n
< SIZE
; n
++ )
79 m_items
[n
] = (size_t)-1;
85 // return the cached cell for this index or NULL if none
86 wxHtmlCell
*Get(size_t item
) const
88 for ( size_t n
= 0; n
< SIZE
; n
++ )
90 if ( m_items
[n
] == item
)
97 // returns true if we already have this item cached
98 bool Has(size_t item
) const { return Get(item
) != NULL
; }
100 // ensure that the item is cached
101 void Store(size_t item
, wxHtmlCell
*cell
)
103 delete m_cells
[m_next
];
104 m_cells
[m_next
] = cell
;
105 m_items
[m_next
] = item
;
107 // advance to the next item wrapping around if there are no more
108 if ( ++m_next
== SIZE
)
113 // the max number of the items we cache
116 // the index of the LRU (oldest) cell
119 // the parsed representation of the cached item or NULL
120 wxHtmlCell
*m_cells
[SIZE
];
122 // the index of the currently cached item (only valid if m_cells != NULL)
123 size_t m_items
[SIZE
];
126 // ----------------------------------------------------------------------------
127 // wxHtmlListBoxStyle
128 // ----------------------------------------------------------------------------
130 // just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that
131 // they could be overridden by the user code
132 class wxHtmlListBoxStyle
: public wxDefaultHtmlRenderingStyle
135 wxHtmlListBoxStyle(wxHtmlListBox
& hlbox
) : m_hlbox(hlbox
) { }
137 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
)
139 return m_hlbox
.GetSelectedTextColour(colFg
);
142 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
)
144 return m_hlbox
.GetSelectedTextBgColour(colBg
);
148 const wxHtmlListBox
& m_hlbox
;
150 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle
)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 BEGIN_EVENT_TABLE(wxHtmlListBox
, wxVListBox
)
159 EVT_SIZE(wxHtmlListBox::OnSize
)
162 // ============================================================================
164 // ============================================================================
166 IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox
, wxVListBox
)
169 // ----------------------------------------------------------------------------
170 // wxHtmlListBox creation
171 // ----------------------------------------------------------------------------
173 void wxHtmlListBox::Init()
176 m_htmlRendStyle
= new wxHtmlListBoxStyle(*this);
177 m_cache
= new wxHtmlListBoxCache
;
180 bool wxHtmlListBox::Create(wxWindow
*parent
,
185 const wxString
& name
)
187 return wxVListBox::Create(parent
, id
, pos
, size
, style
, name
);
190 wxHtmlListBox::~wxHtmlListBox()
196 delete m_htmlParser
->GetDC();
200 delete m_htmlRendStyle
;
203 // ----------------------------------------------------------------------------
204 // wxHtmlListBox appearance
205 // ----------------------------------------------------------------------------
207 wxColour
wxHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
209 return m_htmlRendStyle
->
210 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg
);
214 wxHtmlListBox::GetSelectedTextBgColour(const wxColour
& WXUNUSED(colBg
)) const
216 return GetSelectionBackground();
219 // ----------------------------------------------------------------------------
220 // wxHtmlListBox items markup
221 // ----------------------------------------------------------------------------
223 wxString
wxHtmlListBox::OnGetItemMarkup(size_t n
) const
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
231 // ----------------------------------------------------------------------------
232 // wxHtmlListBox cache handling
233 // ----------------------------------------------------------------------------
235 void wxHtmlListBox::CacheItem(size_t n
) const
237 if ( !m_cache
->Has(n
) )
241 wxHtmlListBox
*self
= wxConstCast(this, wxHtmlListBox
);
243 self
->m_htmlParser
= new wxHtmlWinParser
;
244 m_htmlParser
->SetDC(new wxClientDC(self
));
245 m_htmlParser
->SetFS(&self
->m_filesystem
);
248 wxHtmlContainerCell
*cell
= (wxHtmlContainerCell
*)m_htmlParser
->
249 Parse(OnGetItemMarkup(n
));
250 wxCHECK_RET( cell
, _T("wxHtmlParser::Parse() returned NULL?") );
252 cell
->Layout(GetClientSize().x
- 2*GetMargins().x
);
254 m_cache
->Store(n
, cell
);
258 void wxHtmlListBox::OnSize(wxSizeEvent
& event
)
260 // we need to relayout all the cached cells
266 void wxHtmlListBox::RefreshAll()
270 wxVListBox::RefreshAll();
273 void wxHtmlListBox::SetItemCount(size_t count
)
275 // the items are going to change, forget the old ones
278 wxVListBox::SetItemCount(count
);
281 // ----------------------------------------------------------------------------
282 // wxHtmlListBox implementation of wxVListBox pure virtuals
283 // ----------------------------------------------------------------------------
285 void wxHtmlListBox::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
289 wxHtmlCell
*cell
= m_cache
->Get(n
);
290 wxCHECK_RET( cell
, _T("this cell should be cached!") );
292 wxHtmlRenderingInfo htmlRendInfo
;
294 // draw the selected cell in selected state
297 wxHtmlSelection htmlSel
;
298 htmlSel
.Set(wxPoint(0, 0), cell
, wxPoint(INT_MAX
, INT_MAX
), cell
);
299 htmlRendInfo
.SetSelection(&htmlSel
);
300 if ( m_htmlRendStyle
)
301 htmlRendInfo
.SetStyle(m_htmlRendStyle
);
302 htmlRendInfo
.GetState().SetSelectionState(wxHTML_SEL_IN
);
305 // note that we can't stop drawing exactly at the window boundary as then
306 // even the visible cells part could be not drawn, so always draw the
308 cell
->Draw(dc
, rect
.x
+2, rect
.y
+2, 0, INT_MAX
, htmlRendInfo
);
311 wxCoord
wxHtmlListBox::OnMeasureItem(size_t n
) const
315 wxHtmlCell
*cell
= m_cache
->Get(n
);
316 wxCHECK_MSG( cell
, 0, _T("this cell should be cached!") );
318 return cell
->GetHeight() + cell
->GetDescent() + 4;