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: wxWindows 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"
31 #include "wx/htmllbox.h"
33 #include "wx/html/htmlcell.h"
34 #include "wx/html/winpars.h"
36 // this hack forces the linker to always link in m_* files
37 #include "wx/html/forcelnk.h"
38 FORCE_WXHTML_MODULES()
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
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
50 class wxHtmlListBoxCache
55 for ( size_t n
= 0; n
< SIZE
; n
++ )
57 m_items
[n
] = (size_t)-1;
66 for ( size_t n
= 0; n
< SIZE
; n
++ )
72 // completely invalidate the cache
75 for ( size_t n
= 0; n
< SIZE
; n
++ )
77 m_items
[n
] = (size_t)-1;
83 // return the cached cell for this index or NULL if none
84 wxHtmlCell
*Get(size_t item
) const
86 for ( size_t n
= 0; n
< SIZE
; n
++ )
88 if ( m_items
[n
] == item
)
95 // returns true if we already have this item cached
96 bool Has(size_t item
) const { return Get(item
) != NULL
; }
98 // ensure that the item is cached
99 void Store(size_t item
, wxHtmlCell
*cell
)
101 delete m_cells
[m_next
];
102 m_cells
[m_next
] = cell
;
103 m_items
[m_next
] = item
;
105 // advance to the next item wrapping around if there are no more
106 if ( ++m_next
== SIZE
)
111 // the max number of the items we cache
114 // the index of the LRU (oldest) cell
117 // the parsed representation of the cached item or NULL
118 wxHtmlCell
*m_cells
[SIZE
];
120 // the index of the currently cached item (only valid if m_cells != NULL)
121 size_t m_items
[SIZE
];
124 // ----------------------------------------------------------------------------
125 // wxHtmlListBoxStyle
126 // ----------------------------------------------------------------------------
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
133 wxHtmlListBoxStyle(wxHtmlListBox
& hlbox
) : m_hlbox(hlbox
) { }
135 virtual wxColour
GetSelectedTextColour(const wxColour
& colFg
)
137 return m_hlbox
.GetSelectedTextColour(colFg
);
140 virtual wxColour
GetSelectedTextBgColour(const wxColour
& colBg
)
142 return m_hlbox
.GetSelectedTextBgColour(colBg
);
146 const wxHtmlListBox
& m_hlbox
;
148 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle
)
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 BEGIN_EVENT_TABLE(wxHtmlListBox
, wxVListBox
)
157 EVT_SIZE(wxHtmlListBox::OnSize
)
160 // ============================================================================
162 // ============================================================================
164 IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox
, wxVListBox
)
167 // ----------------------------------------------------------------------------
168 // wxHtmlListBox creation
169 // ----------------------------------------------------------------------------
171 void wxHtmlListBox::Init()
174 m_htmlRendStyle
= new wxHtmlListBoxStyle(*this);
175 m_cache
= new wxHtmlListBoxCache
;
178 bool wxHtmlListBox::Create(wxWindow
*parent
,
183 const wxString
& name
)
185 return wxVListBox::Create(parent
, id
, pos
, size
, style
, name
);
188 wxHtmlListBox::~wxHtmlListBox()
194 delete m_htmlParser
->GetDC();
198 delete m_htmlRendStyle
;
201 // ----------------------------------------------------------------------------
202 // wxHtmlListBox appearance
203 // ----------------------------------------------------------------------------
205 wxColour
wxHtmlListBox::GetSelectedTextColour(const wxColour
& colFg
) const
207 return m_htmlRendStyle
->
208 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg
);
212 wxHtmlListBox::GetSelectedTextBgColour(const wxColour
& WXUNUSED(colBg
)) const
214 return GetSelectionBackground();
217 // ----------------------------------------------------------------------------
218 // wxHtmlListBox items markup
219 // ----------------------------------------------------------------------------
221 wxString
wxHtmlListBox::OnGetItemMarkup(size_t n
) const
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
229 // ----------------------------------------------------------------------------
230 // wxHtmlListBox cache handling
231 // ----------------------------------------------------------------------------
233 void wxHtmlListBox::CacheItem(size_t n
) const
235 if ( !m_cache
->Has(n
) )
239 wxHtmlListBox
*self
= wxConstCast(this, wxHtmlListBox
);
241 self
->m_htmlParser
= new wxHtmlWinParser
;
242 m_htmlParser
->SetDC(new wxClientDC(self
));
245 wxHtmlContainerCell
*cell
= (wxHtmlContainerCell
*)m_htmlParser
->
246 Parse(OnGetItemMarkup(n
));
247 wxCHECK_RET( cell
, _T("wxHtmlParser::Parse() returned NULL?") );
249 cell
->Layout(GetClientSize().x
- 2*GetMargins().x
);
251 m_cache
->Store(n
, cell
);
255 void wxHtmlListBox::OnSize(wxSizeEvent
& event
)
257 // we need to relayout all the cached cells
263 void wxHtmlListBox::RefreshAll()
267 wxVListBox::RefreshAll();
270 void wxHtmlListBox::SetItemCount(size_t count
)
272 // the items are going to change, forget the old ones
275 wxVListBox::SetItemCount(count
);
278 // ----------------------------------------------------------------------------
279 // wxHtmlListBox implementation of wxVListBox pure virtuals
280 // ----------------------------------------------------------------------------
282 void wxHtmlListBox::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
286 wxHtmlCell
*cell
= m_cache
->Get(n
);
287 wxCHECK_RET( cell
, _T("this cell should be cached!") );
289 wxHtmlRenderingInfo htmlRendInfo
;
291 // draw the selected cell in selected state
294 wxHtmlSelection htmlSel
;
295 htmlSel
.Set(wxPoint(0, 0), cell
, wxPoint(INT_MAX
, INT_MAX
), cell
);
296 htmlRendInfo
.SetSelection(&htmlSel
);
297 if ( m_htmlRendStyle
)
298 htmlRendInfo
.SetStyle(m_htmlRendStyle
);
299 htmlRendInfo
.GetState().SetSelectionState(wxHTML_SEL_IN
);
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
305 cell
->Draw(dc
, rect
.x
+2, rect
.y
+2, 0, INT_MAX
, htmlRendInfo
);
308 wxCoord
wxHtmlListBox::OnMeasureItem(size_t n
) const
312 wxHtmlCell
*cell
= m_cache
->Get(n
);
313 wxCHECK_MSG( cell
, 0, _T("this cell should be cached!") );
315 return cell
->GetHeight() + cell
->GetDescent() + 4;