]> git.saurik.com Git - wxWidgets.git/blame - src/generic/htmllbox.cpp
Include wx/slider.h according to precompiled headers of wx/wx.h (with other minor...
[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
bc55e31b
VS
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46// small border always added to the cells:
47static const wxCoord CELL_BORDER = 2;
48
9a9b4940 49// ============================================================================
e0c6027b 50// private classes
9a9b4940
VZ
51// ============================================================================
52
53// ----------------------------------------------------------------------------
54// wxHtmlListBoxCache
e0c6027b
VZ
55// ----------------------------------------------------------------------------
56
57// this class is used by wxHtmlListBox to cache the parsed representation of
58// the items to avoid doing it anew each time an item must be drawn
e0c6027b
VZ
59class wxHtmlListBoxCache
60{
aead8a4e
VZ
61private:
62 // invalidate a single item, used by Clear() and InvalidateRange()
63 void InvalidateItem(size_t n)
64 {
65 m_items[n] = (size_t)-1;
66 delete m_cells[n];
67 m_cells[n] = NULL;
68 }
69
e0c6027b 70public:
5ecdc7ab
VZ
71 wxHtmlListBoxCache()
72 {
73 for ( size_t n = 0; n < SIZE; n++ )
74 {
75 m_items[n] = (size_t)-1;
76 m_cells[n] = NULL;
77 }
e0c6027b 78
5ecdc7ab
VZ
79 m_next = 0;
80 }
e0c6027b 81
5ecdc7ab 82 ~wxHtmlListBoxCache()
e0c6027b 83 {
5ecdc7ab
VZ
84 for ( size_t n = 0; n < SIZE; n++ )
85 {
86 delete m_cells[n];
87 }
88 }
e0c6027b 89
5ecdc7ab
VZ
90 // completely invalidate the cache
91 void Clear()
92 {
93 for ( size_t n = 0; n < SIZE; n++ )
94 {
aead8a4e 95 InvalidateItem(n);
5ecdc7ab 96 }
e0c6027b
VZ
97 }
98
99 // return the cached cell for this index or NULL if none
5ecdc7ab
VZ
100 wxHtmlCell *Get(size_t item) const
101 {
102 for ( size_t n = 0; n < SIZE; n++ )
103 {
104 if ( m_items[n] == item )
105 return m_cells[n];
106 }
107
108 return NULL;
109 }
110
111 // returns true if we already have this item cached
112 bool Has(size_t item) const { return Get(item) != NULL; }
113
114 // ensure that the item is cached
115 void Store(size_t item, wxHtmlCell *cell)
e0c6027b 116 {
5ecdc7ab
VZ
117 delete m_cells[m_next];
118 m_cells[m_next] = cell;
119 m_items[m_next] = item;
120
121 // advance to the next item wrapping around if there are no more
122 if ( ++m_next == SIZE )
123 m_next = 0;
e0c6027b
VZ
124 }
125
aead8a4e
VZ
126 // forget the cached value of the item(s) between the given ones (inclusive)
127 void InvalidateRange(size_t from, size_t to)
128 {
129 for ( size_t n = 0; n < SIZE; n++ )
130 {
131 if ( m_items[n] >= from && m_items[n] <= to )
132 {
133 InvalidateItem(n);
134 }
135 }
136 }
137
e0c6027b 138private:
5ecdc7ab
VZ
139 // the max number of the items we cache
140 enum { SIZE = 50 };
141
142 // the index of the LRU (oldest) cell
143 size_t m_next;
144
e0c6027b 145 // the parsed representation of the cached item or NULL
5ecdc7ab 146 wxHtmlCell *m_cells[SIZE];
e0c6027b 147
5ecdc7ab
VZ
148 // the index of the currently cached item (only valid if m_cells != NULL)
149 size_t m_items[SIZE];
e0c6027b
VZ
150};
151
9a9b4940
VZ
152// ----------------------------------------------------------------------------
153// wxHtmlListBoxStyle
154// ----------------------------------------------------------------------------
155
156// just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that
157// they could be overridden by the user code
158class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle
159{
160public:
fbfb8bcc 161 wxHtmlListBoxStyle(const wxHtmlListBox& hlbox) : m_hlbox(hlbox) { }
9a9b4940
VZ
162
163 virtual wxColour GetSelectedTextColour(const wxColour& colFg)
164 {
165 return m_hlbox.GetSelectedTextColour(colFg);
166 }
167
168 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg)
169 {
170 return m_hlbox.GetSelectedTextBgColour(colBg);
171 }
172
173private:
174 const wxHtmlListBox& m_hlbox;
27d0dcd0
VZ
175
176 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle)
9a9b4940
VZ
177};
178
5ecdc7ab
VZ
179// ----------------------------------------------------------------------------
180// event tables
181// ----------------------------------------------------------------------------
182
183BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox)
184 EVT_SIZE(wxHtmlListBox::OnSize)
bc55e31b
VS
185 EVT_MOTION(wxHtmlListBox::OnMouseMove)
186 EVT_LEFT_DOWN(wxHtmlListBox::OnLeftDown)
5ecdc7ab
VZ
187END_EVENT_TABLE()
188
e0c6027b
VZ
189// ============================================================================
190// implementation
191// ============================================================================
192
0c8392ca
RD
193IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox)
194
195
e0c6027b
VZ
196// ----------------------------------------------------------------------------
197// wxHtmlListBox creation
198// ----------------------------------------------------------------------------
199
bc55e31b
VS
200wxHtmlListBox::wxHtmlListBox()
201 : wxHtmlWindowMouseHelper(this)
202{
203 Init();
204}
205
206// normal constructor which calls Create() internally
207wxHtmlListBox::wxHtmlListBox(wxWindow *parent,
208 wxWindowID id,
209 const wxPoint& pos,
210 const wxSize& size,
211 long style,
212 const wxString& name)
213 : wxHtmlWindowMouseHelper(this)
214{
215 Init();
216
217 (void)Create(parent, id, pos, size, style, name);
218}
219
e0c6027b
VZ
220void wxHtmlListBox::Init()
221{
222 m_htmlParser = NULL;
9a9b4940 223 m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
e0c6027b
VZ
224 m_cache = new wxHtmlListBoxCache;
225}
226
227bool wxHtmlListBox::Create(wxWindow *parent,
228 wxWindowID id,
229 const wxPoint& pos,
230 const wxSize& size,
e0c6027b
VZ
231 long style,
232 const wxString& name)
233{
43e319a3 234 return wxVListBox::Create(parent, id, pos, size, style, name);
e0c6027b
VZ
235}
236
237wxHtmlListBox::~wxHtmlListBox()
238{
239 delete m_cache;
9a9b4940 240
e0c6027b
VZ
241 if ( m_htmlParser )
242 {
243 delete m_htmlParser->GetDC();
244 delete m_htmlParser;
245 }
9a9b4940
VZ
246
247 delete m_htmlRendStyle;
248}
249
250// ----------------------------------------------------------------------------
251// wxHtmlListBox appearance
252// ----------------------------------------------------------------------------
253
254wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
255{
256 return m_htmlRendStyle->
257 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg);
258}
259
260wxColour
261wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const
262{
263 return GetSelectionBackground();
e0c6027b
VZ
264}
265
266// ----------------------------------------------------------------------------
267// wxHtmlListBox items markup
268// ----------------------------------------------------------------------------
269
270wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const
271{
272 // we don't even need to wrap the value returned by OnGetItem() inside
273 // "<html><body>" and "</body></html>" because wxHTML can parse it even
274 // without these tags
275 return OnGetItem(n);
276}
277
5ecdc7ab
VZ
278// ----------------------------------------------------------------------------
279// wxHtmlListBox cache handling
280// ----------------------------------------------------------------------------
281
e0c6027b
VZ
282void wxHtmlListBox::CacheItem(size_t n) const
283{
284 if ( !m_cache->Has(n) )
285 {
286 if ( !m_htmlParser )
287 {
288 wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox);
289
bc55e31b 290 self->m_htmlParser = new wxHtmlWinParser(self);
e0c6027b 291 m_htmlParser->SetDC(new wxClientDC(self));
2d814c19 292 m_htmlParser->SetFS(&self->m_filesystem);
f987cbb1
VS
293
294 // use system's default GUI font by default:
295 m_htmlParser->SetStandardFonts();
e0c6027b
VZ
296 }
297
298 wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser->
299 Parse(OnGetItemMarkup(n));
300 wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") );
301
bc55e31b
VS
302 // set the cell's ID to item's index so that CellCoordsToPhysical()
303 // can quickly find the item:
f1560fa6 304 cell->SetId(wxString::Format(_T("%lu"), (unsigned long)n));
bc55e31b 305
5ecdc7ab 306 cell->Layout(GetClientSize().x - 2*GetMargins().x);
e0c6027b
VZ
307
308 m_cache->Store(n, cell);
309 }
310}
311
5ecdc7ab
VZ
312void wxHtmlListBox::OnSize(wxSizeEvent& event)
313{
314 // we need to relayout all the cached cells
315 m_cache->Clear();
316
317 event.Skip();
318}
319
aead8a4e
VZ
320void wxHtmlListBox::RefreshLine(size_t line)
321{
322 m_cache->InvalidateRange(line, line);
323
324 wxVListBox::RefreshLine(line);
325}
326
327void wxHtmlListBox::RefreshLines(size_t from, size_t to)
328{
329 m_cache->InvalidateRange(from, to);
330
331 wxVListBox::RefreshLines(from, to);
332}
333
5ecdc7ab
VZ
334void wxHtmlListBox::RefreshAll()
335{
336 m_cache->Clear();
337
338 wxVListBox::RefreshAll();
339}
340
03495767
VZ
341void wxHtmlListBox::SetItemCount(size_t count)
342{
343 // the items are going to change, forget the old ones
344 m_cache->Clear();
345
346 wxVListBox::SetItemCount(count);
347}
348
e0c6027b
VZ
349// ----------------------------------------------------------------------------
350// wxHtmlListBox implementation of wxVListBox pure virtuals
351// ----------------------------------------------------------------------------
352
353void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
354{
355 CacheItem(n);
356
357 wxHtmlCell *cell = m_cache->Get(n);
358 wxCHECK_RET( cell, _T("this cell should be cached!") );
359
901b583c 360 wxHtmlRenderingInfo htmlRendInfo;
5ecdc7ab 361
e0c6027b
VZ
362 // draw the selected cell in selected state
363 if ( IsSelected(n) )
364 {
365 wxHtmlSelection htmlSel;
c47addef 366 htmlSel.Set(wxPoint(0,0), cell, wxPoint(INT_MAX, INT_MAX), cell);
901b583c 367 htmlRendInfo.SetSelection(&htmlSel);
9a9b4940
VZ
368 if ( m_htmlRendStyle )
369 htmlRendInfo.SetStyle(m_htmlRendStyle);
370 htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN);
e0c6027b 371 }
5ecdc7ab
VZ
372
373 // note that we can't stop drawing exactly at the window boundary as then
374 // even the visible cells part could be not drawn, so always draw the
375 // entire cell
bc55e31b
VS
376 cell->Draw(dc,
377 rect.x + CELL_BORDER, rect.y + CELL_BORDER,
378 0, INT_MAX, htmlRendInfo);
e0c6027b
VZ
379}
380
381wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const
382{
383 CacheItem(n);
384
385 wxHtmlCell *cell = m_cache->Get(n);
386 wxCHECK_MSG( cell, 0, _T("this cell should be cached!") );
387
d3017584 388 return cell->GetHeight() + cell->GetDescent() + 4;
e0c6027b
VZ
389}
390
bc55e31b
VS
391// ----------------------------------------------------------------------------
392// wxHtmlListBox implementation of wxHtmlListBoxWinInterface
393// ----------------------------------------------------------------------------
394
395void wxHtmlListBox::SetHTMLWindowTitle(const wxString& WXUNUSED(title))
396{
397 // nothing to do
398}
399
400void wxHtmlListBox::OnHTMLLinkClicked(const wxHtmlLinkInfo& link)
401{
402 OnLinkClicked(GetItemForCell(link.GetHtmlCell()), link);
403}
404
405wxHtmlOpeningStatus
406wxHtmlListBox::OnHTMLOpeningURL(wxHtmlURLType WXUNUSED(type),
407 const wxString& WXUNUSED(url),
408 wxString *WXUNUSED(redirect)) const
409{
410 return wxHTML_OPEN;
411}
412
413wxPoint wxHtmlListBox::HTMLCoordsToWindow(wxHtmlCell *cell,
414 const wxPoint& pos) const
415{
416 return CellCoordsToPhysical(pos, cell);
417}
418
419wxWindow* wxHtmlListBox::GetHTMLWindow() { return this; }
420
421wxColour wxHtmlListBox::GetHTMLBackgroundColour() const
422{
423 return GetBackgroundColour();
424}
425
426void wxHtmlListBox::SetHTMLBackgroundColour(const wxColour& WXUNUSED(clr))
427{
428 // nothing to do
429}
430
431void wxHtmlListBox::SetHTMLBackgroundImage(const wxBitmap& WXUNUSED(bmpBg))
432{
433 // nothing to do
434}
435
436void wxHtmlListBox::SetHTMLStatusText(const wxString& WXUNUSED(text))
437{
438 // nothing to do
439}
440
88a1b648
VS
441wxCursor wxHtmlListBox::GetHTMLCursor(HTMLCursor type) const
442{
443 // we don't want to show text selection cursor in listboxes
444 if (type == HTMLCursor_Text)
445 return wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor_Default);
446
447 // in all other cases, use the same cursor as wxHtmlWindow:
448 return wxHtmlWindow::GetDefaultHTMLCursor(type);
449}
450
bc55e31b
VS
451// ----------------------------------------------------------------------------
452// wxHtmlListBox handling of HTML links
453// ----------------------------------------------------------------------------
aead8a4e 454
bc55e31b
VS
455wxPoint wxHtmlListBox::GetRootCellCoords(size_t n) const
456{
457 wxPoint pos(CELL_BORDER, CELL_BORDER);
458 pos += GetMargins();
459 pos.y += GetLinesHeight(GetFirstVisibleLine(), n);
460 return pos;
461}
462
463bool wxHtmlListBox::PhysicalCoordsToCell(wxPoint& pos, wxHtmlCell*& cell) const
464{
465 int n = HitTest(pos);
466 if ( n == wxNOT_FOUND )
467 return false;
468
469 // convert mouse coordinates to coords relative to item's wxHtmlCell:
470 pos -= GetRootCellCoords(n);
471
472 CacheItem(n);
473 cell = m_cache->Get(n);
474
475 return true;
476}
477
478size_t wxHtmlListBox::GetItemForCell(const wxHtmlCell *cell) const
479{
480 wxCHECK_MSG( cell, 0, _T("no cell") );
481
482 cell = cell->GetRootCell();
483
484 wxCHECK_MSG( cell, 0, _T("no root cell") );
485
486 // the cell's ID contains item index, see CacheItem():
487 unsigned long n;
488 if ( !cell->GetId().ToULong(&n) )
489 {
490 wxFAIL_MSG( _T("unexpected root cell's ID") );
491 return 0;
492 }
493
494 return n;
495}
496
497wxPoint
498wxHtmlListBox::CellCoordsToPhysical(const wxPoint& pos, wxHtmlCell *cell) const
499{
500 return pos + GetRootCellCoords(GetItemForCell(cell));
501}
502
503void wxHtmlListBox::OnInternalIdle()
504{
505 wxVListBox::OnInternalIdle();
506
507 if ( wxHtmlWindowMouseHelper::DidMouseMove() )
508 {
509 wxPoint pos = ScreenToClient(wxGetMousePosition());
510 wxHtmlCell *cell;
511
512 if ( !PhysicalCoordsToCell(pos, cell) )
513 return;
514
515 wxHtmlWindowMouseHelper::HandleIdle(cell, pos);
516 }
517}
518
519void wxHtmlListBox::OnMouseMove(wxMouseEvent& event)
520{
521 wxHtmlWindowMouseHelper::HandleMouseMoved();
522 event.Skip();
523}
524
525void wxHtmlListBox::OnLeftDown(wxMouseEvent& event)
526{
527 wxPoint pos = event.GetPosition();
528 wxHtmlCell *cell;
529
530 if ( !PhysicalCoordsToCell(pos, cell) )
531 {
532 event.Skip();
533 return;
534 }
535
536 if ( !wxHtmlWindowMouseHelper::HandleMouseClick(cell, pos, event) )
537 {
538 // no link was clicked, so let the listbox code handle the click (e.g.
539 // by selecting another item in the list):
540 event.Skip();
541 }
542}
543
544#endif // wxUSE_HTML