1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/htmlcell.cpp
3 // Purpose: wxHtmlCell - basic element of HTML output
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_HTML && wxUSE_STREAMS
19 #include "wx/dynarray.h"
21 #include "wx/colour.h"
23 #include "wx/settings.h"
24 #include "wx/module.h"
25 #include "wx/wxcrtvararg.h"
28 #include "wx/html/htmlcell.h"
29 #include "wx/html/htmlwin.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 void wxHtmlSelection::Set(const wxPoint
& fromPos
, const wxHtmlCell
*fromCell
,
38 const wxPoint
& toPos
, const wxHtmlCell
*toCell
)
40 m_fromCell
= fromCell
;
46 void wxHtmlSelection::Set(const wxHtmlCell
*fromCell
, const wxHtmlCell
*toCell
)
48 wxPoint p1
= fromCell
? fromCell
->GetAbsPos() : wxDefaultPosition
;
49 wxPoint p2
= toCell
? toCell
->GetAbsPos() : wxDefaultPosition
;
52 p2
.x
+= toCell
->GetWidth();
53 p2
.y
+= toCell
->GetHeight();
55 Set(p1
, fromCell
, p2
, toCell
);
59 wxDefaultHtmlRenderingStyle::
60 GetSelectedTextColour(const wxColour
& WXUNUSED(clr
))
62 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
66 wxDefaultHtmlRenderingStyle::
67 GetSelectedTextBgColour(const wxColour
& WXUNUSED(clr
))
69 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell
, wxObject
)
79 wxHtmlCell::wxHtmlCell() : wxObject()
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;
90 wxHtmlCell::~wxHtmlCell()
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
)
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;
106 m_ScriptBaseline
= 0;
108 m_Descent
+= m_ScriptBaseline
;
111 #if WXWIN_COMPATIBILITY_2_6
113 struct wxHtmlCellOnMouseClickCompatHelper
;
115 static wxHtmlCellOnMouseClickCompatHelper
*gs_helperOnMouseClick
= NULL
;
117 // helper for routing calls to new ProcessMouseClick() method to deprecated
118 // OnMouseClick() method
119 struct wxHtmlCellOnMouseClickCompatHelper
121 wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface
*window_
,
123 const wxMouseEvent
& event_
)
124 : window(window_
), pos(pos_
), event(event_
), retval(false)
128 bool CallOnMouseClick(wxHtmlCell
*cell
)
130 wxHtmlCellOnMouseClickCompatHelper
*oldHelper
= gs_helperOnMouseClick
;
131 gs_helperOnMouseClick
= this;
134 window
? window
->GetHTMLWindow() : NULL
,
138 gs_helperOnMouseClick
= oldHelper
;
142 wxHtmlWindowInterface
*window
;
144 const wxMouseEvent
& event
;
147 #endif // WXWIN_COMPATIBILITY_2_6
149 bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
151 const wxMouseEvent
& event
)
153 wxCHECK_MSG( window
, false, _T("window interface must be provided") );
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
161 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
162 return compat
.CallOnMouseClick(this);
165 void wxHtmlCell::OnMouseClick(wxWindow
*, int, int, const wxMouseEvent
& event
)
167 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
168 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
169 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
170 #endif // WXWIN_COMPATIBILITY_2_6
172 wxHtmlLinkInfo
*lnk
= GetLink(pos
.x
, pos
.y
);
177 wxHtmlLinkInfo
lnk2(*lnk
);
178 lnk2
.SetEvent(&event
);
179 lnk2
.SetHtmlCell(this);
181 window
->OnHTMLLinkClicked(lnk2
);
185 #if WXWIN_COMPATIBILITY_2_6
186 gs_helperOnMouseClick
->retval
= retval
;
189 #endif // WXWIN_COMPATIBILITY_2_6
192 #if WXWIN_COMPATIBILITY_2_6
193 wxCursor
wxHtmlCell::GetCursor() const
197 #endif // WXWIN_COMPATIBILITY_2_6
199 wxCursor
wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
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 // overriden GetCursor() continues to work. The trick is that the base
205 // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we
206 // know that GetCursor() was overriden iff it returns valid cursor.
207 wxCursor cur
= GetCursor();
210 #endif // WXWIN_COMPATIBILITY_2_6
214 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link
);
218 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default
);
223 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
,
224 wxArrayInt
& WXUNUSED(known_pagebreaks
)) const
226 if ((!m_CanLiveOnPagebreak
) &&
227 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
238 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
240 if (m_Link
) delete m_Link
;
242 if (link
.GetHref() != wxEmptyString
)
243 m_Link
= new wxHtmlLinkInfo(link
);
247 void wxHtmlCell::Layout(int WXUNUSED(w
))
254 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
260 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
261 unsigned flags
) const
263 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
265 return wxConstCast(this, wxHtmlCell
);
269 if ((flags
& wxHTML_FIND_NEAREST_AFTER
) &&
270 (y
< 0 || (y
< 0+m_Height
&& x
< 0+m_Width
)))
271 return wxConstCast(this, wxHtmlCell
);
272 else if ((flags
& wxHTML_FIND_NEAREST_BEFORE
) &&
273 (y
>= 0+m_Height
|| (y
>= 0 && x
>= 0)))
274 return wxConstCast(this, wxHtmlCell
);
281 wxPoint
wxHtmlCell::GetAbsPos(wxHtmlCell
*rootCell
) const
283 wxPoint
p(m_PosX
, m_PosY
);
284 for (wxHtmlCell
*parent
= m_Parent
; parent
&& parent
!= rootCell
;
285 parent
= parent
->m_Parent
)
287 p
.x
+= parent
->m_PosX
;
288 p
.y
+= parent
->m_PosY
;
293 wxHtmlCell
*wxHtmlCell::GetRootCell() const
295 wxHtmlCell
*c
= wxConstCast(this, wxHtmlCell
);
296 while ( c
->m_Parent
)
301 unsigned wxHtmlCell::GetDepth() const
304 for (wxHtmlCell
*p
= m_Parent
; p
; p
= p
->m_Parent
)
309 bool wxHtmlCell::IsBefore(wxHtmlCell
*cell
) const
311 const wxHtmlCell
*c1
= this;
312 const wxHtmlCell
*c2
= cell
;
313 unsigned d1
= GetDepth();
314 unsigned d2
= cell
->GetDepth();
317 for (; d1
!= d2
; d1
-- )
320 for (; d1
!= d2
; d2
-- )
328 if ( c1
->m_Parent
== c2
->m_Parent
)
345 wxFAIL_MSG(_T("Cells are in different trees"));
350 //-----------------------------------------------------------------------------
352 //-----------------------------------------------------------------------------
354 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell
, wxHtmlCell
)
356 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, const wxDC
& dc
) : wxHtmlCell()
360 dc
.GetTextExtent(m_Word
, &w
, &h
, &d
);
364 SetCanLiveOnPagebreak(false);
365 m_allowLinebreak
= true;
368 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
370 if ( cell
&& m_Parent
== cell
->m_Parent
&&
371 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
373 m_allowLinebreak
= false;
377 // Splits m_Word into up to three parts according to selection, returns
378 // substring before, in and after selection and the points (in relative coords)
379 // where s2 and s3 start:
380 void wxHtmlWordCell::Split(const wxDC
& dc
,
381 const wxPoint
& selFrom
, const wxPoint
& selTo
,
382 unsigned& pos1
, unsigned& pos2
) const
384 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
385 wxDefaultPosition
: selFrom
- GetAbsPos();
386 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
387 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
389 unsigned len
= m_Word
.length();
393 // adjust for cases when the start/end position is completely
397 if ( pt2
.y
>= m_Height
)
402 // implementation using PartialExtents to support fractional widths
404 dc
.GetPartialTextExtents(m_Word
,widths
) ;
405 while( i
< len
&& pt1
.x
>= widths
[i
] )
408 wxCoord charW
, charH
;
409 while ( pt1
.x
> 0 && i
< len
)
411 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
419 #endif // __WXMAC__/!__WXMAC__
424 while( j
< len
&& pt2
.x
>= widths
[j
] )
429 while ( pt2
.x
> 0 && j
< len
)
431 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
439 #endif // __WXMAC__/!__WXMAC__
445 void wxHtmlWordCell::SetSelectionPrivPos(const wxDC
& dc
, wxHtmlSelection
*s
) const
450 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
451 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
454 wxPoint
p(0, m_Word
.length());
456 if ( this == s
->GetFromCell() )
457 p
.x
= p1
; // selection starts here
458 if ( this == s
->GetToCell() )
459 p
.y
= p2
; // selection ends here
461 if ( this == s
->GetFromCell() )
462 s
->SetFromPrivPos(p
);
463 if ( this == s
->GetToCell() )
468 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
471 wxColour fg
= info
.GetState().GetFgColour();
472 wxColour bg
= info
.GetState().GetBgColour();
476 dc
.SetBackgroundMode(wxSOLID
);
477 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
478 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
479 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
484 dc
.SetBackgroundMode(wxTRANSPARENT
);
485 dc
.SetTextForeground(fg
);
486 dc
.SetTextBackground(bg
);
487 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
492 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
493 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
494 wxHtmlRenderingInfo
& info
)
496 #if 0 // useful for debugging
497 dc
.SetPen(*wxBLACK_PEN
);
498 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
501 bool drawSelectionAfterCell
= false;
503 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
505 // Selection changing, we must draw the word piecewise:
506 wxHtmlSelection
*s
= info
.GetSelection();
511 wxPoint priv
= (this == s
->GetFromCell()) ?
512 s
->GetFromPrivPos() : s
->GetToPrivPos();
514 // NB: this is quite a hack: in order to compute selection boundaries
515 // (in word's characters) we must know current font, which is only
516 // possible inside rendering code. Therefore we update the
517 // information here and store it in wxHtmlSelection so that
518 // ConvertToText can use it later:
519 if ( priv
== wxDefaultPosition
)
521 SetSelectionPrivPos(dc
, s
);
522 priv
= (this == s
->GetFromCell()) ?
523 s
->GetFromPrivPos() : s
->GetToPrivPos();
531 txt
= m_Word
.Mid(0, part1
);
532 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
533 dc
.GetTextExtent(txt
, &w
, &h
);
537 SwitchSelState(dc
, info
, true);
539 txt
= m_Word
.Mid(part1
, part2
-part1
);
540 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
542 if ( (size_t)part2
< m_Word
.length() )
544 dc
.GetTextExtent(txt
, &w
, &h
);
546 SwitchSelState(dc
, info
, false);
547 txt
= m_Word
.Mid(part2
);
548 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
551 drawSelectionAfterCell
= true;
555 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
556 // Not changing selection state, draw the word in single mode:
557 if ( selstate
!= wxHTML_SEL_OUT
&&
558 dc
.GetBackgroundMode() != wxSOLID
)
560 SwitchSelState(dc
, info
, true);
562 else if ( selstate
== wxHTML_SEL_OUT
&&
563 dc
.GetBackgroundMode() == wxSOLID
)
565 SwitchSelState(dc
, info
, false);
567 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
568 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
571 // NB: If the text is justified then there is usually some free space
572 // between adjacent cells and drawing the selection only onto cells
573 // would result in ugly unselected spaces. The code below detects
574 // this special case and renders the selection *outside* the sell,
576 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
577 drawSelectionAfterCell
)
579 wxHtmlCell
*nextCell
= m_Next
;
580 while ( nextCell
&& nextCell
->IsFormattingCell() )
581 nextCell
= nextCell
->GetNext();
584 int nextX
= nextCell
->GetPosX();
585 if ( m_PosX
+ m_Width
< nextX
)
587 dc
.SetBrush(dc
.GetBackground());
588 dc
.SetPen(*wxTRANSPARENT_PEN
);
589 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
590 nextX
- m_PosX
- m_Width
, m_Height
);
597 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
599 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
601 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
604 // VZ: we may be called before we had a chance to re-render ourselves
605 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
606 // that this only happens in case of a double/triple click (which
607 // seems to be the case now) and so it makes sense to select the
608 // entire contents of the cell in this case
610 // TODO: but this really needs to be fixed in some better way later...
611 if ( priv
!= wxDefaultPosition
)
615 return m_Word
.Mid(part1
, part2
-part1
);
617 //else: return the whole word below
623 wxCursor
wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
627 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text
);
631 return wxHtmlCell::GetMouseCursor(window
);
636 //-----------------------------------------------------------------------------
637 // wxHtmlContainerCell
638 //-----------------------------------------------------------------------------
640 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
642 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
644 m_Cells
= m_LastCell
= NULL
;
647 if (m_Parent
) m_Parent
->InsertCell(this);
648 m_AlignHor
= wxHTML_ALIGN_LEFT
;
649 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
650 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
651 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
652 m_UseBkColour
= false;
655 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
659 wxHtmlContainerCell::~wxHtmlContainerCell()
661 wxHtmlCell
*cell
= m_Cells
;
664 wxHtmlCell
*cellNext
= cell
->GetNext();
672 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
674 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
675 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
676 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
677 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
678 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
684 int wxHtmlContainerCell::GetIndent(int ind
) const
686 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
687 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
688 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
689 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
690 else return -1; /* BUG! Should not be called... */
696 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
699 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
700 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
701 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
702 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
703 if (p
) return wxHTML_UNITS_PERCENT
;
704 else return wxHTML_UNITS_PIXELS
;
708 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
,
709 wxArrayInt
& known_pagebreaks
) const
711 if (!m_CanLiveOnPagebreak
)
712 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
);
714 wxHtmlCell
*c
= GetFirstChild();
716 int pbrk
= *pagebreak
- m_PosY
;
720 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
))
725 *pagebreak
= pbrk
+ m_PosY
;
730 void wxHtmlContainerCell::Layout(int w
)
732 wxHtmlCell::Layout(w
);
734 if (m_LastLayout
== w
) return;
736 // VS: Any attempt to layout with negative or zero width leads to hell,
737 // but we can't ignore such attempts completely, since it sometimes
738 // happen (e.g. when trying how small a table can be). The best thing we
739 // can do is to set the width of child cells to zero
743 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
745 // this does two things: it recursively calls this code on all
746 // child contrainers and resets children's position to (0,0)
750 wxHtmlCell
*nextCell
;
751 long xpos
= 0, ypos
= m_IndentTop
;
752 int xdelta
= 0, ybasicpos
= 0, ydiff
;
753 int s_width
, nextWordWidth
, s_indent
;
754 int ysizeup
= 0, ysizedown
= 0;
755 int MaxLineWidth
= 0;
756 int curLineWidth
= 0;
766 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
768 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
769 else m_Width
= m_WidthFloat
* w
/ 100;
773 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
774 else m_Width
= m_WidthFloat
;
779 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
780 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
781 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
782 cell
->Layout(m_Width
- (l
+ r
));
791 // adjust indentation:
792 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
793 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
796 wxHtmlCell
*cell
= m_Cells
,
802 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
803 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
804 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
806 ydiff
= cell
->GetHeight() + ybasicpos
;
808 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
809 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
811 // layout nonbreakable run of cells:
812 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
813 xpos
+= cell
->GetWidth();
814 if (!cell
->IsTerminalCell())
816 // Container cell indicates new line
817 if (curLineWidth
> m_MaxTotalWidth
)
818 m_MaxTotalWidth
= curLineWidth
;
820 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
821 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
825 // Normal cell, add maximum cell width to line width
826 curLineWidth
+= cell
->GetMaxTotalWidth();
828 cell
= cell
->GetNext();
830 // compute length of the next word that would be added:
837 nextWordWidth
+= nextCell
->GetWidth();
838 nextCell
= nextCell
->GetNext();
839 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
842 // force new line if occurred:
843 if ((cell
== NULL
) ||
844 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
846 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
847 if (ysizeup
< 0) ysizeup
= 0;
848 if (ysizedown
< 0) ysizedown
= 0;
849 switch (m_AlignHor
) {
850 case wxHTML_ALIGN_LEFT
:
851 case wxHTML_ALIGN_JUSTIFY
:
854 case wxHTML_ALIGN_RIGHT
:
855 xdelta
= 0 + (s_width
- xpos
);
857 case wxHTML_ALIGN_CENTER
:
858 xdelta
= 0 + (s_width
- xpos
) / 2;
861 if (xdelta
< 0) xdelta
= 0;
866 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
870 line
->SetPos(line
->GetPosX() + xdelta
,
871 ypos
+ line
->GetPosY());
872 line
= line
->GetNext();
875 else // align == justify
877 // we have to distribute the extra horz space between the cells
880 // an added complication is that some cells have fixed size and
881 // shouldn't get any increment (it so happens that these cells
882 // also don't allow line break on them which provides with an
883 // easy way to test for this) -- and neither should the cells
884 // adjacent to them as this could result in a visible space
885 // between two cells separated by, e.g. font change, cell which
888 int step
= s_width
- xpos
;
891 // first count the cells which will get extra space
897 for ( c
= line
; c
!= cell
; c
= c
->GetNext() )
899 if ( c
->IsLinebreakAllowed() )
906 // and now extra space to those cells which merit it
909 // first visible cell on line is not moved:
910 while (line
!=cell
&& !line
->IsLinebreakAllowed())
912 line
->SetPos(line
->GetPosX() + s_indent
,
913 line
->GetPosY() + ypos
);
914 line
= line
->GetNext();
919 line
->SetPos(line
->GetPosX() + s_indent
,
920 line
->GetPosY() + ypos
);
922 line
= line
->GetNext();
925 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
927 if ( line
->IsLinebreakAllowed() )
929 // offset the next cell relative to this one
930 // thus increasing our size
934 line
->SetPos(line
->GetPosX() + s_indent
+
935 ((n
* step
) / total
),
936 line
->GetPosY() + ypos
);
941 // this will cause the code to enter "else branch" below:
946 if ( step
<= 0 ) // no extra space to distribute
948 // just set the indent properly
951 line
->SetPos(line
->GetPosX() + s_indent
,
952 line
->GetPosY() + ypos
);
953 line
= line
->GetNext();
960 ysizeup
= ysizedown
= 0;
965 // setup height & width, depending on container layout:
966 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
968 if (m_Height
< m_MinHeight
)
970 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
972 int diff
= m_MinHeight
- m_Height
;
973 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
977 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
978 cell
= cell
->GetNext();
981 m_Height
= m_MinHeight
;
984 if (curLineWidth
> m_MaxTotalWidth
)
985 m_MaxTotalWidth
= curLineWidth
;
987 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
988 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
989 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
994 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
995 wxHtmlCell
*cell
) const
997 wxHtmlSelection
*s
= info
.GetSelection();
999 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
1001 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
1005 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
1006 wxHtmlCell
*cell
) const
1008 wxHtmlSelection
*s
= info
.GetSelection();
1010 if (s
->GetToCell() == cell
)
1011 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
1012 else if (s
->GetFromCell() == cell
)
1013 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
1016 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
1017 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
1019 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
1020 wxHtmlRenderingInfo
& info
)
1022 #if 0 // useful for debugging
1023 dc
.SetPen(*wxRED_PEN
);
1024 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
1027 int xlocal
= x
+ m_PosX
;
1028 int ylocal
= y
+ m_PosY
;
1032 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
1034 int real_y1
= mMax(ylocal
, view_y1
);
1035 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
1038 dc
.SetPen(*wxTRANSPARENT_PEN
);
1039 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
1044 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
1045 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
1048 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
1049 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
1051 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
1052 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
1057 // draw container's contents:
1058 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1061 // optimize drawing: don't render off-screen content:
1062 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
1063 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
1065 // the cell is visible, draw it:
1066 UpdateRenderingStatePre(info
, cell
);
1068 xlocal
, ylocal
, view_y1
, view_y2
,
1070 UpdateRenderingStatePost(info
, cell
);
1074 // the cell is off-screen, proceed with font+color+etc.
1076 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
1084 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
1085 wxHtmlRenderingInfo
& info
)
1089 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1091 UpdateRenderingStatePre(info
, cell
);
1092 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
1093 UpdateRenderingStatePost(info
, cell
);
1099 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1104 return wxNullColour
;
1109 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1111 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1113 // VZ: I don't know if we should pass absolute or relative coords to
1114 // wxHtmlCell::GetLink()? As the base class version just ignores them
1115 // anyhow, it hardly matters right now but should still be clarified
1116 return cell
? cell
->GetLink(x
, y
) : NULL
;
1121 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1123 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1126 m_LastCell
->SetNext(f
);
1128 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1136 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1138 if (tag
.HasParam(wxT("ALIGN")))
1140 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1142 if (alg
== wxT("CENTER"))
1143 SetAlignHor(wxHTML_ALIGN_CENTER
);
1144 else if (alg
== wxT("LEFT"))
1145 SetAlignHor(wxHTML_ALIGN_LEFT
);
1146 else if (alg
== wxT("JUSTIFY"))
1147 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1148 else if (alg
== wxT("RIGHT"))
1149 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1156 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1158 if (tag
.HasParam(wxT("WIDTH")))
1161 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1163 if (wd
[wd
.length()-1] == wxT('%'))
1165 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1166 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1170 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1171 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1179 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1183 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1185 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1193 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1194 unsigned flags
) const
1196 if ( flags
& wxHTML_FIND_EXACT
)
1198 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1200 int cx
= cell
->GetPosX(),
1201 cy
= cell
->GetPosY();
1203 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1204 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1206 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1210 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1213 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1215 if ( cell
->IsFormattingCell() )
1217 int cellY
= cell
->GetPosY();
1218 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1219 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1222 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1226 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1228 wxHtmlCell
*c2
, *c
= NULL
;
1229 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1231 if ( cell
->IsFormattingCell() )
1233 int cellY
= cell
->GetPosY();
1234 if (!( cellY
+ cell
->GetHeight() <= y
||
1235 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1237 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1248 bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
1250 const wxMouseEvent
& event
)
1252 #if WXWIN_COMPATIBILITY_2_6
1253 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
1254 return compat
.CallOnMouseClick(this);
1257 void wxHtmlContainerCell::OnMouseClick(wxWindow
*,
1258 int, int, const wxMouseEvent
& event
)
1260 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
1261 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
1262 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
1263 #endif // WXWIN_COMPATIBILITY_2_6
1265 bool retval
= false;
1266 wxHtmlCell
*cell
= FindCellByPos(pos
.x
, pos
.y
);
1268 retval
= cell
->ProcessMouseClick(window
, pos
, event
);
1270 #if WXWIN_COMPATIBILITY_2_6
1271 gs_helperOnMouseClick
->retval
= retval
;
1274 #endif // WXWIN_COMPATIBILITY_2_6
1278 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1283 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1285 c2
= c
->GetFirstTerminal();
1293 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1297 // most common case first:
1298 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1303 wxHtmlCell
*c2
= NULL
;
1304 for (c
= m_Cells
; c
; c
= c
->GetNext())
1306 ctmp
= c
->GetLastTerminal();
1317 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1319 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1321 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1327 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1330 SetIndent(0, wxHTML_INDENT_TOP
);
1332 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1337 wxHtmlContainerCell
*cont
;
1340 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1342 if ( c
->IsTerminalCell() )
1344 if ( !c
->IsFormattingCell() )
1349 cont
= (wxHtmlContainerCell
*)c
;
1350 if ( IsEmptyContainer(cont
) )
1352 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1356 cont
->RemoveExtraSpacing(true, false);
1366 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1369 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1371 c
= (wxHtmlCell
*)arr
[i
];
1372 if ( c
->IsTerminalCell() )
1374 if ( !c
->IsFormattingCell() )
1379 cont
= (wxHtmlContainerCell
*)c
;
1380 if ( IsEmptyContainer(cont
) )
1382 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1386 cont
->RemoveExtraSpacing(false, true);
1398 // --------------------------------------------------------------------------
1400 // --------------------------------------------------------------------------
1402 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1404 void wxHtmlColourCell::Draw(wxDC
& dc
,
1406 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1407 wxHtmlRenderingInfo
& info
)
1409 DrawInvisible(dc
, x
, y
, info
);
1412 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1413 int WXUNUSED(x
), int WXUNUSED(y
),
1414 wxHtmlRenderingInfo
& info
)
1416 wxHtmlRenderingState
& state
= info
.GetState();
1417 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1419 state
.SetFgColour(m_Colour
);
1420 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1421 dc
.SetTextForeground(m_Colour
);
1423 dc
.SetTextForeground(
1424 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1426 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1428 state
.SetBgColour(m_Colour
);
1429 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1431 dc
.SetTextBackground(m_Colour
);
1432 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1436 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1437 dc
.SetTextBackground(c
);
1438 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1446 // ---------------------------------------------------------------------------
1448 // ---------------------------------------------------------------------------
1450 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1452 void wxHtmlFontCell::Draw(wxDC
& dc
,
1453 int WXUNUSED(x
), int WXUNUSED(y
),
1454 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1455 wxHtmlRenderingInfo
& WXUNUSED(info
))
1460 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1461 wxHtmlRenderingInfo
& WXUNUSED(info
))
1473 // ---------------------------------------------------------------------------
1475 // ---------------------------------------------------------------------------
1477 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1479 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1483 m_Wnd
->GetSize(&sx
, &sy
);
1484 m_Width
= sx
, m_Height
= sy
;
1489 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1490 int WXUNUSED(x
), int WXUNUSED(y
),
1491 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1492 wxHtmlRenderingInfo
& WXUNUSED(info
))
1494 int absx
= 0, absy
= 0, stx
, sty
;
1495 wxHtmlCell
*c
= this;
1499 absx
+= c
->GetPosX();
1500 absy
+= c
->GetPosY();
1504 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1505 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1510 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1511 int WXUNUSED(x
), int WXUNUSED(y
),
1512 wxHtmlRenderingInfo
& WXUNUSED(info
))
1514 int absx
= 0, absy
= 0, stx
, sty
;
1515 wxHtmlCell
*c
= this;
1519 absx
+= c
->GetPosX();
1520 absy
+= c
->GetPosY();
1524 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1525 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1530 void wxHtmlWidgetCell::Layout(int w
)
1532 if (m_WidthFloat
!= 0)
1534 m_Width
= (w
* m_WidthFloat
) / 100;
1535 m_Wnd
->SetSize(m_Width
, m_Height
);
1538 wxHtmlCell::Layout(w
);
1543 // ----------------------------------------------------------------------------
1544 // wxHtmlTerminalCellsInterator
1545 // ----------------------------------------------------------------------------
1547 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1554 if ( m_pos
== m_to
)
1560 if ( m_pos
->GetNext() )
1561 m_pos
= m_pos
->GetNext();
1564 // we must go up the hierarchy until we reach container where this
1565 // is not the last child, and then go down to first terminal cell:
1566 while ( m_pos
->GetNext() == NULL
)
1568 m_pos
= m_pos
->GetParent();
1572 m_pos
= m_pos
->GetNext();
1574 while ( m_pos
->GetFirstChild() != NULL
)
1575 m_pos
= m_pos
->GetFirstChild();
1576 } while ( !m_pos
->IsTerminalCell() );