]> git.saurik.com Git - wxWidgets.git/blame - src/generic/htmllbox.cpp
Fixed a rare wxAuiFloatingFrame dtor crash on MSW using a registration mechanism...
[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
9ebb7cad
VZ
49const wxChar wxHtmlListBoxNameStr[] = wxT("htmlListBox");
50const wxChar wxSimpleHtmlListBoxNameStr[] = wxT("simpleHtmlListBox");
51
9a9b4940 52// ============================================================================
e0c6027b 53// private classes
9a9b4940
VZ
54// ============================================================================
55
56// ----------------------------------------------------------------------------
57// wxHtmlListBoxCache
e0c6027b
VZ
58// ----------------------------------------------------------------------------
59
60// this class is used by wxHtmlListBox to cache the parsed representation of
61// the items to avoid doing it anew each time an item must be drawn
e0c6027b
VZ
62class wxHtmlListBoxCache
63{
aead8a4e
VZ
64private:
65 // invalidate a single item, used by Clear() and InvalidateRange()
66 void InvalidateItem(size_t n)
67 {
68 m_items[n] = (size_t)-1;
69 delete m_cells[n];
70 m_cells[n] = NULL;
71 }
72
e0c6027b 73public:
5ecdc7ab
VZ
74 wxHtmlListBoxCache()
75 {
76 for ( size_t n = 0; n < SIZE; n++ )
77 {
78 m_items[n] = (size_t)-1;
79 m_cells[n] = NULL;
80 }
e0c6027b 81
5ecdc7ab
VZ
82 m_next = 0;
83 }
e0c6027b 84
5ecdc7ab 85 ~wxHtmlListBoxCache()
e0c6027b 86 {
5ecdc7ab
VZ
87 for ( size_t n = 0; n < SIZE; n++ )
88 {
89 delete m_cells[n];
90 }
91 }
e0c6027b 92
5ecdc7ab
VZ
93 // completely invalidate the cache
94 void Clear()
95 {
96 for ( size_t n = 0; n < SIZE; n++ )
97 {
aead8a4e 98 InvalidateItem(n);
5ecdc7ab 99 }
e0c6027b
VZ
100 }
101
102 // return the cached cell for this index or NULL if none
5ecdc7ab
VZ
103 wxHtmlCell *Get(size_t item) const
104 {
105 for ( size_t n = 0; n < SIZE; n++ )
106 {
107 if ( m_items[n] == item )
108 return m_cells[n];
109 }
110
111 return NULL;
112 }
113
114 // returns true if we already have this item cached
115 bool Has(size_t item) const { return Get(item) != NULL; }
116
117 // ensure that the item is cached
118 void Store(size_t item, wxHtmlCell *cell)
e0c6027b 119 {
5ecdc7ab
VZ
120 delete m_cells[m_next];
121 m_cells[m_next] = cell;
122 m_items[m_next] = item;
123
124 // advance to the next item wrapping around if there are no more
125 if ( ++m_next == SIZE )
126 m_next = 0;
e0c6027b
VZ
127 }
128
aead8a4e
VZ
129 // forget the cached value of the item(s) between the given ones (inclusive)
130 void InvalidateRange(size_t from, size_t to)
131 {
132 for ( size_t n = 0; n < SIZE; n++ )
133 {
134 if ( m_items[n] >= from && m_items[n] <= to )
135 {
136 InvalidateItem(n);
137 }
138 }
139 }
140
e0c6027b 141private:
5ecdc7ab
VZ
142 // the max number of the items we cache
143 enum { SIZE = 50 };
144
145 // the index of the LRU (oldest) cell
146 size_t m_next;
147
e0c6027b 148 // the parsed representation of the cached item or NULL
5ecdc7ab 149 wxHtmlCell *m_cells[SIZE];
e0c6027b 150
5ecdc7ab
VZ
151 // the index of the currently cached item (only valid if m_cells != NULL)
152 size_t m_items[SIZE];
e0c6027b
VZ
153};
154
9a9b4940
VZ
155// ----------------------------------------------------------------------------
156// wxHtmlListBoxStyle
157// ----------------------------------------------------------------------------
158
159// just forward wxDefaultHtmlRenderingStyle callbacks to the main class so that
160// they could be overridden by the user code
161class wxHtmlListBoxStyle : public wxDefaultHtmlRenderingStyle
162{
163public:
fbfb8bcc 164 wxHtmlListBoxStyle(const wxHtmlListBox& hlbox) : m_hlbox(hlbox) { }
9a9b4940
VZ
165
166 virtual wxColour GetSelectedTextColour(const wxColour& colFg)
167 {
168 return m_hlbox.GetSelectedTextColour(colFg);
169 }
170
171 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg)
172 {
173 return m_hlbox.GetSelectedTextBgColour(colBg);
174 }
175
176private:
177 const wxHtmlListBox& m_hlbox;
27d0dcd0
VZ
178
179 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle)
9a9b4940
VZ
180};
181
5ecdc7ab
VZ
182// ----------------------------------------------------------------------------
183// event tables
184// ----------------------------------------------------------------------------
185
186BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox)
187 EVT_SIZE(wxHtmlListBox::OnSize)
bc55e31b
VS
188 EVT_MOTION(wxHtmlListBox::OnMouseMove)
189 EVT_LEFT_DOWN(wxHtmlListBox::OnLeftDown)
5ecdc7ab
VZ
190END_EVENT_TABLE()
191
e0c6027b
VZ
192// ============================================================================
193// implementation
194// ============================================================================
195
0c8392ca
RD
196IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox)
197
198
e0c6027b
VZ
199// ----------------------------------------------------------------------------
200// wxHtmlListBox creation
201// ----------------------------------------------------------------------------
202
bc55e31b
VS
203wxHtmlListBox::wxHtmlListBox()
204 : wxHtmlWindowMouseHelper(this)
205{
206 Init();
207}
208
209// normal constructor which calls Create() internally
210wxHtmlListBox::wxHtmlListBox(wxWindow *parent,
211 wxWindowID id,
212 const wxPoint& pos,
213 const wxSize& size,
214 long style,
215 const wxString& name)
216 : wxHtmlWindowMouseHelper(this)
217{
218 Init();
219
220 (void)Create(parent, id, pos, size, style, name);
221}
222
e0c6027b
VZ
223void wxHtmlListBox::Init()
224{
225 m_htmlParser = NULL;
9a9b4940 226 m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
e0c6027b
VZ
227 m_cache = new wxHtmlListBoxCache;
228}
229
230bool wxHtmlListBox::Create(wxWindow *parent,
231 wxWindowID id,
232 const wxPoint& pos,
233 const wxSize& size,
e0c6027b
VZ
234 long style,
235 const wxString& name)
236{
43e319a3 237 return wxVListBox::Create(parent, id, pos, size, style, name);
e0c6027b
VZ
238}
239
240wxHtmlListBox::~wxHtmlListBox()
241{
242 delete m_cache;
9a9b4940 243
e0c6027b
VZ
244 if ( m_htmlParser )
245 {
246 delete m_htmlParser->GetDC();
247 delete m_htmlParser;
248 }
9a9b4940
VZ
249
250 delete m_htmlRendStyle;
251}
252
253// ----------------------------------------------------------------------------
254// wxHtmlListBox appearance
255// ----------------------------------------------------------------------------
256
257wxColour wxHtmlListBox::GetSelectedTextColour(const wxColour& colFg) const
258{
259 return m_htmlRendStyle->
260 wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg);
261}
262
263wxColour
264wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const
265{
266 return GetSelectionBackground();
e0c6027b
VZ
267}
268
269// ----------------------------------------------------------------------------
270// wxHtmlListBox items markup
271// ----------------------------------------------------------------------------
272
273wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const
274{
275 // we don't even need to wrap the value returned by OnGetItem() inside
276 // "<html><body>" and "</body></html>" because wxHTML can parse it even
277 // without these tags
278 return OnGetItem(n);
279}
280
5ecdc7ab
VZ
281// ----------------------------------------------------------------------------
282// wxHtmlListBox cache handling
283// ----------------------------------------------------------------------------
284
e0c6027b
VZ
285void wxHtmlListBox::CacheItem(size_t n) const
286{
287 if ( !m_cache->Has(n) )
288 {
289 if ( !m_htmlParser )
290 {
291 wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox);
292
bc55e31b 293 self->m_htmlParser = new wxHtmlWinParser(self);
e0c6027b 294 m_htmlParser->SetDC(new wxClientDC(self));
2d814c19 295 m_htmlParser->SetFS(&self->m_filesystem);
67339f7d 296#if !wxUSE_UNICODE
85d3d198
JS
297 if (GetFont().Ok())
298 m_htmlParser->SetInputEncoding(GetFont().GetEncoding());
67339f7d 299#endif
f987cbb1
VS
300 // use system's default GUI font by default:
301 m_htmlParser->SetStandardFonts();
e0c6027b
VZ
302 }
303
304 wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser->
305 Parse(OnGetItemMarkup(n));
306 wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") );
307
bc55e31b
VS
308 // set the cell's ID to item's index so that CellCoordsToPhysical()
309 // can quickly find the item:
f1560fa6 310 cell->SetId(wxString::Format(_T("%lu"), (unsigned long)n));
bc55e31b 311
5ecdc7ab 312 cell->Layout(GetClientSize().x - 2*GetMargins().x);
e0c6027b
VZ
313
314 m_cache->Store(n, cell);
315 }
316}
317
5ecdc7ab
VZ
318void wxHtmlListBox::OnSize(wxSizeEvent& event)
319{
320 // we need to relayout all the cached cells
321 m_cache->Clear();
322
323 event.Skip();
324}
325
e02c72fa 326void wxHtmlListBox::RefreshRow(size_t line)
aead8a4e
VZ
327{
328 m_cache->InvalidateRange(line, line);
329
f18eaf26 330 wxVListBox::RefreshRow(line);
aead8a4e
VZ
331}
332
e02c72fa 333void wxHtmlListBox::RefreshRows(size_t from, size_t to)
aead8a4e
VZ
334{
335 m_cache->InvalidateRange(from, to);
336
f18eaf26 337 wxVListBox::RefreshRows(from, to);
aead8a4e
VZ
338}
339
5ecdc7ab
VZ
340void wxHtmlListBox::RefreshAll()
341{
342 m_cache->Clear();
343
344 wxVListBox::RefreshAll();
345}
346
03495767
VZ
347void wxHtmlListBox::SetItemCount(size_t count)
348{
349 // the items are going to change, forget the old ones
350 m_cache->Clear();
351
352 wxVListBox::SetItemCount(count);
353}
354
e0c6027b
VZ
355// ----------------------------------------------------------------------------
356// wxHtmlListBox implementation of wxVListBox pure virtuals
357// ----------------------------------------------------------------------------
358
359void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
360{
361 CacheItem(n);
362
363 wxHtmlCell *cell = m_cache->Get(n);
364 wxCHECK_RET( cell, _T("this cell should be cached!") );
365
901b583c 366 wxHtmlRenderingInfo htmlRendInfo;
5ecdc7ab 367
5ecdc7ab
VZ
368 // note that we can't stop drawing exactly at the window boundary as then
369 // even the visible cells part could be not drawn, so always draw the
370 // entire cell
bc55e31b
VS
371 cell->Draw(dc,
372 rect.x + CELL_BORDER, rect.y + CELL_BORDER,
373 0, INT_MAX, htmlRendInfo);
e0c6027b
VZ
374}
375
376wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const
377{
378 CacheItem(n);
379
380 wxHtmlCell *cell = m_cache->Get(n);
381 wxCHECK_MSG( cell, 0, _T("this cell should be cached!") );
382
d3017584 383 return cell->GetHeight() + cell->GetDescent() + 4;
e0c6027b
VZ
384}
385
bc55e31b
VS
386// ----------------------------------------------------------------------------
387// wxHtmlListBox implementation of wxHtmlListBoxWinInterface
388// ----------------------------------------------------------------------------
389
390void wxHtmlListBox::SetHTMLWindowTitle(const wxString& WXUNUSED(title))
391{
392 // nothing to do
393}
394
395void wxHtmlListBox::OnHTMLLinkClicked(const wxHtmlLinkInfo& link)
396{
397 OnLinkClicked(GetItemForCell(link.GetHtmlCell()), link);
398}
399
a3b5cead
VS
400void wxHtmlListBox::OnLinkClicked(size_t WXUNUSED(n),
401 const wxHtmlLinkInfo& link)
402{
403 wxHtmlLinkEvent event(GetId(), link);
404 GetEventHandler()->ProcessEvent(event);
405}
406
bc55e31b
VS
407wxHtmlOpeningStatus
408wxHtmlListBox::OnHTMLOpeningURL(wxHtmlURLType WXUNUSED(type),
409 const wxString& WXUNUSED(url),
410 wxString *WXUNUSED(redirect)) const
411{
412 return wxHTML_OPEN;
413}
414
415wxPoint wxHtmlListBox::HTMLCoordsToWindow(wxHtmlCell *cell,
416 const wxPoint& pos) const
417{
418 return CellCoordsToPhysical(pos, cell);
419}
420
421wxWindow* wxHtmlListBox::GetHTMLWindow() { return this; }
422
423wxColour wxHtmlListBox::GetHTMLBackgroundColour() const
424{
425 return GetBackgroundColour();
426}
427
428void wxHtmlListBox::SetHTMLBackgroundColour(const wxColour& WXUNUSED(clr))
429{
430 // nothing to do
431}
432
433void wxHtmlListBox::SetHTMLBackgroundImage(const wxBitmap& WXUNUSED(bmpBg))
434{
435 // nothing to do
436}
437
438void wxHtmlListBox::SetHTMLStatusText(const wxString& WXUNUSED(text))
439{
440 // nothing to do
441}
442
88a1b648
VS
443wxCursor wxHtmlListBox::GetHTMLCursor(HTMLCursor type) const
444{
445 // we don't want to show text selection cursor in listboxes
446 if (type == HTMLCursor_Text)
447 return wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor_Default);
448
449 // in all other cases, use the same cursor as wxHtmlWindow:
450 return wxHtmlWindow::GetDefaultHTMLCursor(type);
451}
452
bc55e31b
VS
453// ----------------------------------------------------------------------------
454// wxHtmlListBox handling of HTML links
455// ----------------------------------------------------------------------------
aead8a4e 456
bc55e31b
VS
457wxPoint wxHtmlListBox::GetRootCellCoords(size_t n) const
458{
459 wxPoint pos(CELL_BORDER, CELL_BORDER);
460 pos += GetMargins();
e02c72fa 461 pos.y += GetRowsHeight(GetVisibleBegin(), n);
bc55e31b
VS
462 return pos;
463}
464
465bool wxHtmlListBox::PhysicalCoordsToCell(wxPoint& pos, wxHtmlCell*& cell) const
466{
10368bff 467 int n = VirtualHitTest(pos.y);
bc55e31b
VS
468 if ( n == wxNOT_FOUND )
469 return false;
470
471 // convert mouse coordinates to coords relative to item's wxHtmlCell:
472 pos -= GetRootCellCoords(n);
473
474 CacheItem(n);
475 cell = m_cache->Get(n);
476
477 return true;
478}
479
480size_t wxHtmlListBox::GetItemForCell(const wxHtmlCell *cell) const
481{
482 wxCHECK_MSG( cell, 0, _T("no cell") );
483
484 cell = cell->GetRootCell();
485
486 wxCHECK_MSG( cell, 0, _T("no root cell") );
487
488 // the cell's ID contains item index, see CacheItem():
489 unsigned long n;
490 if ( !cell->GetId().ToULong(&n) )
491 {
492 wxFAIL_MSG( _T("unexpected root cell's ID") );
493 return 0;
494 }
495
496 return n;
497}
498
499wxPoint
500wxHtmlListBox::CellCoordsToPhysical(const wxPoint& pos, wxHtmlCell *cell) const
501{
502 return pos + GetRootCellCoords(GetItemForCell(cell));
503}
504
505void wxHtmlListBox::OnInternalIdle()
506{
507 wxVListBox::OnInternalIdle();
508
509 if ( wxHtmlWindowMouseHelper::DidMouseMove() )
510 {
511 wxPoint pos = ScreenToClient(wxGetMousePosition());
512 wxHtmlCell *cell;
513
514 if ( !PhysicalCoordsToCell(pos, cell) )
515 return;
516
517 wxHtmlWindowMouseHelper::HandleIdle(cell, pos);
518 }
519}
520
521void wxHtmlListBox::OnMouseMove(wxMouseEvent& event)
522{
523 wxHtmlWindowMouseHelper::HandleMouseMoved();
524 event.Skip();
525}
526
527void wxHtmlListBox::OnLeftDown(wxMouseEvent& event)
528{
529 wxPoint pos = event.GetPosition();
530 wxHtmlCell *cell;
531
532 if ( !PhysicalCoordsToCell(pos, cell) )
533 {
534 event.Skip();
535 return;
536 }
537
538 if ( !wxHtmlWindowMouseHelper::HandleMouseClick(cell, pos, event) )
539 {
540 // no link was clicked, so let the listbox code handle the click (e.g.
541 // by selecting another item in the list):
542 event.Skip();
543 }
544}
545
9ebb7cad
VZ
546
547// ----------------------------------------------------------------------------
548// wxSimpleHtmlListBox
549// ----------------------------------------------------------------------------
550
551bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id,
552 const wxPoint& pos,
553 const wxSize& size,
554 int n, const wxString choices[],
555 long style,
556 const wxValidator& validator,
557 const wxString& name)
558{
559 if (!wxHtmlListBox::Create(parent, id, pos, size, style, name))
560 return false;
561
9a9b5822 562#if wxUSE_VALIDATORS
9ebb7cad 563 SetValidator(validator);
9a9b5822 564#endif
a236aa20
VZ
565
566 Append(n, choices);
9ebb7cad
VZ
567
568 return true;
569}
570
571bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id,
a236aa20
VZ
572 const wxPoint& pos,
573 const wxSize& size,
574 const wxArrayString& choices,
575 long style,
576 const wxValidator& validator,
577 const wxString& name)
9ebb7cad
VZ
578{
579 if (!wxHtmlListBox::Create(parent, id, pos, size, style, name))
580 return false;
581
9a9b5822 582#if wxUSE_VALIDATORS
9ebb7cad 583 SetValidator(validator);
9a9b5822 584#endif
a236aa20 585
9ebb7cad
VZ
586 Append(choices);
587
588 return true;
589}
590
591wxSimpleHtmlListBox::~wxSimpleHtmlListBox()
592{
a236aa20 593 wxItemContainer::Clear();
9ebb7cad
VZ
594}
595
a236aa20 596void wxSimpleHtmlListBox::DoClear()
9ebb7cad 597{
a236aa20
VZ
598 wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount());
599
9ebb7cad 600 m_items.Clear();
9d8f8138 601 m_HTMLclientData.Clear();
a236aa20 602
9ebb7cad
VZ
603 UpdateCount();
604}
605
e52cd41e
VZ
606void wxSimpleHtmlListBox::Clear()
607{
608 DoClear();
609}
610
a236aa20 611void wxSimpleHtmlListBox::DoDeleteOneItem(unsigned int n)
9ebb7cad
VZ
612{
613 m_items.RemoveAt(n);
a236aa20 614
9d8f8138 615 m_HTMLclientData.RemoveAt(n);
9ebb7cad 616
9ebb7cad
VZ
617 UpdateCount();
618}
619
a236aa20
VZ
620int wxSimpleHtmlListBox::DoInsertItems(const wxArrayStringsAdapter& items,
621 unsigned int pos,
622 void **clientData,
623 wxClientDataType type)
9ebb7cad 624{
a236aa20
VZ
625 const unsigned int count = items.GetCount();
626
627 m_items.Insert(wxEmptyString, pos, count);
628 m_HTMLclientData.Insert(NULL, pos, count);
629
630 for ( unsigned int i = 0; i < count; ++i, ++pos )
631 {
632 m_items[pos] = items[i];
633 AssignNewItemClientData(pos, clientData, i, type);
634 }
9ebb7cad 635
9ebb7cad 636 UpdateCount();
a236aa20 637
9ebb7cad
VZ
638 return pos;
639}
640
641void wxSimpleHtmlListBox::SetString(unsigned int n, const wxString& s)
642{
643 wxCHECK_RET( IsValid(n),
644 wxT("invalid index in wxSimpleHtmlListBox::SetString") );
645
a236aa20 646 m_items[n]=s;
e02c72fa 647 RefreshRow(n);
9ebb7cad
VZ
648}
649
650wxString wxSimpleHtmlListBox::GetString(unsigned int n) const
651{
652 wxCHECK_MSG( IsValid(n), wxEmptyString,
653 wxT("invalid index in wxSimpleHtmlListBox::GetString") );
654
655 return m_items[n];
656}
657
658void wxSimpleHtmlListBox::UpdateCount()
659{
9d8f8138 660 wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount());
9ebb7cad
VZ
661 wxHtmlListBox::SetItemCount(m_items.GetCount());
662
663 // very small optimization: if you need to add lot of items to
664 // a wxSimpleHtmlListBox be sure to use the
665 // wxSimpleHtmlListBox::Append(const wxArrayString&) method instead!
666 if (!this->IsFrozen())
667 RefreshAll();
668}
669
bc55e31b 670#endif // wxUSE_HTML