]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/htmllbox.cpp
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"
30 #include "wx/htmllbox.h"
32 #include "wx/html/htmlcell.h"
33 #include "wx/html/winpars.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // this class is used by wxHtmlListBox to cache the parsed representation of
40 // the items to avoid doing it anew each time an item must be drawn
42 // TODO: extend the class to cache more than item
43 class wxHtmlListBoxCache
46 wxHtmlListBoxCache() { m_cell
= NULL
; }
47 ~wxHtmlListBoxCache() { delete m_cell
; }
49 // returns true if we already have this item cached
50 bool Has(size_t n
) const { return m_cell
&& n
== m_item
; }
52 // ensure that the item is cached
53 void Store(size_t n
, wxHtmlCell
*cell
)
61 // return the cached cell for this index or NULL if none
62 wxHtmlCell
*Get(size_t n
) const
64 // we could be reading uninitialized m_item here but the code is still
66 return n
== m_item
? m_cell
: NULL
;
70 // the parsed representation of the cached item or NULL
73 // the index of the currently cached item (only valid if m_cell != NULL)
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
82 // wxHtmlListBox creation
83 // ----------------------------------------------------------------------------
85 void wxHtmlListBox::Init()
88 m_cache
= new wxHtmlListBoxCache
;
91 bool wxHtmlListBox::Create(wxWindow
*parent
,
99 return wxVListBox::Create(parent
, id
, pos
, size
, countItems
, style
, name
);
102 wxHtmlListBox::~wxHtmlListBox()
107 delete m_htmlParser
->GetDC();
112 // ----------------------------------------------------------------------------
113 // wxHtmlListBox items markup
114 // ----------------------------------------------------------------------------
116 wxString
wxHtmlListBox::OnGetItemMarkup(size_t n
) const
118 // we don't even need to wrap the value returned by OnGetItem() inside
119 // "<html><body>" and "</body></html>" because wxHTML can parse it even
120 // without these tags
124 void wxHtmlListBox::CacheItem(size_t n
) const
126 if ( !m_cache
->Has(n
) )
130 wxHtmlListBox
*self
= wxConstCast(this, wxHtmlListBox
);
132 self
->m_htmlParser
= new wxHtmlWinParser
;
133 m_htmlParser
->SetDC(new wxClientDC(self
));
136 wxHtmlContainerCell
*cell
= (wxHtmlContainerCell
*)m_htmlParser
->
137 Parse(OnGetItemMarkup(n
));
138 wxCHECK_RET( cell
, _T("wxHtmlParser::Parse() returned NULL?") );
140 cell
->Layout(GetClientSize().x
);
142 m_cache
->Store(n
, cell
);
146 // ----------------------------------------------------------------------------
147 // wxHtmlListBox implementation of wxVListBox pure virtuals
148 // ----------------------------------------------------------------------------
150 void wxHtmlListBox::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
154 wxHtmlCell
*cell
= m_cache
->Get(n
);
155 wxCHECK_RET( cell
, _T("this cell should be cached!") );
157 // draw the selected cell in selected state
160 wxHtmlSelection htmlSel
;
161 htmlSel
.Set(wxPoint(0, 0), cell
, wxPoint(INT_MAX
, INT_MAX
), cell
);
162 wxHtmlRenderingState
htmlRendState(&htmlSel
);
163 htmlRendState
.SetSelectionState(wxHTML_SEL_IN
);
164 cell
->Draw(dc
, rect
.x
, rect
.y
, 0, INT_MAX
, htmlRendState
);
168 // note that we can't stop drawing exactly at the window boundary as then
169 // even the visible cells part could be not drawn, so always draw the
171 wxHtmlRenderingState
htmlRendState(NULL
);
172 cell
->Draw(dc
, rect
.x
, rect
.y
, 0, INT_MAX
, htmlRendState
);
176 wxCoord
wxHtmlListBox::OnMeasureItem(size_t n
) const
180 wxHtmlCell
*cell
= m_cache
->Get(n
);
181 wxCHECK_MSG( cell
, 0, _T("this cell should be cached!") );
183 return cell
->GetHeight() + cell
->GetDescent();