]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
Fix VC6 compilation after r73880.
[wxWidgets.git] / src / html / htmlcell.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlcell.cpp
3 // Purpose: wxHtmlCell - basic element of HTML output
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #include "wx/dynarray.h"
20 #include "wx/brush.h"
21 #include "wx/colour.h"
22 #include "wx/dc.h"
23 #include "wx/settings.h"
24 #include "wx/module.h"
25 #include "wx/wxcrtvararg.h"
26 #endif
27
28 #include "wx/html/htmlcell.h"
29 #include "wx/html/htmlwin.h"
30
31 #include <stdlib.h>
32
33 //-----------------------------------------------------------------------------
34 // Helper classes
35 //-----------------------------------------------------------------------------
36
37 void wxHtmlSelection::Set(const wxPoint& fromPos, const wxHtmlCell *fromCell,
38 const wxPoint& toPos, const wxHtmlCell *toCell)
39 {
40 m_fromCell = fromCell;
41 m_toCell = toCell;
42 m_fromPos = fromPos;
43 m_toPos = toPos;
44 }
45
46 void wxHtmlSelection::Set(const wxHtmlCell *fromCell, const wxHtmlCell *toCell)
47 {
48 wxPoint p1 = fromCell ? fromCell->GetAbsPos() : wxDefaultPosition;
49 wxPoint p2 = toCell ? toCell->GetAbsPos() : wxDefaultPosition;
50 if ( toCell )
51 {
52 p2.x += toCell->GetWidth();
53 p2.y += toCell->GetHeight();
54 }
55 Set(p1, fromCell, p2, toCell);
56 }
57
58 wxColour
59 wxDefaultHtmlRenderingStyle::
60 GetSelectedTextColour(const wxColour& WXUNUSED(clr))
61 {
62 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
63 }
64
65 wxColour
66 wxDefaultHtmlRenderingStyle::
67 GetSelectedTextBgColour(const wxColour& WXUNUSED(clr))
68 {
69 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
70 }
71
72
73 //-----------------------------------------------------------------------------
74 // wxHtmlCell
75 //-----------------------------------------------------------------------------
76
77 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell, wxObject)
78
79 wxHtmlCell::wxHtmlCell() : wxObject()
80 {
81 m_Next = NULL;
82 m_Parent = NULL;
83 m_Width = m_Height = m_Descent = 0;
84 m_ScriptMode = wxHTML_SCRIPT_NORMAL; // <sub> or <sup> mode
85 m_ScriptBaseline = 0; // <sub> or <sup> baseline
86 m_CanLiveOnPagebreak = true;
87 m_Link = NULL;
88 }
89
90 wxHtmlCell::~wxHtmlCell()
91 {
92 delete m_Link;
93 }
94
95 // Update the descent value when whe are in a <sub> or <sup>.
96 // prevbase is the parent base
97 void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode, long previousBase)
98 {
99 m_ScriptMode = mode;
100
101 if (mode == wxHTML_SCRIPT_SUP)
102 m_ScriptBaseline = previousBase - (m_Height + 1) / 2;
103 else if (mode == wxHTML_SCRIPT_SUB)
104 m_ScriptBaseline = previousBase + (m_Height + 1) / 6;
105 else
106 m_ScriptBaseline = 0;
107
108 m_Descent += m_ScriptBaseline;
109 }
110
111 #if WXWIN_COMPATIBILITY_2_6
112
113 struct wxHtmlCellOnMouseClickCompatHelper;
114
115 static wxHtmlCellOnMouseClickCompatHelper *gs_helperOnMouseClick = NULL;
116
117 // helper for routing calls to new ProcessMouseClick() method to deprecated
118 // OnMouseClick() method
119 struct wxHtmlCellOnMouseClickCompatHelper
120 {
121 wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface *window_,
122 const wxPoint& pos_,
123 const wxMouseEvent& event_)
124 : window(window_), pos(pos_), event(event_), retval(false)
125 {
126 }
127
128 bool CallOnMouseClick(wxHtmlCell *cell)
129 {
130 wxHtmlCellOnMouseClickCompatHelper *oldHelper = gs_helperOnMouseClick;
131 gs_helperOnMouseClick = this;
132 cell->OnMouseClick
133 (
134 window ? window->GetHTMLWindow() : NULL,
135 pos.x, pos.y,
136 event
137 );
138 gs_helperOnMouseClick = oldHelper;
139 return retval;
140 }
141
142 wxHtmlWindowInterface *window;
143 const wxPoint& pos;
144 const wxMouseEvent& event;
145 bool retval;
146 };
147 #endif // WXWIN_COMPATIBILITY_2_6
148
149 bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface *window,
150 const wxPoint& pos,
151 const wxMouseEvent& event)
152 {
153 wxCHECK_MSG( window, false, wxT("window interface must be provided") );
154
155 #if WXWIN_COMPATIBILITY_2_6
156 // NB: this hack puts the body of ProcessMouseClick() into OnMouseClick()
157 // (for which it has to pass the arguments and return value via a
158 // helper variable because these two methods have different
159 // signatures), so that old code overriding OnMouseClick will continue
160 // to work
161 wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event);
162 return compat.CallOnMouseClick(this);
163 }
164
165 void wxHtmlCell::OnMouseClick(wxWindow *, int, int, const wxMouseEvent& event)
166 {
167 wxCHECK_RET( gs_helperOnMouseClick, wxT("unexpected call to OnMouseClick") );
168 wxHtmlWindowInterface *window = gs_helperOnMouseClick->window;
169 const wxPoint& pos = gs_helperOnMouseClick->pos;
170 #endif // WXWIN_COMPATIBILITY_2_6
171
172 wxHtmlLinkInfo *lnk = GetLink(pos.x, pos.y);
173 bool retval = false;
174
175 if (lnk)
176 {
177 wxHtmlLinkInfo lnk2(*lnk);
178 lnk2.SetEvent(&event);
179 lnk2.SetHtmlCell(this);
180
181 window->OnHTMLLinkClicked(lnk2);
182 retval = true;
183 }
184
185 #if WXWIN_COMPATIBILITY_2_6
186 gs_helperOnMouseClick->retval = retval;
187 #else
188 return retval;
189 #endif // WXWIN_COMPATIBILITY_2_6
190 }
191
192 #if WXWIN_COMPATIBILITY_2_6
193 wxCursor wxHtmlCell::GetCursor() const
194 {
195 return wxNullCursor;
196 }
197 #endif // WXWIN_COMPATIBILITY_2_6
198
199 wxCursor wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface *window) const
200 {
201 #if WXWIN_COMPATIBILITY_2_6
202 // NB: Older versions of wx used GetCursor() virtual method in place of
203 // GetMouseCursor(interface). This code ensures that user code that
204 // overridden GetCursor() continues to work. The trick is that the base
205 // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we
206 // know that GetCursor() was overridden iff it returns valid cursor.
207 wxCursor cur = GetCursor();
208 if (cur.IsOk())
209 return cur;
210 #endif // WXWIN_COMPATIBILITY_2_6
211
212 if ( GetLink() )
213 {
214 return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link);
215 }
216 else
217 {
218 return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default);
219 }
220 }
221
222
223 bool
224 wxHtmlCell::AdjustPagebreak(int *pagebreak,
225 const wxArrayInt& WXUNUSED(known_pagebreaks),
226 int pageHeight) const
227 {
228 // Notice that we always break the cells bigger than the page height here
229 // as otherwise we wouldn't be able to break them at all.
230 if ( m_Height <= pageHeight &&
231 (!m_CanLiveOnPagebreak &&
232 m_PosY < *pagebreak && m_PosY + m_Height > *pagebreak) )
233 {
234 *pagebreak = m_PosY;
235 return true;
236 }
237
238 return false;
239 }
240
241
242
243 void wxHtmlCell::SetLink(const wxHtmlLinkInfo& link)
244 {
245 wxDELETE(m_Link);
246 if (link.GetHref() != wxEmptyString)
247 m_Link = new wxHtmlLinkInfo(link);
248 }
249
250
251 void wxHtmlCell::Layout(int WXUNUSED(w))
252 {
253 SetPos(0, 0);
254 }
255
256
257
258 const wxHtmlCell* wxHtmlCell::Find(int WXUNUSED(condition), const void* WXUNUSED(param)) const
259 {
260 return NULL;
261 }
262
263
264 wxHtmlCell *wxHtmlCell::FindCellByPos(wxCoord x, wxCoord y,
265 unsigned flags) const
266 {
267 if ( x >= 0 && x < m_Width && y >= 0 && y < m_Height )
268 {
269 return wxConstCast(this, wxHtmlCell);
270 }
271 else
272 {
273 if ((flags & wxHTML_FIND_NEAREST_AFTER) &&
274 (y < 0 || (y < 0+m_Height && x < 0+m_Width)))
275 return wxConstCast(this, wxHtmlCell);
276 else if ((flags & wxHTML_FIND_NEAREST_BEFORE) &&
277 (y >= 0+m_Height || (y >= 0 && x >= 0)))
278 return wxConstCast(this, wxHtmlCell);
279 else
280 return NULL;
281 }
282 }
283
284
285 wxPoint wxHtmlCell::GetAbsPos(wxHtmlCell *rootCell) const
286 {
287 wxPoint p(m_PosX, m_PosY);
288 for (wxHtmlCell *parent = m_Parent; parent && parent != rootCell;
289 parent = parent->m_Parent)
290 {
291 p.x += parent->m_PosX;
292 p.y += parent->m_PosY;
293 }
294 return p;
295 }
296
297 wxHtmlCell *wxHtmlCell::GetRootCell() const
298 {
299 wxHtmlCell *c = wxConstCast(this, wxHtmlCell);
300 while ( c->m_Parent )
301 c = c->m_Parent;
302 return c;
303 }
304
305 unsigned wxHtmlCell::GetDepth() const
306 {
307 unsigned d = 0;
308 for (wxHtmlCell *p = m_Parent; p; p = p->m_Parent)
309 d++;
310 return d;
311 }
312
313 bool wxHtmlCell::IsBefore(wxHtmlCell *cell) const
314 {
315 const wxHtmlCell *c1 = this;
316 const wxHtmlCell *c2 = cell;
317 unsigned d1 = GetDepth();
318 unsigned d2 = cell->GetDepth();
319
320 if ( d1 > d2 )
321 for (; d1 != d2; d1-- )
322 c1 = c1->m_Parent;
323 else if ( d1 < d2 )
324 for (; d1 != d2; d2-- )
325 c2 = c2->m_Parent;
326
327 if ( cell == this )
328 return true;
329
330 while ( c1 && c2 )
331 {
332 if ( c1->m_Parent == c2->m_Parent )
333 {
334 while ( c1 )
335 {
336 if ( c1 == c2 )
337 return true;
338 c1 = c1->GetNext();
339 }
340 return false;
341 }
342 else
343 {
344 c1 = c1->m_Parent;
345 c2 = c2->m_Parent;
346 }
347 }
348
349 wxFAIL_MSG(wxT("Cells are in different trees"));
350 return false;
351 }
352
353
354 //-----------------------------------------------------------------------------
355 // wxHtmlWordCell
356 //-----------------------------------------------------------------------------
357
358 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell, wxHtmlCell)
359
360 wxHtmlWordCell::wxHtmlWordCell(const wxString& word, const wxDC& dc) : wxHtmlCell()
361 {
362 m_Word = word;
363 wxCoord w, h, d;
364 dc.GetTextExtent(m_Word, &w, &h, &d);
365 m_Width = w;
366 m_Height = h;
367 m_Descent = d;
368 SetCanLiveOnPagebreak(false);
369 m_allowLinebreak = true;
370 }
371
372 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell *cell)
373 {
374 if ( cell && m_Parent == cell->m_Parent &&
375 !wxIsspace(cell->m_Word.Last()) && !wxIsspace(m_Word[0u]) )
376 {
377 m_allowLinebreak = false;
378 }
379 }
380
381 // Splits m_Word into up to three parts according to selection, returns
382 // substring before, in and after selection and the points (in relative coords)
383 // where s2 and s3 start:
384 void wxHtmlWordCell::Split(const wxDC& dc,
385 const wxPoint& selFrom, const wxPoint& selTo,
386 unsigned& pos1, unsigned& pos2) const
387 {
388 wxPoint pt1 = (selFrom == wxDefaultPosition) ?
389 wxDefaultPosition : selFrom - GetAbsPos();
390 wxPoint pt2 = (selTo == wxDefaultPosition) ?
391 wxPoint(m_Width, wxDefaultCoord) : selTo - GetAbsPos();
392
393 // if the selection is entirely within this cell, make sure pt1 < pt2 in
394 // order to make the rest of this function simpler:
395 if ( selFrom != wxDefaultPosition && selTo != wxDefaultPosition &&
396 selFrom.x > selTo.x )
397 {
398 wxPoint tmp = pt1;
399 pt1 = pt2;
400 pt2 = tmp;
401 }
402
403 unsigned len = m_Word.length();
404 unsigned i = 0;
405 pos1 = 0;
406
407 // adjust for cases when the start/end position is completely
408 // outside the cell:
409 if ( pt1.y < 0 )
410 pt1.x = 0;
411 if ( pt2.y >= m_Height )
412 pt2.x = m_Width;
413
414 // before selection:
415 // (include character under caret only if in first half of width)
416 #ifdef __WXMAC__
417 // implementation using PartialExtents to support fractional widths
418 wxArrayInt widths ;
419 dc.GetPartialTextExtents(m_Word,widths) ;
420 while( i < len && pt1.x >= widths[i] )
421 i++ ;
422 if ( i < len )
423 {
424 int charW = (i > 0) ? widths[i] - widths[i-1] : widths[i];
425 if ( widths[i] - pt1.x < charW/2 )
426 i++;
427 }
428 #else // !__WXMAC__
429 wxCoord charW, charH;
430 while ( pt1.x > 0 && i < len )
431 {
432 dc.GetTextExtent(m_Word[i], &charW, &charH);
433 pt1.x -= charW;
434 if ( pt1.x >= -charW/2 )
435 {
436 pos1 += charW;
437 i++;
438 }
439 }
440 #endif // __WXMAC__/!__WXMAC__
441
442 // in selection:
443 // (include character under caret only if in first half of width)
444 unsigned j = i;
445 #ifdef __WXMAC__
446 while( j < len && pt2.x >= widths[j] )
447 j++ ;
448 if ( j < len )
449 {
450 int charW = (j > 0) ? widths[j] - widths[j-1] : widths[j];
451 if ( widths[j] - pt2.x < charW/2 )
452 j++;
453 }
454 #else // !__WXMAC__
455 pos2 = pos1;
456 pt2.x -= pos2;
457 while ( pt2.x > 0 && j < len )
458 {
459 dc.GetTextExtent(m_Word[j], &charW, &charH);
460 pt2.x -= charW;
461 if ( pt2.x >= -charW/2 )
462 {
463 pos2 += charW;
464 j++;
465 }
466 }
467 #endif // __WXMAC__/!__WXMAC__
468
469 pos1 = i;
470 pos2 = j;
471
472 wxASSERT( pos2 >= pos1 );
473 }
474
475 void wxHtmlWordCell::SetSelectionPrivPos(const wxDC& dc, wxHtmlSelection *s) const
476 {
477 unsigned p1, p2;
478
479 Split(dc,
480 this == s->GetFromCell() ? s->GetFromPos() : wxDefaultPosition,
481 this == s->GetToCell() ? s->GetToPos() : wxDefaultPosition,
482 p1, p2);
483
484 if ( this == s->GetFromCell() )
485 s->SetFromCharacterPos (p1); // selection starts here
486 if ( this == s->GetToCell() )
487 s->SetToCharacterPos (p2); // selection ends here
488 }
489
490
491 static void SwitchSelState(wxDC& dc, wxHtmlRenderingInfo& info,
492 bool toSelection)
493 {
494 wxColour fg = info.GetState().GetFgColour();
495 wxColour bg = info.GetState().GetBgColour();
496
497 if ( toSelection )
498 {
499 dc.SetBackgroundMode(wxSOLID);
500 dc.SetTextForeground(info.GetStyle().GetSelectedTextColour(fg));
501 dc.SetTextBackground(info.GetStyle().GetSelectedTextBgColour(bg));
502 dc.SetBackground(wxBrush(info.GetStyle().GetSelectedTextBgColour(bg),
503 wxBRUSHSTYLE_SOLID));
504 }
505 else
506 {
507 const int mode = info.GetState().GetBgMode();
508 dc.SetBackgroundMode(mode);
509 dc.SetTextForeground(fg);
510 dc.SetTextBackground(bg);
511 if ( mode != wxTRANSPARENT )
512 dc.SetBackground(wxBrush(bg, mode));
513 }
514 }
515
516
517 void wxHtmlWordCell::Draw(wxDC& dc, int x, int y,
518 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
519 wxHtmlRenderingInfo& info)
520 {
521 #if 0 // useful for debugging
522 dc.SetPen(*wxBLACK_PEN);
523 dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width /* VZ: +1? */ ,m_Height);
524 #endif
525
526 bool drawSelectionAfterCell = false;
527
528 if ( info.GetState().GetSelectionState() == wxHTML_SEL_CHANGING )
529 {
530 // Selection changing, we must draw the word piecewise:
531 wxHtmlSelection *s = info.GetSelection();
532 wxString txt;
533 int w, h;
534 int ofs = 0;
535
536 // NB: this is quite a hack: in order to compute selection boundaries
537 // (in word's characters) we must know current font, which is only
538 // possible inside rendering code. Therefore we update the
539 // information here and store it in wxHtmlSelection so that
540 // ConvertToText can use it later:
541 if ( !s->AreFromToCharacterPosSet () )
542 {
543 SetSelectionPrivPos(dc, s);
544 }
545
546 int part1 = s->GetFromCell()==this ? s->GetFromCharacterPos() : 0;
547 int part2 = s->GetToCell()==this ? s->GetToCharacterPos() : m_Word.Length();
548
549 if ( part1 > 0 )
550 {
551 txt = m_Word.Mid(0, part1);
552 dc.DrawText(txt, x + m_PosX, y + m_PosY);
553 dc.GetTextExtent(txt, &w, &h);
554 ofs += w;
555 }
556
557 SwitchSelState(dc, info, true);
558
559 txt = m_Word.Mid(part1, part2-part1);
560 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
561
562 if ( (size_t)part2 < m_Word.length() )
563 {
564 dc.GetTextExtent(txt, &w, &h);
565 ofs += w;
566 SwitchSelState(dc, info, false);
567 txt = m_Word.Mid(part2);
568 dc.DrawText(txt, ofs + x + m_PosX, y + m_PosY);
569 }
570 else
571 drawSelectionAfterCell = true;
572 }
573 else
574 {
575 wxHtmlSelectionState selstate = info.GetState().GetSelectionState();
576 // Not changing selection state, draw the word in single mode:
577 SwitchSelState(dc, info, selstate != wxHTML_SEL_OUT);
578 dc.DrawText(m_Word, x + m_PosX, y + m_PosY);
579 drawSelectionAfterCell = (selstate != wxHTML_SEL_OUT);
580 }
581
582 // NB: If the text is justified then there is usually some free space
583 // between adjacent cells and drawing the selection only onto cells
584 // would result in ugly unselected spaces. The code below detects
585 // this special case and renders the selection *outside* the sell,
586 // too.
587 if ( m_Parent->GetAlignHor() == wxHTML_ALIGN_JUSTIFY &&
588 drawSelectionAfterCell )
589 {
590 wxHtmlCell *nextCell = m_Next;
591 while ( nextCell && nextCell->IsFormattingCell() )
592 nextCell = nextCell->GetNext();
593 if ( nextCell )
594 {
595 int nextX = nextCell->GetPosX();
596 if ( m_PosX + m_Width < nextX )
597 {
598 dc.SetBrush(dc.GetBackground());
599 dc.SetPen(*wxTRANSPARENT_PEN);
600 dc.DrawRectangle(x + m_PosX + m_Width, y + m_PosY,
601 nextX - m_PosX - m_Width, m_Height);
602 }
603 }
604 }
605 }
606
607 wxCursor wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface *window) const
608 {
609 if ( !GetLink() )
610 {
611 return window->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text);
612 }
613 else
614 {
615 return wxHtmlCell::GetMouseCursor(window);
616 }
617 }
618
619 wxString wxHtmlWordCell::ConvertToText(wxHtmlSelection *s) const
620 {
621 if ( s && (this == s->GetFromCell() || this == s->GetToCell()) )
622 {
623 // VZ: we may be called before we had a chance to re-render ourselves
624 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
625 // that this only happens in case of a double/triple click (which
626 // seems to be the case now) and so it makes sense to select the
627 // entire contents of the cell in this case
628 //
629 // TODO: but this really needs to be fixed in some better way later...
630 if ( s->AreFromToCharacterPosSet() )
631 {
632 const int part1 = s->GetFromCell()==this ? s->GetFromCharacterPos() : 0;
633 const int part2 = s->GetToCell()==this ? s->GetToCharacterPos() : m_Word.Length();
634 if ( part1 == part2 )
635 return wxEmptyString;
636 return GetPartAsText(part1, part2);
637 }
638 //else: return the whole word below
639 }
640
641 return GetAllAsText();
642 }
643
644 wxString wxHtmlWordWithTabsCell::GetAllAsText() const
645 {
646 return m_wordOrig;
647 }
648
649 wxString wxHtmlWordWithTabsCell::GetPartAsText(int begin, int end) const
650 {
651 // NB: The 'begin' and 'end' positions are in the _displayed_ text
652 // (stored in m_Word) and not in the text with tabs that should
653 // be copied to clipboard (m_wordOrig).
654 //
655 // NB: Because selection is performed on displayed text, it's possible
656 // to select e.g. "half of TAB character" -- IOW, 'begin' and 'end'
657 // may be in the middle of TAB character expansion into ' 's. In this
658 // case, we copy the TAB character to clipboard once.
659
660 wxASSERT( begin < end );
661
662 const unsigned SPACES_PER_TAB = 8;
663
664 wxString sel;
665
666 int pos = 0;
667 wxString::const_iterator i = m_wordOrig.begin();
668
669 // find the beginning of text to copy:
670 for ( ; pos < begin; ++i )
671 {
672 if ( *i == '\t' )
673 {
674 pos += 8 - (m_linepos + pos) % SPACES_PER_TAB;
675 if ( pos >= begin )
676 {
677 sel += '\t';
678 }
679 }
680 else
681 {
682 ++pos;
683 }
684 }
685
686 // copy the content until we reach 'end':
687 for ( ; pos < end; ++i )
688 {
689 const wxChar c = *i;
690 sel += c;
691
692 if ( c == '\t' )
693 pos += 8 - (m_linepos + pos) % SPACES_PER_TAB;
694 else
695 ++pos;
696 }
697
698 return sel;
699 }
700
701
702
703 //-----------------------------------------------------------------------------
704 // wxHtmlContainerCell
705 //-----------------------------------------------------------------------------
706
707 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell, wxHtmlCell)
708
709 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell *parent) : wxHtmlCell()
710 {
711 m_Cells = m_LastCell = NULL;
712 m_Parent = parent;
713 m_MaxTotalWidth = 0;
714 if (m_Parent) m_Parent->InsertCell(this);
715 m_AlignHor = wxHTML_ALIGN_LEFT;
716 m_AlignVer = wxHTML_ALIGN_BOTTOM;
717 m_IndentLeft = m_IndentRight = m_IndentTop = m_IndentBottom = 0;
718 m_WidthFloat = 100; m_WidthFloatUnits = wxHTML_UNITS_PERCENT;
719 m_UseBkColour = false;
720 m_Border = 0;
721 m_MinHeight = 0;
722 m_MinHeightAlign = wxHTML_ALIGN_TOP;
723 m_LastLayout = -1;
724 }
725
726 wxHtmlContainerCell::~wxHtmlContainerCell()
727 {
728 wxHtmlCell *cell = m_Cells;
729 while ( cell )
730 {
731 wxHtmlCell *cellNext = cell->GetNext();
732 delete cell;
733 cell = cellNext;
734 }
735 }
736
737
738
739 void wxHtmlContainerCell::SetIndent(int i, int what, int units)
740 {
741 int val = (units == wxHTML_UNITS_PIXELS) ? i : -i;
742 if (what & wxHTML_INDENT_LEFT) m_IndentLeft = val;
743 if (what & wxHTML_INDENT_RIGHT) m_IndentRight = val;
744 if (what & wxHTML_INDENT_TOP) m_IndentTop = val;
745 if (what & wxHTML_INDENT_BOTTOM) m_IndentBottom = val;
746 m_LastLayout = -1;
747 }
748
749
750
751 int wxHtmlContainerCell::GetIndent(int ind) const
752 {
753 if (ind & wxHTML_INDENT_LEFT) return m_IndentLeft;
754 else if (ind & wxHTML_INDENT_RIGHT) return m_IndentRight;
755 else if (ind & wxHTML_INDENT_TOP) return m_IndentTop;
756 else if (ind & wxHTML_INDENT_BOTTOM) return m_IndentBottom;
757 else return -1; /* BUG! Should not be called... */
758 }
759
760
761
762
763 int wxHtmlContainerCell::GetIndentUnits(int ind) const
764 {
765 bool p = false;
766 if (ind & wxHTML_INDENT_LEFT) p = m_IndentLeft < 0;
767 else if (ind & wxHTML_INDENT_RIGHT) p = m_IndentRight < 0;
768 else if (ind & wxHTML_INDENT_TOP) p = m_IndentTop < 0;
769 else if (ind & wxHTML_INDENT_BOTTOM) p = m_IndentBottom < 0;
770 if (p) return wxHTML_UNITS_PERCENT;
771 else return wxHTML_UNITS_PIXELS;
772 }
773
774
775 bool
776 wxHtmlContainerCell::AdjustPagebreak(int *pagebreak,
777 const wxArrayInt& known_pagebreaks,
778 int pageHeight) const
779 {
780 if (!m_CanLiveOnPagebreak)
781 return wxHtmlCell::AdjustPagebreak(pagebreak, known_pagebreaks, pageHeight);
782
783 wxHtmlCell *c = GetFirstChild();
784 bool rt = false;
785 int pbrk = *pagebreak - m_PosY;
786
787 while (c)
788 {
789 if (c->AdjustPagebreak(&pbrk, known_pagebreaks, pageHeight))
790 rt = true;
791 c = c->GetNext();
792 }
793 if (rt)
794 *pagebreak = pbrk + m_PosY;
795 return rt;
796 }
797
798
799 void wxHtmlContainerCell::Layout(int w)
800 {
801 wxHtmlCell::Layout(w);
802
803 if (m_LastLayout == w)
804 return;
805 m_LastLayout = w;
806
807 // VS: Any attempt to layout with negative or zero width leads to hell,
808 // but we can't ignore such attempts completely, since it sometimes
809 // happen (e.g. when trying how small a table can be). The best thing we
810 // can do is to set the width of child cells to zero
811 if (w < 1)
812 {
813 m_Width = 0;
814 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
815 cell->Layout(0);
816 // this does two things: it recursively calls this code on all
817 // child contrainers and resets children's position to (0,0)
818 return;
819 }
820
821 wxHtmlCell *nextCell;
822 long xpos = 0, ypos = m_IndentTop;
823 int xdelta = 0, ybasicpos = 0, ydiff;
824 int s_width, nextWordWidth, s_indent;
825 int ysizeup = 0, ysizedown = 0;
826 int MaxLineWidth = 0;
827 int curLineWidth = 0;
828 m_MaxTotalWidth = 0;
829
830
831 /*
832
833 WIDTH ADJUSTING :
834
835 */
836
837 if (m_WidthFloatUnits == wxHTML_UNITS_PERCENT)
838 {
839 if (m_WidthFloat < 0) m_Width = (100 + m_WidthFloat) * w / 100;
840 else m_Width = m_WidthFloat * w / 100;
841 }
842 else
843 {
844 if (m_WidthFloat < 0) m_Width = w + m_WidthFloat;
845 else m_Width = m_WidthFloat;
846 }
847
848 if (m_Cells)
849 {
850 int l = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
851 int r = (m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight;
852 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
853 cell->Layout(m_Width - (l + r));
854 }
855
856 /*
857
858 LAYOUT :
859
860 */
861
862 // adjust indentation:
863 s_indent = (m_IndentLeft < 0) ? (-m_IndentLeft * m_Width / 100) : m_IndentLeft;
864 s_width = m_Width - s_indent - ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
865
866 // my own layout:
867 wxHtmlCell *cell = m_Cells,
868 *line = m_Cells;
869 while (cell != NULL)
870 {
871 switch (m_AlignVer)
872 {
873 case wxHTML_ALIGN_TOP : ybasicpos = 0; break;
874 case wxHTML_ALIGN_BOTTOM : ybasicpos = - cell->GetHeight(); break;
875 case wxHTML_ALIGN_CENTER : ybasicpos = - cell->GetHeight() / 2; break;
876 }
877 ydiff = cell->GetHeight() + ybasicpos;
878
879 if (cell->GetDescent() + ydiff > ysizedown) ysizedown = cell->GetDescent() + ydiff;
880 if (ybasicpos + cell->GetDescent() < -ysizeup) ysizeup = - (ybasicpos + cell->GetDescent());
881
882 // layout nonbreakable run of cells:
883 cell->SetPos(xpos, ybasicpos + cell->GetDescent());
884 xpos += cell->GetWidth();
885 if (!cell->IsTerminalCell())
886 {
887 // Container cell indicates new line
888 if (curLineWidth > m_MaxTotalWidth)
889 m_MaxTotalWidth = curLineWidth;
890
891 if (wxMax(cell->GetWidth(), cell->GetMaxTotalWidth()) > m_MaxTotalWidth)
892 m_MaxTotalWidth = cell->GetMaxTotalWidth();
893 curLineWidth = 0;
894 }
895 else
896 // Normal cell, add maximum cell width to line width
897 curLineWidth += cell->GetMaxTotalWidth();
898
899 cell = cell->GetNext();
900
901 // compute length of the next word that would be added:
902 nextWordWidth = 0;
903 if (cell)
904 {
905 nextCell = cell;
906 do
907 {
908 nextWordWidth += nextCell->GetWidth();
909 nextCell = nextCell->GetNext();
910 } while (nextCell && !nextCell->IsLinebreakAllowed());
911 }
912
913 // force new line if occurred:
914 if ((cell == NULL) ||
915 (xpos + nextWordWidth > s_width && cell->IsLinebreakAllowed()))
916 {
917 if (xpos > MaxLineWidth) MaxLineWidth = xpos;
918 if (ysizeup < 0) ysizeup = 0;
919 if (ysizedown < 0) ysizedown = 0;
920 switch (m_AlignHor) {
921 case wxHTML_ALIGN_LEFT :
922 case wxHTML_ALIGN_JUSTIFY :
923 xdelta = 0;
924 break;
925 case wxHTML_ALIGN_RIGHT :
926 xdelta = 0 + (s_width - xpos);
927 break;
928 case wxHTML_ALIGN_CENTER :
929 xdelta = 0 + (s_width - xpos) / 2;
930 break;
931 }
932 if (xdelta < 0) xdelta = 0;
933 xdelta += s_indent;
934
935 ypos += ysizeup;
936
937 if (m_AlignHor != wxHTML_ALIGN_JUSTIFY || cell == NULL)
938 {
939 while (line != cell)
940 {
941 line->SetPos(line->GetPosX() + xdelta,
942 ypos + line->GetPosY());
943 line = line->GetNext();
944 }
945 }
946 else // align == justify
947 {
948 // we have to distribute the extra horz space between the cells
949 // on this line
950
951 // an added complication is that some cells have fixed size and
952 // shouldn't get any increment (it so happens that these cells
953 // also don't allow line break on them which provides with an
954 // easy way to test for this) -- and neither should the cells
955 // adjacent to them as this could result in a visible space
956 // between two cells separated by, e.g. font change, cell which
957 // is wrong
958
959 int step = s_width - xpos;
960 if ( step > 0 )
961 {
962 // first count the cells which will get extra space
963 int total = -1;
964
965 const wxHtmlCell *c;
966 if ( line != cell )
967 {
968 for ( c = line; c != cell; c = c->GetNext() )
969 {
970 if ( c->IsLinebreakAllowed() )
971 {
972 total++;
973 }
974 }
975 }
976
977 // and now extra space to those cells which merit it
978 if ( total )
979 {
980 // first visible cell on line is not moved:
981 while (line !=cell && !line->IsLinebreakAllowed())
982 {
983 line->SetPos(line->GetPosX() + s_indent,
984 line->GetPosY() + ypos);
985 line = line->GetNext();
986 }
987
988 if (line != cell)
989 {
990 line->SetPos(line->GetPosX() + s_indent,
991 line->GetPosY() + ypos);
992
993 line = line->GetNext();
994 }
995
996 for ( int n = 0; line != cell; line = line->GetNext() )
997 {
998 if ( line->IsLinebreakAllowed() )
999 {
1000 // offset the next cell relative to this one
1001 // thus increasing our size
1002 n++;
1003 }
1004
1005 line->SetPos(line->GetPosX() + s_indent +
1006 ((n * step) / total),
1007 line->GetPosY() + ypos);
1008 }
1009 }
1010 else
1011 {
1012 // this will cause the code to enter "else branch" below:
1013 step = 0;
1014 }
1015 }
1016 // else branch:
1017 if ( step <= 0 ) // no extra space to distribute
1018 {
1019 // just set the indent properly
1020 while (line != cell)
1021 {
1022 line->SetPos(line->GetPosX() + s_indent,
1023 line->GetPosY() + ypos);
1024 line = line->GetNext();
1025 }
1026 }
1027 }
1028
1029 ypos += ysizedown;
1030 xpos = 0;
1031 ysizeup = ysizedown = 0;
1032 line = cell;
1033 }
1034 }
1035
1036 // setup height & width, depending on container layout:
1037 m_Height = ypos + (ysizedown + ysizeup) + m_IndentBottom;
1038
1039 if (m_Height < m_MinHeight)
1040 {
1041 if (m_MinHeightAlign != wxHTML_ALIGN_TOP)
1042 {
1043 int diff = m_MinHeight - m_Height;
1044 if (m_MinHeightAlign == wxHTML_ALIGN_CENTER) diff /= 2;
1045 cell = m_Cells;
1046 while (cell)
1047 {
1048 cell->SetPos(cell->GetPosX(), cell->GetPosY() + diff);
1049 cell = cell->GetNext();
1050 }
1051 }
1052 m_Height = m_MinHeight;
1053 }
1054
1055 if (curLineWidth > m_MaxTotalWidth)
1056 m_MaxTotalWidth = curLineWidth;
1057
1058 m_MaxTotalWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
1059 MaxLineWidth += s_indent + ((m_IndentRight < 0) ? (-m_IndentRight * m_Width / 100) : m_IndentRight);
1060 if (m_Width < MaxLineWidth) m_Width = MaxLineWidth;
1061 }
1062
1063 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo& info,
1064 wxHtmlCell *cell) const
1065 {
1066 wxHtmlSelection *s = info.GetSelection();
1067 if (!s) return;
1068 if (s->GetFromCell() == cell || s->GetToCell() == cell)
1069 {
1070 info.GetState().SetSelectionState(wxHTML_SEL_CHANGING);
1071 }
1072 }
1073
1074 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo& info,
1075 wxHtmlCell *cell) const
1076 {
1077 wxHtmlSelection *s = info.GetSelection();
1078 if (!s) return;
1079 if (s->GetToCell() == cell)
1080 info.GetState().SetSelectionState(wxHTML_SEL_OUT);
1081 else if (s->GetFromCell() == cell)
1082 info.GetState().SetSelectionState(wxHTML_SEL_IN);
1083 }
1084
1085 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
1086 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
1087
1088 void wxHtmlContainerCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2,
1089 wxHtmlRenderingInfo& info)
1090 {
1091 #if 0 // useful for debugging
1092 dc.SetPen(*wxRED_PEN);
1093 dc.DrawRectangle(x+m_PosX,y+m_PosY,m_Width,m_Height);
1094 #endif
1095
1096 int xlocal = x + m_PosX;
1097 int ylocal = y + m_PosY;
1098
1099 if (m_UseBkColour)
1100 {
1101 wxBrush myb = wxBrush(m_BkColour, wxBRUSHSTYLE_SOLID);
1102
1103 int real_y1 = mMax(ylocal, view_y1);
1104 int real_y2 = mMin(ylocal + m_Height - 1, view_y2);
1105
1106 dc.SetBrush(myb);
1107 dc.SetPen(*wxTRANSPARENT_PEN);
1108 dc.DrawRectangle(xlocal, real_y1, m_Width, real_y2 - real_y1 + 1);
1109 }
1110
1111 if (m_Border == 1)
1112 {
1113 // draw thin border using lines
1114 wxPen mypen1(m_BorderColour1, 1, wxPENSTYLE_SOLID);
1115 wxPen mypen2(m_BorderColour2, 1, wxPENSTYLE_SOLID);
1116
1117 dc.SetPen(mypen1);
1118 dc.DrawLine(xlocal, ylocal, xlocal, ylocal + m_Height - 1);
1119 dc.DrawLine(xlocal, ylocal, xlocal + m_Width, ylocal);
1120 dc.SetPen(mypen2);
1121 dc.DrawLine(xlocal + m_Width - 1, ylocal, xlocal + m_Width - 1, ylocal + m_Height - 1);
1122 dc.DrawLine(xlocal, ylocal + m_Height - 1, xlocal + m_Width, ylocal + m_Height - 1);
1123 }
1124 else if (m_Border> 0)
1125 {
1126 wxBrush mybrush1(m_BorderColour1, wxBRUSHSTYLE_SOLID);
1127 wxBrush mybrush2(m_BorderColour2, wxBRUSHSTYLE_SOLID);
1128
1129 // draw upper left corner
1130 // 0---------------5
1131 // | /
1132 // | 3-----------4
1133 // | |
1134 // | 2
1135 // |/
1136 // 1
1137
1138 wxPoint poly[6];
1139 poly[0].x =m_PosX; poly[0].y = m_PosY ;
1140 poly[1].x =m_PosX; poly[1].y = m_PosY + m_Height;
1141 poly[2].x =m_PosX + m_Border; poly[2].y = poly[1].y - m_Border;
1142 poly[3].x =poly[2].x ; poly[3].y = m_PosY + m_Border;
1143 poly[4].x =m_PosX + m_Width - m_Border; poly[4].y = poly[3].y;
1144 poly[5].x =m_PosX + m_Width; poly[5].y = m_PosY;
1145
1146 dc.SetBrush(mybrush1);
1147 dc.SetPen(*wxTRANSPARENT_PEN);
1148 dc.DrawPolygon(6, poly, x, y);
1149
1150 // draw lower right corner reusing point 1,2,4 and 5
1151 // 5
1152 // /|
1153 // 4 |
1154 // | |
1155 // 2-----------3 |
1156 // / |
1157 // 1---------------0
1158 dc.SetBrush(mybrush2);
1159 poly[0].x = poly[5].x; poly[0].y = poly[1].y;
1160 poly[3].x = poly[4].x; poly[3].y = poly[2].y;
1161 dc.DrawPolygon(6, poly, x, y);
1162
1163 // smooth color transition like firefox
1164 wxColour borderMediumColour(
1165 (m_BorderColour1.Red() + m_BorderColour2.Red()) /2 ,
1166 (m_BorderColour1.Green() + m_BorderColour2.Green()) /2 ,
1167 (m_BorderColour1.Blue() + m_BorderColour2.Blue()) /2
1168 );
1169 wxPen mypen3(borderMediumColour, 1, wxPENSTYLE_SOLID);
1170 dc.SetPen(mypen3);
1171 dc.DrawLines(2, &poly[1], x, y - 1); // between 1 and 2
1172 dc.DrawLines(2, &poly[4], x, y - 1); // between 4 and 5
1173 }
1174 if (m_Cells)
1175 {
1176 // draw container's contents:
1177 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
1178 {
1179
1180 // optimize drawing: don't render off-screen content:
1181 if ((ylocal + cell->GetPosY() <= view_y2) &&
1182 (ylocal + cell->GetPosY() + cell->GetHeight() > view_y1))
1183 {
1184 // the cell is visible, draw it:
1185 UpdateRenderingStatePre(info, cell);
1186 cell->Draw(dc,
1187 xlocal, ylocal, view_y1, view_y2,
1188 info);
1189 UpdateRenderingStatePost(info, cell);
1190 }
1191 else
1192 {
1193 // the cell is off-screen, proceed with font+color+etc.
1194 // changes only:
1195 cell->DrawInvisible(dc, xlocal, ylocal, info);
1196 }
1197 }
1198 }
1199 }
1200
1201
1202
1203 void wxHtmlContainerCell::DrawInvisible(wxDC& dc, int x, int y,
1204 wxHtmlRenderingInfo& info)
1205 {
1206 if (m_Cells)
1207 {
1208 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
1209 {
1210 UpdateRenderingStatePre(info, cell);
1211 cell->DrawInvisible(dc, x + m_PosX, y + m_PosY, info);
1212 UpdateRenderingStatePost(info, cell);
1213 }
1214 }
1215 }
1216
1217
1218 wxColour wxHtmlContainerCell::GetBackgroundColour()
1219 {
1220 if (m_UseBkColour)
1221 return m_BkColour;
1222 else
1223 return wxNullColour;
1224 }
1225
1226
1227
1228 wxHtmlLinkInfo *wxHtmlContainerCell::GetLink(int x, int y) const
1229 {
1230 wxHtmlCell *cell = FindCellByPos(x, y);
1231
1232 // VZ: I don't know if we should pass absolute or relative coords to
1233 // wxHtmlCell::GetLink()? As the base class version just ignores them
1234 // anyhow, it hardly matters right now but should still be clarified
1235 return cell ? cell->GetLink(x, y) : NULL;
1236 }
1237
1238
1239
1240 void wxHtmlContainerCell::InsertCell(wxHtmlCell *f)
1241 {
1242 if (!m_Cells) m_Cells = m_LastCell = f;
1243 else
1244 {
1245 m_LastCell->SetNext(f);
1246 m_LastCell = f;
1247 if (m_LastCell) while (m_LastCell->GetNext()) m_LastCell = m_LastCell->GetNext();
1248 }
1249 f->SetParent(this);
1250 m_LastLayout = -1;
1251 }
1252
1253
1254
1255 void wxHtmlContainerCell::SetAlign(const wxHtmlTag& tag)
1256 {
1257 if (tag.HasParam(wxT("ALIGN")))
1258 {
1259 wxString alg = tag.GetParam(wxT("ALIGN"));
1260 alg.MakeUpper();
1261 if (alg == wxT("CENTER"))
1262 SetAlignHor(wxHTML_ALIGN_CENTER);
1263 else if (alg == wxT("LEFT"))
1264 SetAlignHor(wxHTML_ALIGN_LEFT);
1265 else if (alg == wxT("JUSTIFY"))
1266 SetAlignHor(wxHTML_ALIGN_JUSTIFY);
1267 else if (alg == wxT("RIGHT"))
1268 SetAlignHor(wxHTML_ALIGN_RIGHT);
1269 m_LastLayout = -1;
1270 }
1271 }
1272
1273
1274
1275 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag& tag, double pixel_scale)
1276 {
1277 if (tag.HasParam(wxT("WIDTH")))
1278 {
1279 int wdi;
1280 wxString wd = tag.GetParam(wxT("WIDTH"));
1281
1282 if (wd[wd.length()-1] == wxT('%'))
1283 {
1284 wxSscanf(wd.c_str(), wxT("%i%%"), &wdi);
1285 SetWidthFloat(wdi, wxHTML_UNITS_PERCENT);
1286 }
1287 else
1288 {
1289 wxSscanf(wd.c_str(), wxT("%i"), &wdi);
1290 SetWidthFloat((int)(pixel_scale * (double)wdi), wxHTML_UNITS_PIXELS);
1291 }
1292 m_LastLayout = -1;
1293 }
1294 }
1295
1296
1297
1298 const wxHtmlCell* wxHtmlContainerCell::Find(int condition, const void* param) const
1299 {
1300 if (m_Cells)
1301 {
1302 for (wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext())
1303 {
1304 const wxHtmlCell *r = cell->Find(condition, param);
1305 if (r) return r;
1306 }
1307 }
1308 return NULL;
1309 }
1310
1311
1312 wxHtmlCell *wxHtmlContainerCell::FindCellByPos(wxCoord x, wxCoord y,
1313 unsigned flags) const
1314 {
1315 if ( flags & wxHTML_FIND_EXACT )
1316 {
1317 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
1318 {
1319 int cx = cell->GetPosX(),
1320 cy = cell->GetPosY();
1321
1322 if ( (cx <= x) && (cx + cell->GetWidth() > x) &&
1323 (cy <= y) && (cy + cell->GetHeight() > y) )
1324 {
1325 return cell->FindCellByPos(x - cx, y - cy, flags);
1326 }
1327 }
1328 }
1329 else if ( flags & wxHTML_FIND_NEAREST_AFTER )
1330 {
1331 wxHtmlCell *c;
1332 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
1333 {
1334 if ( cell->IsFormattingCell() )
1335 continue;
1336 int cellY = cell->GetPosY();
1337 if (!( y < cellY || (y < cellY + cell->GetHeight() &&
1338 x < cell->GetPosX() + cell->GetWidth()) ))
1339 continue;
1340
1341 c = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
1342 if (c) return c;
1343 }
1344 }
1345 else if ( flags & wxHTML_FIND_NEAREST_BEFORE )
1346 {
1347 wxHtmlCell *c2, *c = NULL;
1348 for ( const wxHtmlCell *cell = m_Cells; cell; cell = cell->GetNext() )
1349 {
1350 if ( cell->IsFormattingCell() )
1351 continue;
1352 int cellY = cell->GetPosY();
1353 if (!( cellY + cell->GetHeight() <= y ||
1354 (y >= cellY && x >= cell->GetPosX()) ))
1355 break;
1356 c2 = cell->FindCellByPos(x - cell->GetPosX(), y - cellY, flags);
1357 if (c2)
1358 c = c2;
1359 }
1360 if (c) return c;
1361 }
1362
1363 return NULL;
1364 }
1365
1366
1367 bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface *window,
1368 const wxPoint& pos,
1369 const wxMouseEvent& event)
1370 {
1371 #if WXWIN_COMPATIBILITY_2_6
1372 wxHtmlCellOnMouseClickCompatHelper compat(window, pos, event);
1373 return compat.CallOnMouseClick(this);
1374 }
1375
1376 void wxHtmlContainerCell::OnMouseClick(wxWindow*,
1377 int, int, const wxMouseEvent& event)
1378 {
1379 wxCHECK_RET( gs_helperOnMouseClick, wxT("unexpected call to OnMouseClick") );
1380 wxHtmlWindowInterface *window = gs_helperOnMouseClick->window;
1381 const wxPoint& pos = gs_helperOnMouseClick->pos;
1382 #endif // WXWIN_COMPATIBILITY_2_6
1383
1384 bool retval = false;
1385 wxHtmlCell *cell = FindCellByPos(pos.x, pos.y);
1386 if ( cell )
1387 retval = cell->ProcessMouseClick(window, pos, event);
1388
1389 #if WXWIN_COMPATIBILITY_2_6
1390 gs_helperOnMouseClick->retval = retval;
1391 #else
1392 return retval;
1393 #endif // WXWIN_COMPATIBILITY_2_6
1394 }
1395
1396
1397 wxHtmlCell *wxHtmlContainerCell::GetFirstTerminal() const
1398 {
1399 if ( m_Cells )
1400 {
1401 wxHtmlCell *c2;
1402 for (wxHtmlCell *c = m_Cells; c; c = c->GetNext())
1403 {
1404 c2 = c->GetFirstTerminal();
1405 if ( c2 )
1406 return c2;
1407 }
1408 }
1409 return NULL;
1410 }
1411
1412 wxHtmlCell *wxHtmlContainerCell::GetLastTerminal() const
1413 {
1414 if ( m_Cells )
1415 {
1416 // most common case first:
1417 wxHtmlCell *c = m_LastCell->GetLastTerminal();
1418 if ( c )
1419 return c;
1420
1421 wxHtmlCell *ctmp;
1422 wxHtmlCell *c2 = NULL;
1423 for (c = m_Cells; c; c = c->GetNext())
1424 {
1425 ctmp = c->GetLastTerminal();
1426 if ( ctmp )
1427 c2 = ctmp;
1428 }
1429 return c2;
1430 }
1431 else
1432 return NULL;
1433 }
1434
1435
1436 static bool IsEmptyContainer(wxHtmlContainerCell *cell)
1437 {
1438 for ( wxHtmlCell *c = cell->GetFirstChild(); c; c = c->GetNext() )
1439 {
1440 if ( !c->IsTerminalCell() || !c->IsFormattingCell() )
1441 return false;
1442 }
1443 return true;
1444 }
1445
1446 void wxHtmlContainerCell::RemoveExtraSpacing(bool top, bool bottom)
1447 {
1448 if ( top )
1449 SetIndent(0, wxHTML_INDENT_TOP);
1450 if ( bottom )
1451 SetIndent(0, wxHTML_INDENT_BOTTOM);
1452
1453 if ( m_Cells )
1454 {
1455 wxHtmlCell *c;
1456 wxHtmlContainerCell *cont;
1457 if ( top )
1458 {
1459 for ( c = m_Cells; c; c = c->GetNext() )
1460 {
1461 if ( c->IsTerminalCell() )
1462 {
1463 if ( !c->IsFormattingCell() )
1464 break;
1465 }
1466 else
1467 {
1468 cont = (wxHtmlContainerCell*)c;
1469 if ( IsEmptyContainer(cont) )
1470 {
1471 cont->SetIndent(0, wxHTML_INDENT_VERTICAL);
1472 }
1473 else
1474 {
1475 cont->RemoveExtraSpacing(true, false);
1476 break;
1477 }
1478 }
1479 }
1480 }
1481
1482 if ( bottom )
1483 {
1484 wxArrayPtrVoid arr;
1485 for ( c = m_Cells; c; c = c->GetNext() )
1486 arr.Add((void*)c);
1487
1488 for ( int i = arr.GetCount() - 1; i >= 0; i--)
1489 {
1490 c = (wxHtmlCell*)arr[i];
1491 if ( c->IsTerminalCell() )
1492 {
1493 if ( !c->IsFormattingCell() )
1494 break;
1495 }
1496 else
1497 {
1498 cont = (wxHtmlContainerCell*)c;
1499 if ( IsEmptyContainer(cont) )
1500 {
1501 cont->SetIndent(0, wxHTML_INDENT_VERTICAL);
1502 }
1503 else
1504 {
1505 cont->RemoveExtraSpacing(false, true);
1506 break;
1507 }
1508 }
1509 }
1510 }
1511 }
1512 }
1513
1514
1515
1516
1517 // --------------------------------------------------------------------------
1518 // wxHtmlColourCell
1519 // --------------------------------------------------------------------------
1520
1521 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell, wxHtmlCell)
1522
1523 void wxHtmlColourCell::Draw(wxDC& dc,
1524 int x, int y,
1525 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1526 wxHtmlRenderingInfo& info)
1527 {
1528 DrawInvisible(dc, x, y, info);
1529 }
1530
1531 void wxHtmlColourCell::DrawInvisible(wxDC& dc,
1532 int WXUNUSED(x), int WXUNUSED(y),
1533 wxHtmlRenderingInfo& info)
1534 {
1535 wxHtmlRenderingState& state = info.GetState();
1536 if (m_Flags & wxHTML_CLR_FOREGROUND)
1537 {
1538 state.SetFgColour(m_Colour);
1539 if (state.GetSelectionState() != wxHTML_SEL_IN)
1540 dc.SetTextForeground(m_Colour);
1541 else
1542 dc.SetTextForeground(
1543 info.GetStyle().GetSelectedTextColour(m_Colour));
1544 }
1545 if (m_Flags & wxHTML_CLR_BACKGROUND)
1546 {
1547 state.SetBgColour(m_Colour);
1548 state.SetBgMode(wxSOLID);
1549 const wxColour c = state.GetSelectionState() == wxHTML_SEL_IN
1550 ? info.GetStyle().GetSelectedTextBgColour(m_Colour)
1551 : m_Colour;
1552 dc.SetTextBackground(c);
1553 dc.SetBackground(c);
1554 dc.SetBackgroundMode(wxSOLID);
1555 }
1556 if (m_Flags & wxHTML_CLR_TRANSPARENT_BACKGROUND)
1557 {
1558 state.SetBgColour(m_Colour);
1559 state.SetBgMode(wxTRANSPARENT);
1560 const wxColour c = state.GetSelectionState() == wxHTML_SEL_IN
1561 ? info.GetStyle().GetSelectedTextBgColour(m_Colour)
1562 : m_Colour;
1563 dc.SetTextBackground(c);
1564 dc.SetBackgroundMode(wxTRANSPARENT);
1565 }
1566 }
1567
1568
1569
1570
1571 // ---------------------------------------------------------------------------
1572 // wxHtmlFontCell
1573 // ---------------------------------------------------------------------------
1574
1575 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell, wxHtmlCell)
1576
1577 void wxHtmlFontCell::Draw(wxDC& dc,
1578 int WXUNUSED(x), int WXUNUSED(y),
1579 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1580 wxHtmlRenderingInfo& WXUNUSED(info))
1581 {
1582 dc.SetFont(m_Font);
1583 }
1584
1585 void wxHtmlFontCell::DrawInvisible(wxDC& dc, int WXUNUSED(x), int WXUNUSED(y),
1586 wxHtmlRenderingInfo& WXUNUSED(info))
1587 {
1588 dc.SetFont(m_Font);
1589 }
1590
1591
1592
1593
1594
1595
1596
1597
1598 // ---------------------------------------------------------------------------
1599 // wxHtmlWidgetCell
1600 // ---------------------------------------------------------------------------
1601
1602 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell, wxHtmlCell)
1603
1604 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow *wnd, int w)
1605 {
1606 int sx, sy;
1607 m_Wnd = wnd;
1608 m_Wnd->GetSize(&sx, &sy);
1609 m_Width = sx, m_Height = sy;
1610 m_WidthFloat = w;
1611 }
1612
1613
1614 void wxHtmlWidgetCell::Draw(wxDC& WXUNUSED(dc),
1615 int WXUNUSED(x), int WXUNUSED(y),
1616 int WXUNUSED(view_y1), int WXUNUSED(view_y2),
1617 wxHtmlRenderingInfo& WXUNUSED(info))
1618 {
1619 int absx = 0, absy = 0, stx, sty;
1620 wxHtmlCell *c = this;
1621
1622 while (c)
1623 {
1624 absx += c->GetPosX();
1625 absy += c->GetPosY();
1626 c = c->GetParent();
1627 }
1628
1629 wxScrolledWindow *scrolwin =
1630 wxDynamicCast(m_Wnd->GetParent(), wxScrolledWindow);
1631 wxCHECK_RET( scrolwin,
1632 wxT("widget cells can only be placed in wxHtmlWindow") );
1633
1634 scrolwin->GetViewStart(&stx, &sty);
1635 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx,
1636 absy - wxHTML_SCROLL_STEP * sty,
1637 m_Width, m_Height);
1638 }
1639
1640
1641
1642 void wxHtmlWidgetCell::DrawInvisible(wxDC& WXUNUSED(dc),
1643 int WXUNUSED(x), int WXUNUSED(y),
1644 wxHtmlRenderingInfo& WXUNUSED(info))
1645 {
1646 int absx = 0, absy = 0, stx, sty;
1647 wxHtmlCell *c = this;
1648
1649 while (c)
1650 {
1651 absx += c->GetPosX();
1652 absy += c->GetPosY();
1653 c = c->GetParent();
1654 }
1655
1656 ((wxScrolledWindow*)(m_Wnd->GetParent()))->GetViewStart(&stx, &sty);
1657 m_Wnd->SetSize(absx - wxHTML_SCROLL_STEP * stx, absy - wxHTML_SCROLL_STEP * sty, m_Width, m_Height);
1658 }
1659
1660
1661
1662 void wxHtmlWidgetCell::Layout(int w)
1663 {
1664 if (m_WidthFloat != 0)
1665 {
1666 m_Width = (w * m_WidthFloat) / 100;
1667 m_Wnd->SetSize(m_Width, m_Height);
1668 }
1669
1670 wxHtmlCell::Layout(w);
1671 }
1672
1673
1674
1675 // ----------------------------------------------------------------------------
1676 // wxHtmlTerminalCellsInterator
1677 // ----------------------------------------------------------------------------
1678
1679 const wxHtmlCell* wxHtmlTerminalCellsInterator::operator++()
1680 {
1681 if ( !m_pos )
1682 return NULL;
1683
1684 do
1685 {
1686 if ( m_pos == m_to )
1687 {
1688 m_pos = NULL;
1689 return NULL;
1690 }
1691
1692 if ( m_pos->GetNext() )
1693 m_pos = m_pos->GetNext();
1694 else
1695 {
1696 // we must go up the hierarchy until we reach container where this
1697 // is not the last child, and then go down to first terminal cell:
1698 while ( m_pos->GetNext() == NULL )
1699 {
1700 m_pos = m_pos->GetParent();
1701 if ( !m_pos )
1702 return NULL;
1703 }
1704 m_pos = m_pos->GetNext();
1705 }
1706 while ( m_pos->GetFirstChild() != NULL )
1707 m_pos = m_pos->GetFirstChild();
1708 } while ( !m_pos->IsTerminalCell() );
1709
1710 return m_pos;
1711 }
1712
1713 #endif