]>
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"
28 #include "wx/dcclient.h"
31 #include "wx/htmllbox.h"
33 #include "wx/html/htmlcell.h"
34 #include "wx/html/winpars.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // this class is used by wxHtmlListBox to cache the parsed representation of
41 // the items to avoid doing it anew each time an item must be drawn
43 // TODO: extend the class to cache more than item
44 class wxHtmlListBoxCache
47 wxHtmlListBoxCache() { m_cell
= NULL
; }
48 ~wxHtmlListBoxCache() { delete m_cell
; }
50 // returns true if we already have this item cached
51 bool Has(size_t n
) const { return m_cell
&& n
== m_item
; }
53 // ensure that the item is cached
54 void Store(size_t n
, wxHtmlCell
*cell
)
62 // return the cached cell for this index or NULL if none
63 wxHtmlCell
*Get(size_t n
) const
65 // we could be reading uninitialized m_item here but the code is still
67 return n
== m_item
? m_cell
: NULL
;
71 // the parsed representation of the cached item or NULL
74 // the index of the currently cached item (only valid if m_cell != NULL)
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
83 // wxHtmlListBox creation
84 // ----------------------------------------------------------------------------
86 void wxHtmlListBox::Init()
89 m_cache
= new wxHtmlListBoxCache
;
92 bool wxHtmlListBox::Create(wxWindow
*parent
,
100 return wxVListBox::Create(parent
, id
, pos
, size
, countItems
, style
, name
);
103 wxHtmlListBox::~wxHtmlListBox()
108 delete m_htmlParser
->GetDC();
113 // ----------------------------------------------------------------------------
114 // wxHtmlListBox items markup
115 // ----------------------------------------------------------------------------
117 wxString
wxHtmlListBox::OnGetItemMarkup(size_t n
) const
119 // we don't even need to wrap the value returned by OnGetItem() inside
120 // "<html><body>" and "</body></html>" because wxHTML can parse it even
121 // without these tags
125 void wxHtmlListBox::CacheItem(size_t n
) const
127 if ( !m_cache
->Has(n
) )
131 wxHtmlListBox
*self
= wxConstCast(this, wxHtmlListBox
);
133 self
->m_htmlParser
= new wxHtmlWinParser
;
134 m_htmlParser
->SetDC(new wxClientDC(self
));
137 wxHtmlContainerCell
*cell
= (wxHtmlContainerCell
*)m_htmlParser
->
138 Parse(OnGetItemMarkup(n
));
139 wxCHECK_RET( cell
, _T("wxHtmlParser::Parse() returned NULL?") );
141 cell
->Layout(GetClientSize().x
);
143 m_cache
->Store(n
, cell
);
147 // ----------------------------------------------------------------------------
148 // wxHtmlListBox implementation of wxVListBox pure virtuals
149 // ----------------------------------------------------------------------------
151 void wxHtmlListBox::OnDrawItem(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
155 wxHtmlCell
*cell
= m_cache
->Get(n
);
156 wxCHECK_RET( cell
, _T("this cell should be cached!") );
158 // draw the selected cell in selected state
161 wxHtmlSelection htmlSel
;
162 htmlSel
.Set(wxPoint(0, 0), cell
, wxPoint(INT_MAX
, INT_MAX
), cell
);
163 wxHtmlRenderingState
htmlRendState(&htmlSel
);
164 htmlRendState
.SetSelectionState(wxHTML_SEL_IN
);
165 cell
->Draw(dc
, rect
.x
, rect
.y
, 0, INT_MAX
, htmlRendState
);
169 // note that we can't stop drawing exactly at the window boundary as then
170 // even the visible cells part could be not drawn, so always draw the
172 wxHtmlRenderingState
htmlRendState(NULL
);
173 cell
->Draw(dc
, rect
.x
, rect
.y
, 0, INT_MAX
, htmlRendState
);
177 wxCoord
wxHtmlListBox::OnMeasureItem(size_t n
) const
181 wxHtmlCell
*cell
= m_cache
->Get(n
);
182 wxCHECK_MSG( cell
, 0, _T("this cell should be cached!") );
184 return cell
->GetHeight() + cell
->GetDescent();