1 /////////////////////////////////////////////////////////////////////////////
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"
14 #if wxUSE_HTML && wxUSE_STREAMS
22 #include "wx/colour.h"
26 #include "wx/html/htmlcell.h"
27 #include "wx/html/htmlwin.h"
28 #include "wx/settings.h"
29 #include "wx/module.h"
30 #include "wx/dynarray.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 void wxHtmlSelection::Set(const wxPoint
& fromPos
, const wxHtmlCell
*fromCell
,
39 const wxPoint
& toPos
, const wxHtmlCell
*toCell
)
41 m_fromCell
= fromCell
;
47 void wxHtmlSelection::Set(const wxHtmlCell
*fromCell
, const wxHtmlCell
*toCell
)
49 wxPoint p1
= fromCell
? fromCell
->GetAbsPos() : wxDefaultPosition
;
50 wxPoint p2
= toCell
? toCell
->GetAbsPos() : wxDefaultPosition
;
53 p2
.x
+= toCell
->GetWidth();
54 p2
.y
+= toCell
->GetHeight();
56 Set(p1
, fromCell
, p2
, toCell
);
60 wxDefaultHtmlRenderingStyle::
61 GetSelectedTextColour(const wxColour
& WXUNUSED(clr
))
63 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
67 wxDefaultHtmlRenderingStyle::
68 GetSelectedTextBgColour(const wxColour
& WXUNUSED(clr
))
70 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
78 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell
, wxObject
)
80 wxHtmlCell::wxHtmlCell() : wxObject()
84 m_Width
= m_Height
= m_Descent
= 0;
85 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
; // <sub> or <sup> mode
86 m_ScriptBaseline
= 0; // <sub> or <sup> baseline
87 m_CanLiveOnPagebreak
= true;
91 wxHtmlCell::~wxHtmlCell()
96 // Update the descent value when whe are in a <sub> or <sup>.
97 // prevbase is the parent base
98 void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode
, long previousBase
)
102 if (mode
== wxHTML_SCRIPT_SUP
)
103 m_ScriptBaseline
= previousBase
- (m_Height
+ 1) / 2;
104 else if (mode
== wxHTML_SCRIPT_SUB
)
105 m_ScriptBaseline
= previousBase
+ (m_Height
+ 1) / 6;
107 m_ScriptBaseline
= 0;
109 m_Descent
+= m_ScriptBaseline
;
112 #if WXWIN_COMPATIBILITY_2_6
114 struct wxHtmlCellOnMouseClickCompatHelper
;
116 static wxHtmlCellOnMouseClickCompatHelper
*gs_helperOnMouseClick
= NULL
;
118 // helper for routing calls to new ProcessMouseClick() method to deprecated
119 // OnMouseClick() method
120 struct wxHtmlCellOnMouseClickCompatHelper
122 wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface
*window_
,
124 const wxMouseEvent
& event_
)
125 : window(window_
), pos(pos_
), event(event_
), retval(false)
129 bool CallOnMouseClick(wxHtmlCell
*cell
)
131 wxHtmlCellOnMouseClickCompatHelper
*oldHelper
= gs_helperOnMouseClick
;
132 gs_helperOnMouseClick
= this;
135 window
? window
->GetHTMLWindow() : NULL
,
139 gs_helperOnMouseClick
= oldHelper
;
143 wxHtmlWindowInterface
*window
;
145 const wxMouseEvent
& event
;
148 #endif // WXWIN_COMPATIBILITY_2_6
150 bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
152 const wxMouseEvent
& event
)
154 wxCHECK_MSG( window
, false, _T("window interface must be provided") );
156 #if WXWIN_COMPATIBILITY_2_6
157 // NB: this hack puts the body of ProcessMouseClick() into OnMouseClick()
158 // (for which it has to pass the arguments and return value via a
159 // helper variable because these two methods have different
160 // signatures), so that old code overriding OnMouseClick will continue
162 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
163 return compat
.CallOnMouseClick(this);
166 void wxHtmlCell::OnMouseClick(wxWindow
*, int, int, const wxMouseEvent
& event
)
168 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
169 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
170 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
171 #endif // WXWIN_COMPATIBILITY_2_6
173 wxHtmlLinkInfo
*lnk
= GetLink(pos
.x
, pos
.y
);
178 wxHtmlLinkInfo
lnk2(*lnk
);
179 lnk2
.SetEvent(&event
);
180 lnk2
.SetHtmlCell(this);
182 window
->OnHTMLLinkClicked(lnk2
);
186 #if WXWIN_COMPATIBILITY_2_6
187 gs_helperOnMouseClick
->retval
= retval
;
190 #endif // WXWIN_COMPATIBILITY_2_6
193 #if WXWIN_COMPATIBILITY_2_6
194 wxCursor
wxHtmlCell::GetCursor() const
198 #endif // WXWIN_COMPATIBILITY_2_6
200 wxCursor
wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
202 #if WXWIN_COMPATIBILITY_2_6
203 // NB: Older versions of wx used GetCursor() virtual method in place of
204 // GetMouseCursor(interface). This code ensures that user code that
205 // overriden GetCursor() continues to work. The trick is that the base
206 // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we
207 // know that GetCursor() was overriden iff it returns valid cursor.
208 wxCursor cur
= GetCursor();
211 #endif // WXWIN_COMPATIBILITY_2_6
215 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link
);
219 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default
);
224 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) 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()
359 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
360 SetCanLiveOnPagebreak(false);
361 m_allowLinebreak
= true;
364 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
366 if ( cell
&& m_Parent
== cell
->m_Parent
&&
367 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
369 m_allowLinebreak
= false;
373 // Splits m_Word into up to three parts according to selection, returns
374 // substring before, in and after selection and the points (in relative coords)
375 // where s2 and s3 start:
376 void wxHtmlWordCell::Split(const wxDC
& dc
,
377 const wxPoint
& selFrom
, const wxPoint
& selTo
,
378 unsigned& pos1
, unsigned& pos2
) const
380 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
381 wxDefaultPosition
: selFrom
- GetAbsPos();
382 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
383 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
385 unsigned len
= m_Word
.length();
389 // adjust for cases when the start/end position is completely
393 if ( pt2
.y
>= m_Height
)
398 // implementation using PartialExtents to support fractional widths
400 dc
.GetPartialTextExtents(m_Word
,widths
) ;
401 while( i
< len
&& pt1
.x
>= widths
[i
] )
404 wxCoord charW
, charH
;
405 while ( pt1
.x
> 0 && i
< len
)
407 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
415 #endif // __WXMAC__/!__WXMAC__
420 while( j
< len
&& pt2
.x
>= widths
[j
] )
425 while ( pt2
.x
> 0 && j
< len
)
427 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
435 #endif // __WXMAC__/!__WXMAC__
441 void wxHtmlWordCell::SetSelectionPrivPos(const wxDC
& dc
, wxHtmlSelection
*s
) const
446 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
447 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
450 wxPoint
p(0, m_Word
.length());
452 if ( this == s
->GetFromCell() )
453 p
.x
= p1
; // selection starts here
454 if ( this == s
->GetToCell() )
455 p
.y
= p2
; // selection ends here
457 if ( this == s
->GetFromCell() )
458 s
->SetFromPrivPos(p
);
459 if ( this == s
->GetToCell() )
464 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
467 wxColour fg
= info
.GetState().GetFgColour();
468 wxColour bg
= info
.GetState().GetBgColour();
472 dc
.SetBackgroundMode(wxSOLID
);
473 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
474 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
475 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
480 dc
.SetBackgroundMode(wxTRANSPARENT
);
481 dc
.SetTextForeground(fg
);
482 dc
.SetTextBackground(bg
);
483 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
488 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
489 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
490 wxHtmlRenderingInfo
& info
)
492 #if 0 // useful for debugging
493 dc
.SetPen(*wxBLACK_PEN
);
494 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
497 bool drawSelectionAfterCell
= false;
499 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
501 // Selection changing, we must draw the word piecewise:
502 wxHtmlSelection
*s
= info
.GetSelection();
507 wxPoint priv
= (this == s
->GetFromCell()) ?
508 s
->GetFromPrivPos() : s
->GetToPrivPos();
510 // NB: this is quite a hack: in order to compute selection boundaries
511 // (in word's characters) we must know current font, which is only
512 // possible inside rendering code. Therefore we update the
513 // information here and store it in wxHtmlSelection so that
514 // ConvertToText can use it later:
515 if ( priv
== wxDefaultPosition
)
517 SetSelectionPrivPos(dc
, s
);
518 priv
= (this == s
->GetFromCell()) ?
519 s
->GetFromPrivPos() : s
->GetToPrivPos();
527 txt
= m_Word
.Mid(0, part1
);
528 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
529 dc
.GetTextExtent(txt
, &w
, &h
);
533 SwitchSelState(dc
, info
, true);
535 txt
= m_Word
.Mid(part1
, part2
-part1
);
536 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
538 if ( (size_t)part2
< m_Word
.length() )
540 dc
.GetTextExtent(txt
, &w
, &h
);
542 SwitchSelState(dc
, info
, false);
543 txt
= m_Word
.Mid(part2
);
544 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
547 drawSelectionAfterCell
= true;
551 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
552 // Not changing selection state, draw the word in single mode:
553 if ( selstate
!= wxHTML_SEL_OUT
&&
554 dc
.GetBackgroundMode() != wxSOLID
)
556 SwitchSelState(dc
, info
, true);
558 else if ( selstate
== wxHTML_SEL_OUT
&&
559 dc
.GetBackgroundMode() == wxSOLID
)
561 SwitchSelState(dc
, info
, false);
563 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
564 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
567 // NB: If the text is justified then there is usually some free space
568 // between adjacent cells and drawing the selection only onto cells
569 // would result in ugly unselected spaces. The code below detects
570 // this special case and renders the selection *outside* the sell,
572 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
573 drawSelectionAfterCell
)
575 wxHtmlCell
*nextCell
= m_Next
;
576 while ( nextCell
&& nextCell
->IsFormattingCell() )
577 nextCell
= nextCell
->GetNext();
580 int nextX
= nextCell
->GetPosX();
581 if ( m_PosX
+ m_Width
< nextX
)
583 dc
.SetBrush(dc
.GetBackground());
584 dc
.SetPen(*wxTRANSPARENT_PEN
);
585 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
586 nextX
- m_PosX
- m_Width
, m_Height
);
593 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
595 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
597 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
600 // VZ: we may be called before we had a chance to re-render ourselves
601 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
602 // that this only happens in case of a double/triple click (which
603 // seems to be the case now) and so it makes sense to select the
604 // entire contents of the cell in this case
606 // TODO: but this really needs to be fixed in some better way later...
607 if ( priv
!= wxDefaultPosition
)
611 return m_Word
.Mid(part1
, part2
-part1
);
613 //else: return the whole word below
619 wxCursor
wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
623 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text
);
627 return wxHtmlCell::GetMouseCursor(window
);
632 //-----------------------------------------------------------------------------
633 // wxHtmlContainerCell
634 //-----------------------------------------------------------------------------
636 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
638 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
640 m_Cells
= m_LastCell
= NULL
;
643 if (m_Parent
) m_Parent
->InsertCell(this);
644 m_AlignHor
= wxHTML_ALIGN_LEFT
;
645 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
646 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
647 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
648 m_UseBkColour
= false;
651 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
655 wxHtmlContainerCell::~wxHtmlContainerCell()
657 wxHtmlCell
*cell
= m_Cells
;
660 wxHtmlCell
*cellNext
= cell
->GetNext();
668 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
670 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
671 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
672 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
673 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
674 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
680 int wxHtmlContainerCell::GetIndent(int ind
) const
682 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
683 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
684 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
685 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
686 else return -1; /* BUG! Should not be called... */
692 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
695 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
696 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
697 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
698 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
699 if (p
) return wxHTML_UNITS_PERCENT
;
700 else return wxHTML_UNITS_PIXELS
;
705 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
707 if (!m_CanLiveOnPagebreak
)
708 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
712 wxHtmlCell
*c
= GetFirstChild();
714 int pbrk
= *pagebreak
- m_PosY
;
718 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
723 *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
->GetNext(); c
!= cell
; c
= c
->GetNext() )
899 if ( c
->IsLinebreakAllowed() )
906 // and now extra space to those cells which merit it
909 // first cell on line is not moved:
910 line
->SetPos(line
->GetPosX() + s_indent
,
911 line
->GetPosY() + ypos
);
913 line
= line
->GetNext();
914 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
916 if ( line
->IsLinebreakAllowed() )
918 // offset the next cell relative to this one
919 // thus increasing our size
923 line
->SetPos(line
->GetPosX() + s_indent
+
924 ((n
* step
) / total
),
925 line
->GetPosY() + ypos
);
930 // this will cause the code to enter "else branch" below:
935 if ( step
<= 0 ) // no extra space to distribute
937 // just set the indent properly
940 line
->SetPos(line
->GetPosX() + s_indent
,
941 line
->GetPosY() + ypos
);
942 line
= line
->GetNext();
949 ysizeup
= ysizedown
= 0;
954 // setup height & width, depending on container layout:
955 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
957 if (m_Height
< m_MinHeight
)
959 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
961 int diff
= m_MinHeight
- m_Height
;
962 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
966 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
967 cell
= cell
->GetNext();
970 m_Height
= m_MinHeight
;
973 if (curLineWidth
> m_MaxTotalWidth
)
974 m_MaxTotalWidth
= curLineWidth
;
976 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
977 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
978 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
983 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
984 wxHtmlCell
*cell
) const
986 wxHtmlSelection
*s
= info
.GetSelection();
988 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
990 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
994 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
995 wxHtmlCell
*cell
) const
997 wxHtmlSelection
*s
= info
.GetSelection();
999 if (s
->GetToCell() == cell
)
1000 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
1001 else if (s
->GetFromCell() == cell
)
1002 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
1005 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
1006 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
1008 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
1009 wxHtmlRenderingInfo
& info
)
1011 #if 0 // useful for debugging
1012 dc
.SetPen(*wxRED_PEN
);
1013 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
1016 int xlocal
= x
+ m_PosX
;
1017 int ylocal
= y
+ m_PosY
;
1021 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
1023 int real_y1
= mMax(ylocal
, view_y1
);
1024 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
1027 dc
.SetPen(*wxTRANSPARENT_PEN
);
1028 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
1033 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
1034 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
1037 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
1038 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
1040 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
1041 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
1046 // draw container's contents:
1047 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1050 // optimize drawing: don't render off-screen content:
1051 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
1052 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
1054 // the cell is visible, draw it:
1055 UpdateRenderingStatePre(info
, cell
);
1057 xlocal
, ylocal
, view_y1
, view_y2
,
1059 UpdateRenderingStatePost(info
, cell
);
1063 // the cell is off-screen, proceed with font+color+etc.
1065 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
1073 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
1074 wxHtmlRenderingInfo
& info
)
1078 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1080 UpdateRenderingStatePre(info
, cell
);
1081 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
1082 UpdateRenderingStatePost(info
, cell
);
1088 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1093 return wxNullColour
;
1098 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1100 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1102 // VZ: I don't know if we should pass absolute or relative coords to
1103 // wxHtmlCell::GetLink()? As the base class version just ignores them
1104 // anyhow, it hardly matters right now but should still be clarified
1105 return cell
? cell
->GetLink(x
, y
) : NULL
;
1110 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1112 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1115 m_LastCell
->SetNext(f
);
1117 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1125 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1127 if (tag
.HasParam(wxT("ALIGN")))
1129 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1131 if (alg
== wxT("CENTER"))
1132 SetAlignHor(wxHTML_ALIGN_CENTER
);
1133 else if (alg
== wxT("LEFT"))
1134 SetAlignHor(wxHTML_ALIGN_LEFT
);
1135 else if (alg
== wxT("JUSTIFY"))
1136 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1137 else if (alg
== wxT("RIGHT"))
1138 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1145 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1147 if (tag
.HasParam(wxT("WIDTH")))
1150 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1152 if (wd
[wd
.Length()-1] == wxT('%'))
1154 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1155 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1159 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1160 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1168 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1172 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1174 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1182 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1183 unsigned flags
) const
1185 if ( flags
& wxHTML_FIND_EXACT
)
1187 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1189 int cx
= cell
->GetPosX(),
1190 cy
= cell
->GetPosY();
1192 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1193 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1195 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1199 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1202 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1204 if ( cell
->IsFormattingCell() )
1206 int cellY
= cell
->GetPosY();
1207 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1208 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1211 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1215 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1217 wxHtmlCell
*c2
, *c
= NULL
;
1218 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1220 if ( cell
->IsFormattingCell() )
1222 int cellY
= cell
->GetPosY();
1223 if (!( cellY
+ cell
->GetHeight() <= y
||
1224 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1226 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1237 bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
1239 const wxMouseEvent
& event
)
1241 #if WXWIN_COMPATIBILITY_2_6
1242 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
1243 return compat
.CallOnMouseClick(this);
1246 void wxHtmlContainerCell::OnMouseClick(wxWindow
*,
1247 int, int, const wxMouseEvent
& event
)
1249 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
1250 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
1251 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
1252 #endif // WXWIN_COMPATIBILITY_2_6
1254 bool retval
= false;
1255 wxHtmlCell
*cell
= FindCellByPos(pos
.x
, pos
.y
);
1257 retval
= cell
->ProcessMouseClick(window
, pos
, event
);
1259 #if WXWIN_COMPATIBILITY_2_6
1260 gs_helperOnMouseClick
->retval
= retval
;
1263 #endif // WXWIN_COMPATIBILITY_2_6
1267 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1272 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1274 c2
= c
->GetFirstTerminal();
1282 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1286 // most common case first:
1287 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1292 wxHtmlCell
*c2
= NULL
;
1293 for (c
= m_Cells
; c
; c
= c
->GetNext())
1295 ctmp
= c
->GetLastTerminal();
1306 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1308 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1310 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1316 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1319 SetIndent(0, wxHTML_INDENT_TOP
);
1321 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1326 wxHtmlContainerCell
*cont
;
1329 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1331 if ( c
->IsTerminalCell() )
1333 if ( !c
->IsFormattingCell() )
1338 cont
= (wxHtmlContainerCell
*)c
;
1339 if ( IsEmptyContainer(cont
) )
1341 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1345 cont
->RemoveExtraSpacing(true, false);
1355 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1358 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1360 c
= (wxHtmlCell
*)arr
[i
];
1361 if ( c
->IsTerminalCell() )
1363 if ( !c
->IsFormattingCell() )
1368 cont
= (wxHtmlContainerCell
*)c
;
1369 if ( IsEmptyContainer(cont
) )
1371 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1375 cont
->RemoveExtraSpacing(false, true);
1387 // --------------------------------------------------------------------------
1389 // --------------------------------------------------------------------------
1391 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1393 void wxHtmlColourCell::Draw(wxDC
& dc
,
1395 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1396 wxHtmlRenderingInfo
& info
)
1398 DrawInvisible(dc
, x
, y
, info
);
1401 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1402 int WXUNUSED(x
), int WXUNUSED(y
),
1403 wxHtmlRenderingInfo
& info
)
1405 wxHtmlRenderingState
& state
= info
.GetState();
1406 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1408 state
.SetFgColour(m_Colour
);
1409 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1410 dc
.SetTextForeground(m_Colour
);
1412 dc
.SetTextForeground(
1413 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1415 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1417 state
.SetBgColour(m_Colour
);
1418 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1420 dc
.SetTextBackground(m_Colour
);
1421 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1425 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1426 dc
.SetTextBackground(c
);
1427 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1435 // ---------------------------------------------------------------------------
1437 // ---------------------------------------------------------------------------
1439 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1441 void wxHtmlFontCell::Draw(wxDC
& dc
,
1442 int WXUNUSED(x
), int WXUNUSED(y
),
1443 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1444 wxHtmlRenderingInfo
& WXUNUSED(info
))
1449 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1450 wxHtmlRenderingInfo
& WXUNUSED(info
))
1462 // ---------------------------------------------------------------------------
1464 // ---------------------------------------------------------------------------
1466 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1468 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1472 m_Wnd
->GetSize(&sx
, &sy
);
1473 m_Width
= sx
, m_Height
= sy
;
1478 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1479 int WXUNUSED(x
), int WXUNUSED(y
),
1480 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1481 wxHtmlRenderingInfo
& WXUNUSED(info
))
1483 int absx
= 0, absy
= 0, stx
, sty
;
1484 wxHtmlCell
*c
= this;
1488 absx
+= c
->GetPosX();
1489 absy
+= c
->GetPosY();
1493 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1494 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1499 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1500 int WXUNUSED(x
), int WXUNUSED(y
),
1501 wxHtmlRenderingInfo
& WXUNUSED(info
))
1503 int absx
= 0, absy
= 0, stx
, sty
;
1504 wxHtmlCell
*c
= this;
1508 absx
+= c
->GetPosX();
1509 absy
+= c
->GetPosY();
1513 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1514 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1519 void wxHtmlWidgetCell::Layout(int w
)
1521 if (m_WidthFloat
!= 0)
1523 m_Width
= (w
* m_WidthFloat
) / 100;
1524 m_Wnd
->SetSize(m_Width
, m_Height
);
1527 wxHtmlCell::Layout(w
);
1532 // ----------------------------------------------------------------------------
1533 // wxHtmlTerminalCellsInterator
1534 // ----------------------------------------------------------------------------
1536 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1543 if ( m_pos
== m_to
)
1549 if ( m_pos
->GetNext() )
1550 m_pos
= m_pos
->GetNext();
1553 // we must go up the hierarchy until we reach container where this
1554 // is not the last child, and then go down to first terminal cell:
1555 while ( m_pos
->GetNext() == NULL
)
1557 m_pos
= m_pos
->GetParent();
1561 m_pos
= m_pos
->GetNext();
1563 while ( m_pos
->GetFirstChild() != NULL
)
1564 m_pos
= m_pos
->GetFirstChild();
1565 } while ( !m_pos
->IsTerminalCell() );