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