]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/htmllbox.cpp
moving embedding to common API
[wxWidgets.git] / src / generic / htmllbox.cpp
... / ...
CommitLineData
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>
9// License: wxWindows license
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
28 #include "wx/dcclient.h"
29#endif //WX_PRECOMP
30
31#if wxUSE_HTML
32
33#include "wx/htmllbox.h"
34
35#include "wx/html/htmlcell.h"
36#include "wx/html/winpars.h"
37
38// this hack forces the linker to always link in m_* files
39#include "wx/html/forcelnk.h"
40FORCE_WXHTML_MODULES()
41
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
46// small border always added to the cells:
47static const wxCoord CELL_BORDER = 2;
48
49const wxChar wxHtmlListBoxNameStr[] = wxT("htmlListBox");
50const wxChar wxSimpleHtmlListBoxNameStr[] = wxT("simpleHtmlListBox");
51
52// ============================================================================
53// private classes
54// ============================================================================
55
56// ----------------------------------------------------------------------------
57// wxHtmlListBoxCache
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
62class wxHtmlListBoxCache
63{
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
73public:
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 }
81
82 m_next = 0;
83 }
84
85 ~wxHtmlListBoxCache()
86 {
87 for ( size_t n = 0; n < SIZE; n++ )
88 {
89 delete m_cells[n];
90 }
91 }
92
93 // completely invalidate the cache
94 void Clear()
95 {
96 for ( size_t n = 0; n < SIZE; n++ )
97 {
98 InvalidateItem(n);
99 }
100 }
101
102 // return the cached cell for this index or NULL if none
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)
119 {
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;
127 }
128
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
141private:
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
148 // the parsed representation of the cached item or NULL
149 wxHtmlCell *m_cells[SIZE];
150
151 // the index of the currently cached item (only valid if m_cells != NULL)
152 size_t m_items[SIZE];
153};
154
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:
164 wxHtmlListBoxStyle(const wxHtmlListBox& hlbox) : m_hlbox(hlbox) { }
165
166 virtual wxColour GetSelectedTextColour(const wxColour& colFg)
167 {
168 // by default wxHtmlListBox doesn't implement GetSelectedTextColour()
169 // and returns wxNullColour from it, so use the default HTML colour for
170 // selection
171 wxColour col = m_hlbox.GetSelectedTextColour(colFg);
172 if ( !col.IsOk() )
173 {
174 col = wxDefaultHtmlRenderingStyle::GetSelectedTextColour(colFg);
175 }
176
177 return col;
178 }
179
180 virtual wxColour GetSelectedTextBgColour(const wxColour& colBg)
181 {
182 wxColour col = m_hlbox.GetSelectedTextBgColour(colBg);
183 if ( !col.IsOk() )
184 {
185 col = wxDefaultHtmlRenderingStyle::GetSelectedTextBgColour(colBg);
186 }
187
188 return col;
189 }
190
191private:
192 const wxHtmlListBox& m_hlbox;
193
194 DECLARE_NO_COPY_CLASS(wxHtmlListBoxStyle)
195};
196
197// ----------------------------------------------------------------------------
198// event tables
199// ----------------------------------------------------------------------------
200
201BEGIN_EVENT_TABLE(wxHtmlListBox, wxVListBox)
202 EVT_SIZE(wxHtmlListBox::OnSize)
203 EVT_MOTION(wxHtmlListBox::OnMouseMove)
204 EVT_LEFT_DOWN(wxHtmlListBox::OnLeftDown)
205END_EVENT_TABLE()
206
207// ============================================================================
208// implementation
209// ============================================================================
210
211IMPLEMENT_ABSTRACT_CLASS(wxHtmlListBox, wxVListBox)
212
213
214// ----------------------------------------------------------------------------
215// wxHtmlListBox creation
216// ----------------------------------------------------------------------------
217
218wxHtmlListBox::wxHtmlListBox()
219 : wxHtmlWindowMouseHelper(this)
220{
221 Init();
222}
223
224// normal constructor which calls Create() internally
225wxHtmlListBox::wxHtmlListBox(wxWindow *parent,
226 wxWindowID id,
227 const wxPoint& pos,
228 const wxSize& size,
229 long style,
230 const wxString& name)
231 : wxHtmlWindowMouseHelper(this)
232{
233 Init();
234
235 (void)Create(parent, id, pos, size, style, name);
236}
237
238void wxHtmlListBox::Init()
239{
240 m_htmlParser = NULL;
241 m_htmlRendStyle = new wxHtmlListBoxStyle(*this);
242 m_cache = new wxHtmlListBoxCache;
243}
244
245bool wxHtmlListBox::Create(wxWindow *parent,
246 wxWindowID id,
247 const wxPoint& pos,
248 const wxSize& size,
249 long style,
250 const wxString& name)
251{
252 return wxVListBox::Create(parent, id, pos, size, style, name);
253}
254
255wxHtmlListBox::~wxHtmlListBox()
256{
257 delete m_cache;
258
259 if ( m_htmlParser )
260 {
261 delete m_htmlParser->GetDC();
262 delete m_htmlParser;
263 }
264
265 delete m_htmlRendStyle;
266}
267
268// ----------------------------------------------------------------------------
269// wxHtmlListBox appearance
270// ----------------------------------------------------------------------------
271
272wxColour
273wxHtmlListBox::GetSelectedTextColour(const wxColour& WXUNUSED(colFg)) const
274{
275 return wxNullColour;
276}
277
278wxColour
279wxHtmlListBox::GetSelectedTextBgColour(const wxColour& WXUNUSED(colBg)) const
280{
281 return GetSelectionBackground();
282}
283
284// ----------------------------------------------------------------------------
285// wxHtmlListBox items markup
286// ----------------------------------------------------------------------------
287
288wxString wxHtmlListBox::OnGetItemMarkup(size_t n) const
289{
290 // we don't even need to wrap the value returned by OnGetItem() inside
291 // "<html><body>" and "</body></html>" because wxHTML can parse it even
292 // without these tags
293 return OnGetItem(n);
294}
295
296// ----------------------------------------------------------------------------
297// wxHtmlListBox cache handling
298// ----------------------------------------------------------------------------
299
300void wxHtmlListBox::CacheItem(size_t n) const
301{
302 if ( !m_cache->Has(n) )
303 {
304 if ( !m_htmlParser )
305 {
306 wxHtmlListBox *self = wxConstCast(this, wxHtmlListBox);
307
308 self->m_htmlParser = new wxHtmlWinParser(self);
309 m_htmlParser->SetDC(new wxClientDC(self));
310 m_htmlParser->SetFS(&self->m_filesystem);
311#if !wxUSE_UNICODE
312 if (GetFont().Ok())
313 m_htmlParser->SetInputEncoding(GetFont().GetEncoding());
314#endif
315 // use system's default GUI font by default:
316 m_htmlParser->SetStandardFonts();
317 }
318
319 wxHtmlContainerCell *cell = (wxHtmlContainerCell *)m_htmlParser->
320 Parse(OnGetItemMarkup(n));
321 wxCHECK_RET( cell, _T("wxHtmlParser::Parse() returned NULL?") );
322
323 // set the cell's ID to item's index so that CellCoordsToPhysical()
324 // can quickly find the item:
325 cell->SetId(wxString::Format(_T("%lu"), (unsigned long)n));
326
327 cell->Layout(GetClientSize().x - 2*GetMargins().x);
328
329 m_cache->Store(n, cell);
330 }
331}
332
333void wxHtmlListBox::OnSize(wxSizeEvent& event)
334{
335 // we need to relayout all the cached cells
336 m_cache->Clear();
337
338 event.Skip();
339}
340
341void wxHtmlListBox::RefreshRow(size_t line)
342{
343 m_cache->InvalidateRange(line, line);
344
345 wxVListBox::RefreshRow(line);
346}
347
348void wxHtmlListBox::RefreshRows(size_t from, size_t to)
349{
350 m_cache->InvalidateRange(from, to);
351
352 wxVListBox::RefreshRows(from, to);
353}
354
355void wxHtmlListBox::RefreshAll()
356{
357 m_cache->Clear();
358
359 wxVListBox::RefreshAll();
360}
361
362void wxHtmlListBox::SetItemCount(size_t count)
363{
364 // the items are going to change, forget the old ones
365 m_cache->Clear();
366
367 wxVListBox::SetItemCount(count);
368}
369
370// ----------------------------------------------------------------------------
371// wxHtmlListBox implementation of wxVListBox pure virtuals
372// ----------------------------------------------------------------------------
373
374void
375wxHtmlListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
376{
377 if ( IsSelected(n) )
378 {
379 if ( DoDrawSolidBackground
380 (
381 GetSelectedTextBgColour(GetBackgroundColour()),
382 dc,
383 rect,
384 n
385 ) )
386 {
387 return;
388 }
389 //else: no custom selection background colour, use base class version
390 }
391
392 wxVListBox::OnDrawBackground(dc, rect, n);
393}
394
395void wxHtmlListBox::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
396{
397 CacheItem(n);
398
399 wxHtmlCell *cell = m_cache->Get(n);
400 wxCHECK_RET( cell, _T("this cell should be cached!") );
401
402 wxHtmlRenderingInfo htmlRendInfo;
403
404 // draw the selected cell in selected state ourselves if we're using custom
405 // colours (to test for this, check the callbacks by passing them any dummy
406 // (but valid, to avoid asserts) colour):
407 if ( IsSelected(n) &&
408 (GetSelectedTextColour(*wxBLACK).IsOk() ||
409 GetSelectedTextBgColour(*wxWHITE).IsOk()) )
410 {
411 wxHtmlSelection htmlSel;
412 htmlSel.Set(wxPoint(0,0), cell, wxPoint(INT_MAX, INT_MAX), cell);
413 htmlRendInfo.SetSelection(&htmlSel);
414 htmlRendInfo.SetStyle(m_htmlRendStyle);
415 htmlRendInfo.GetState().SetSelectionState(wxHTML_SEL_IN);
416 }
417 //else: normal item or selected item with default colours, its background
418 // was already taken care of in the base class
419
420 // note that we can't stop drawing exactly at the window boundary as then
421 // even the visible cells part could be not drawn, so always draw the
422 // entire cell
423 cell->Draw(dc,
424 rect.x + CELL_BORDER, rect.y + CELL_BORDER,
425 0, INT_MAX, htmlRendInfo);
426}
427
428wxCoord wxHtmlListBox::OnMeasureItem(size_t n) const
429{
430 CacheItem(n);
431
432 wxHtmlCell *cell = m_cache->Get(n);
433 wxCHECK_MSG( cell, 0, _T("this cell should be cached!") );
434
435 return cell->GetHeight() + cell->GetDescent() + 4;
436}
437
438// ----------------------------------------------------------------------------
439// wxHtmlListBox implementation of wxHtmlListBoxWinInterface
440// ----------------------------------------------------------------------------
441
442void wxHtmlListBox::SetHTMLWindowTitle(const wxString& WXUNUSED(title))
443{
444 // nothing to do
445}
446
447void wxHtmlListBox::OnHTMLLinkClicked(const wxHtmlLinkInfo& link)
448{
449 OnLinkClicked(GetItemForCell(link.GetHtmlCell()), link);
450}
451
452void wxHtmlListBox::OnLinkClicked(size_t WXUNUSED(n),
453 const wxHtmlLinkInfo& link)
454{
455 wxHtmlLinkEvent event(GetId(), link);
456 GetEventHandler()->ProcessEvent(event);
457}
458
459wxHtmlOpeningStatus
460wxHtmlListBox::OnHTMLOpeningURL(wxHtmlURLType WXUNUSED(type),
461 const wxString& WXUNUSED(url),
462 wxString *WXUNUSED(redirect)) const
463{
464 return wxHTML_OPEN;
465}
466
467wxPoint wxHtmlListBox::HTMLCoordsToWindow(wxHtmlCell *cell,
468 const wxPoint& pos) const
469{
470 return CellCoordsToPhysical(pos, cell);
471}
472
473wxWindow* wxHtmlListBox::GetHTMLWindow() { return this; }
474
475wxColour wxHtmlListBox::GetHTMLBackgroundColour() const
476{
477 return GetBackgroundColour();
478}
479
480void wxHtmlListBox::SetHTMLBackgroundColour(const wxColour& WXUNUSED(clr))
481{
482 // nothing to do
483}
484
485void wxHtmlListBox::SetHTMLBackgroundImage(const wxBitmap& WXUNUSED(bmpBg))
486{
487 // nothing to do
488}
489
490void wxHtmlListBox::SetHTMLStatusText(const wxString& WXUNUSED(text))
491{
492 // nothing to do
493}
494
495wxCursor wxHtmlListBox::GetHTMLCursor(HTMLCursor type) const
496{
497 // we don't want to show text selection cursor in listboxes
498 if (type == HTMLCursor_Text)
499 return wxHtmlWindow::GetDefaultHTMLCursor(HTMLCursor_Default);
500
501 // in all other cases, use the same cursor as wxHtmlWindow:
502 return wxHtmlWindow::GetDefaultHTMLCursor(type);
503}
504
505// ----------------------------------------------------------------------------
506// wxHtmlListBox handling of HTML links
507// ----------------------------------------------------------------------------
508
509wxPoint wxHtmlListBox::GetRootCellCoords(size_t n) const
510{
511 wxPoint pos(CELL_BORDER, CELL_BORDER);
512 pos += GetMargins();
513 pos.y += GetRowsHeight(GetVisibleBegin(), n);
514 return pos;
515}
516
517bool wxHtmlListBox::PhysicalCoordsToCell(wxPoint& pos, wxHtmlCell*& cell) const
518{
519 int n = VirtualHitTest(pos.y);
520 if ( n == wxNOT_FOUND )
521 return false;
522
523 // convert mouse coordinates to coords relative to item's wxHtmlCell:
524 pos -= GetRootCellCoords(n);
525
526 CacheItem(n);
527 cell = m_cache->Get(n);
528
529 return true;
530}
531
532size_t wxHtmlListBox::GetItemForCell(const wxHtmlCell *cell) const
533{
534 wxCHECK_MSG( cell, 0, _T("no cell") );
535
536 cell = cell->GetRootCell();
537
538 wxCHECK_MSG( cell, 0, _T("no root cell") );
539
540 // the cell's ID contains item index, see CacheItem():
541 unsigned long n;
542 if ( !cell->GetId().ToULong(&n) )
543 {
544 wxFAIL_MSG( _T("unexpected root cell's ID") );
545 return 0;
546 }
547
548 return n;
549}
550
551wxPoint
552wxHtmlListBox::CellCoordsToPhysical(const wxPoint& pos, wxHtmlCell *cell) const
553{
554 return pos + GetRootCellCoords(GetItemForCell(cell));
555}
556
557void wxHtmlListBox::OnInternalIdle()
558{
559 wxVListBox::OnInternalIdle();
560
561 if ( wxHtmlWindowMouseHelper::DidMouseMove() )
562 {
563 wxPoint pos = ScreenToClient(wxGetMousePosition());
564 wxHtmlCell *cell;
565
566 if ( !PhysicalCoordsToCell(pos, cell) )
567 return;
568
569 wxHtmlWindowMouseHelper::HandleIdle(cell, pos);
570 }
571}
572
573void wxHtmlListBox::OnMouseMove(wxMouseEvent& event)
574{
575 wxHtmlWindowMouseHelper::HandleMouseMoved();
576 event.Skip();
577}
578
579void wxHtmlListBox::OnLeftDown(wxMouseEvent& event)
580{
581 wxPoint pos = event.GetPosition();
582 wxHtmlCell *cell;
583
584 if ( !PhysicalCoordsToCell(pos, cell) )
585 {
586 event.Skip();
587 return;
588 }
589
590 if ( !wxHtmlWindowMouseHelper::HandleMouseClick(cell, pos, event) )
591 {
592 // no link was clicked, so let the listbox code handle the click (e.g.
593 // by selecting another item in the list):
594 event.Skip();
595 }
596}
597
598
599// ----------------------------------------------------------------------------
600// wxSimpleHtmlListBox
601// ----------------------------------------------------------------------------
602
603bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id,
604 const wxPoint& pos,
605 const wxSize& size,
606 int n, const wxString choices[],
607 long style,
608 const wxValidator& validator,
609 const wxString& name)
610{
611 if (!wxHtmlListBox::Create(parent, id, pos, size, style, name))
612 return false;
613
614#if wxUSE_VALIDATORS
615 SetValidator(validator);
616#endif
617
618 Append(n, choices);
619
620 return true;
621}
622
623bool wxSimpleHtmlListBox::Create(wxWindow *parent, wxWindowID id,
624 const wxPoint& pos,
625 const wxSize& size,
626 const wxArrayString& choices,
627 long style,
628 const wxValidator& validator,
629 const wxString& name)
630{
631 if (!wxHtmlListBox::Create(parent, id, pos, size, style, name))
632 return false;
633
634#if wxUSE_VALIDATORS
635 SetValidator(validator);
636#endif
637
638 Append(choices);
639
640 return true;
641}
642
643wxSimpleHtmlListBox::~wxSimpleHtmlListBox()
644{
645 wxItemContainer::Clear();
646}
647
648void wxSimpleHtmlListBox::DoClear()
649{
650 wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount());
651
652 m_items.Clear();
653 m_HTMLclientData.Clear();
654
655 UpdateCount();
656}
657
658void wxSimpleHtmlListBox::Clear()
659{
660 DoClear();
661}
662
663void wxSimpleHtmlListBox::DoDeleteOneItem(unsigned int n)
664{
665 m_items.RemoveAt(n);
666
667 m_HTMLclientData.RemoveAt(n);
668
669 UpdateCount();
670}
671
672int wxSimpleHtmlListBox::DoInsertItems(const wxArrayStringsAdapter& items,
673 unsigned int pos,
674 void **clientData,
675 wxClientDataType type)
676{
677 const unsigned int count = items.GetCount();
678
679 m_items.Insert(wxEmptyString, pos, count);
680 m_HTMLclientData.Insert(NULL, pos, count);
681
682 for ( unsigned int i = 0; i < count; ++i, ++pos )
683 {
684 m_items[pos] = items[i];
685 AssignNewItemClientData(pos, clientData, i, type);
686 }
687
688 UpdateCount();
689
690 return pos;
691}
692
693void wxSimpleHtmlListBox::SetString(unsigned int n, const wxString& s)
694{
695 wxCHECK_RET( IsValid(n),
696 wxT("invalid index in wxSimpleHtmlListBox::SetString") );
697
698 m_items[n]=s;
699 RefreshRow(n);
700}
701
702wxString wxSimpleHtmlListBox::GetString(unsigned int n) const
703{
704 wxCHECK_MSG( IsValid(n), wxEmptyString,
705 wxT("invalid index in wxSimpleHtmlListBox::GetString") );
706
707 return m_items[n];
708}
709
710void wxSimpleHtmlListBox::UpdateCount()
711{
712 wxASSERT(m_items.GetCount() == m_HTMLclientData.GetCount());
713 wxHtmlListBox::SetItemCount(m_items.GetCount());
714
715 // very small optimization: if you need to add lot of items to
716 // a wxSimpleHtmlListBox be sure to use the
717 // wxSimpleHtmlListBox::Append(const wxArrayString&) method instead!
718 if (!this->IsFrozen())
719 RefreshAll();
720}
721
722#endif // wxUSE_HTML