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"
26 #include "wx/html/htmlcell.h"
27 #include "wx/html/htmlwin.h"
28 #include "wx/module.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 void wxHtmlSelection::Set(const wxPoint
& fromPos
, const wxHtmlCell
*fromCell
,
37 const wxPoint
& toPos
, const wxHtmlCell
*toCell
)
39 m_fromCell
= fromCell
;
45 void wxHtmlSelection::Set(const wxHtmlCell
*fromCell
, const wxHtmlCell
*toCell
)
47 wxPoint p1
= fromCell
? fromCell
->GetAbsPos() : wxDefaultPosition
;
48 wxPoint p2
= toCell
? toCell
->GetAbsPos() : wxDefaultPosition
;
51 p2
.x
+= toCell
->GetWidth();
52 p2
.y
+= toCell
->GetHeight();
54 Set(p1
, fromCell
, p2
, toCell
);
58 wxDefaultHtmlRenderingStyle::
59 GetSelectedTextColour(const wxColour
& WXUNUSED(clr
))
61 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
65 wxDefaultHtmlRenderingStyle::
66 GetSelectedTextBgColour(const wxColour
& WXUNUSED(clr
))
68 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
76 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell
, wxObject
)
78 wxHtmlCell::wxHtmlCell() : wxObject()
82 m_Width
= m_Height
= m_Descent
= 0;
83 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
; // <sub> or <sup> mode
84 m_ScriptBaseline
= 0; // <sub> or <sup> baseline
85 m_CanLiveOnPagebreak
= true;
89 wxHtmlCell::~wxHtmlCell()
94 // Update the descent value when whe are in a <sub> or <sup>.
95 // prevbase is the parent base
96 void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode
, long previousBase
)
100 if (mode
== wxHTML_SCRIPT_SUP
)
101 m_ScriptBaseline
= previousBase
- (m_Height
+ 1) / 2;
102 else if (mode
== wxHTML_SCRIPT_SUB
)
103 m_ScriptBaseline
= previousBase
+ (m_Height
+ 1) / 6;
105 m_ScriptBaseline
= 0;
107 m_Descent
+= m_ScriptBaseline
;
110 #if WXWIN_COMPATIBILITY_2_6
112 struct wxHtmlCellOnMouseClickCompatHelper
;
114 static wxHtmlCellOnMouseClickCompatHelper
*gs_helperOnMouseClick
= NULL
;
116 // helper for routing calls to new ProcessMouseClick() method to deprecated
117 // OnMouseClick() method
118 struct wxHtmlCellOnMouseClickCompatHelper
120 wxHtmlCellOnMouseClickCompatHelper(wxHtmlWindowInterface
*window_
,
122 const wxMouseEvent
& event_
)
123 : window(window_
), pos(pos_
), event(event_
), retval(false)
127 bool CallOnMouseClick(wxHtmlCell
*cell
)
129 wxHtmlCellOnMouseClickCompatHelper
*oldHelper
= gs_helperOnMouseClick
;
130 gs_helperOnMouseClick
= this;
133 window
? window
->GetHTMLWindow() : NULL
,
137 gs_helperOnMouseClick
= oldHelper
;
141 wxHtmlWindowInterface
*window
;
143 const wxMouseEvent
& event
;
146 #endif // WXWIN_COMPATIBILITY_2_6
148 bool wxHtmlCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
150 const wxMouseEvent
& event
)
152 wxCHECK_MSG( window
, false, _T("window interface must be provided") );
154 #if WXWIN_COMPATIBILITY_2_6
155 // NB: this hack puts the body of ProcessMouseClick() into OnMouseClick()
156 // (for which it has to pass the arguments and return value via a
157 // helper variable because these two methods have different
158 // signatures), so that old code overriding OnMouseClick will continue
160 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
161 return compat
.CallOnMouseClick(this);
164 void wxHtmlCell::OnMouseClick(wxWindow
*, int, int, const wxMouseEvent
& event
)
166 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
167 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
168 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
169 #endif // WXWIN_COMPATIBILITY_2_6
171 wxHtmlLinkInfo
*lnk
= GetLink(pos
.x
, pos
.y
);
176 wxHtmlLinkInfo
lnk2(*lnk
);
177 lnk2
.SetEvent(&event
);
178 lnk2
.SetHtmlCell(this);
180 window
->OnHTMLLinkClicked(lnk2
);
184 #if WXWIN_COMPATIBILITY_2_6
185 gs_helperOnMouseClick
->retval
= retval
;
188 #endif // WXWIN_COMPATIBILITY_2_6
191 #if WXWIN_COMPATIBILITY_2_6
192 wxCursor
wxHtmlCell::GetCursor() const
196 #endif // WXWIN_COMPATIBILITY_2_6
198 wxCursor
wxHtmlCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
200 #if WXWIN_COMPATIBILITY_2_6
201 // NB: Older versions of wx used GetCursor() virtual method in place of
202 // GetMouseCursor(interface). This code ensures that user code that
203 // overriden GetCursor() continues to work. The trick is that the base
204 // wxHtmlCell::GetCursor() method simply returns wxNullCursor, so we
205 // know that GetCursor() was overriden iff it returns valid cursor.
206 wxCursor cur
= GetCursor();
209 #endif // WXWIN_COMPATIBILITY_2_6
213 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Link
);
217 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Default
);
222 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) const
224 if ((!m_CanLiveOnPagebreak
) &&
225 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
236 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
238 if (m_Link
) delete m_Link
;
240 if (link
.GetHref() != wxEmptyString
)
241 m_Link
= new wxHtmlLinkInfo(link
);
245 void wxHtmlCell::Layout(int WXUNUSED(w
))
252 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
258 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
259 unsigned flags
) const
261 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
263 return wxConstCast(this, wxHtmlCell
);
267 if ((flags
& wxHTML_FIND_NEAREST_AFTER
) &&
268 (y
< 0 || (y
< 0+m_Height
&& x
< 0+m_Width
)))
269 return wxConstCast(this, wxHtmlCell
);
270 else if ((flags
& wxHTML_FIND_NEAREST_BEFORE
) &&
271 (y
>= 0+m_Height
|| (y
>= 0 && x
>= 0)))
272 return wxConstCast(this, wxHtmlCell
);
279 wxPoint
wxHtmlCell::GetAbsPos(wxHtmlCell
*rootCell
) const
281 wxPoint
p(m_PosX
, m_PosY
);
282 for (wxHtmlCell
*parent
= m_Parent
; parent
&& parent
!= rootCell
;
283 parent
= parent
->m_Parent
)
285 p
.x
+= parent
->m_PosX
;
286 p
.y
+= parent
->m_PosY
;
291 wxHtmlCell
*wxHtmlCell::GetRootCell() const
293 wxHtmlCell
*c
= wxConstCast(this, wxHtmlCell
);
294 while ( c
->m_Parent
)
299 unsigned wxHtmlCell::GetDepth() const
302 for (wxHtmlCell
*p
= m_Parent
; p
; p
= p
->m_Parent
)
307 bool wxHtmlCell::IsBefore(wxHtmlCell
*cell
) const
309 const wxHtmlCell
*c1
= this;
310 const wxHtmlCell
*c2
= cell
;
311 unsigned d1
= GetDepth();
312 unsigned d2
= cell
->GetDepth();
315 for (; d1
!= d2
; d1
-- )
318 for (; d1
!= d2
; d2
-- )
326 if ( c1
->m_Parent
== c2
->m_Parent
)
343 wxFAIL_MSG(_T("Cells are in different trees"));
348 //-----------------------------------------------------------------------------
350 //-----------------------------------------------------------------------------
352 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell
, wxHtmlCell
)
354 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, const wxDC
& dc
) : wxHtmlCell()
357 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
358 SetCanLiveOnPagebreak(false);
359 m_allowLinebreak
= true;
362 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
364 if ( cell
&& m_Parent
== cell
->m_Parent
&&
365 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
367 m_allowLinebreak
= false;
371 // Splits m_Word into up to three parts according to selection, returns
372 // substring before, in and after selection and the points (in relative coords)
373 // where s2 and s3 start:
374 void wxHtmlWordCell::Split(const wxDC
& dc
,
375 const wxPoint
& selFrom
, const wxPoint
& selTo
,
376 unsigned& pos1
, unsigned& pos2
) const
378 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
379 wxDefaultPosition
: selFrom
- GetAbsPos();
380 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
381 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
383 unsigned len
= m_Word
.length();
387 // adjust for cases when the start/end position is completely
391 if ( pt2
.y
>= m_Height
)
396 // implementation using PartialExtents to support fractional widths
398 dc
.GetPartialTextExtents(m_Word
,widths
) ;
399 while( i
< len
&& pt1
.x
>= widths
[i
] )
402 wxCoord charW
, charH
;
403 while ( pt1
.x
> 0 && i
< len
)
405 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
413 #endif // __WXMAC__/!__WXMAC__
418 while( j
< len
&& pt2
.x
>= widths
[j
] )
423 while ( pt2
.x
> 0 && j
< len
)
425 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
433 #endif // __WXMAC__/!__WXMAC__
439 void wxHtmlWordCell::SetSelectionPrivPos(const wxDC
& dc
, wxHtmlSelection
*s
) const
444 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
445 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
448 wxPoint
p(0, m_Word
.length());
450 if ( this == s
->GetFromCell() )
451 p
.x
= p1
; // selection starts here
452 if ( this == s
->GetToCell() )
453 p
.y
= p2
; // selection ends here
455 if ( this == s
->GetFromCell() )
456 s
->SetFromPrivPos(p
);
457 if ( this == s
->GetToCell() )
462 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
465 wxColour fg
= info
.GetState().GetFgColour();
466 wxColour bg
= info
.GetState().GetBgColour();
470 dc
.SetBackgroundMode(wxSOLID
);
471 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
472 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
473 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
478 dc
.SetBackgroundMode(wxTRANSPARENT
);
479 dc
.SetTextForeground(fg
);
480 dc
.SetTextBackground(bg
);
481 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
486 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
487 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
488 wxHtmlRenderingInfo
& info
)
490 #if 0 // useful for debugging
491 dc
.SetPen(*wxBLACK_PEN
);
492 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
495 bool drawSelectionAfterCell
= false;
497 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
499 // Selection changing, we must draw the word piecewise:
500 wxHtmlSelection
*s
= info
.GetSelection();
505 wxPoint priv
= (this == s
->GetFromCell()) ?
506 s
->GetFromPrivPos() : s
->GetToPrivPos();
508 // NB: this is quite a hack: in order to compute selection boundaries
509 // (in word's characters) we must know current font, which is only
510 // possible inside rendering code. Therefore we update the
511 // information here and store it in wxHtmlSelection so that
512 // ConvertToText can use it later:
513 if ( priv
== wxDefaultPosition
)
515 SetSelectionPrivPos(dc
, s
);
516 priv
= (this == s
->GetFromCell()) ?
517 s
->GetFromPrivPos() : s
->GetToPrivPos();
525 txt
= m_Word
.Mid(0, part1
);
526 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
527 dc
.GetTextExtent(txt
, &w
, &h
);
531 SwitchSelState(dc
, info
, true);
533 txt
= m_Word
.Mid(part1
, part2
-part1
);
534 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
536 if ( (size_t)part2
< m_Word
.length() )
538 dc
.GetTextExtent(txt
, &w
, &h
);
540 SwitchSelState(dc
, info
, false);
541 txt
= m_Word
.Mid(part2
);
542 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
545 drawSelectionAfterCell
= true;
549 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
550 // Not changing selection state, draw the word in single mode:
551 if ( selstate
!= wxHTML_SEL_OUT
&&
552 dc
.GetBackgroundMode() != wxSOLID
)
554 SwitchSelState(dc
, info
, true);
556 else if ( selstate
== wxHTML_SEL_OUT
&&
557 dc
.GetBackgroundMode() == wxSOLID
)
559 SwitchSelState(dc
, info
, false);
561 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
562 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
565 // NB: If the text is justified then there is usually some free space
566 // between adjacent cells and drawing the selection only onto cells
567 // would result in ugly unselected spaces. The code below detects
568 // this special case and renders the selection *outside* the sell,
570 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
571 drawSelectionAfterCell
)
573 wxHtmlCell
*nextCell
= m_Next
;
574 while ( nextCell
&& nextCell
->IsFormattingCell() )
575 nextCell
= nextCell
->GetNext();
578 int nextX
= nextCell
->GetPosX();
579 if ( m_PosX
+ m_Width
< nextX
)
581 dc
.SetBrush(dc
.GetBackground());
582 dc
.SetPen(*wxTRANSPARENT_PEN
);
583 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
584 nextX
- m_PosX
- m_Width
, m_Height
);
591 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
593 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
595 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
598 // VZ: we may be called before we had a chance to re-render ourselves
599 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
600 // that this only happens in case of a double/triple click (which
601 // seems to be the case now) and so it makes sense to select the
602 // entire contents of the cell in this case
604 // TODO: but this really needs to be fixed in some better way later...
605 if ( priv
!= wxDefaultPosition
)
609 return m_Word
.Mid(part1
, part2
-part1
);
611 //else: return the whole word below
617 wxCursor
wxHtmlWordCell::GetMouseCursor(wxHtmlWindowInterface
*window
) const
621 return window
->GetHTMLCursor(wxHtmlWindowInterface::HTMLCursor_Text
);
625 return wxHtmlCell::GetMouseCursor(window
);
630 //-----------------------------------------------------------------------------
631 // wxHtmlContainerCell
632 //-----------------------------------------------------------------------------
634 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
636 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
638 m_Cells
= m_LastCell
= NULL
;
641 if (m_Parent
) m_Parent
->InsertCell(this);
642 m_AlignHor
= wxHTML_ALIGN_LEFT
;
643 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
644 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
645 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
646 m_UseBkColour
= false;
649 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
653 wxHtmlContainerCell::~wxHtmlContainerCell()
655 wxHtmlCell
*cell
= m_Cells
;
658 wxHtmlCell
*cellNext
= cell
->GetNext();
666 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
668 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
669 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
670 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
671 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
672 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
678 int wxHtmlContainerCell::GetIndent(int ind
) const
680 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
681 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
682 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
683 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
684 else return -1; /* BUG! Should not be called... */
690 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
693 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
694 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
695 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
696 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
697 if (p
) return wxHTML_UNITS_PERCENT
;
698 else return wxHTML_UNITS_PIXELS
;
703 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
705 if (!m_CanLiveOnPagebreak
)
706 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
710 wxHtmlCell
*c
= GetFirstChild();
712 int pbrk
= *pagebreak
- m_PosY
;
716 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
721 *pagebreak
= pbrk
+ m_PosY
;
728 void wxHtmlContainerCell::Layout(int w
)
730 wxHtmlCell::Layout(w
);
732 if (m_LastLayout
== w
) return;
734 // VS: Any attempt to layout with negative or zero width leads to hell,
735 // but we can't ignore such attempts completely, since it sometimes
736 // happen (e.g. when trying how small a table can be). The best thing we
737 // can do is to set the width of child cells to zero
741 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
743 // this does two things: it recursively calls this code on all
744 // child contrainers and resets children's position to (0,0)
748 wxHtmlCell
*nextCell
;
749 long xpos
= 0, ypos
= m_IndentTop
;
750 int xdelta
= 0, ybasicpos
= 0, ydiff
;
751 int s_width
, nextWordWidth
, s_indent
;
752 int ysizeup
= 0, ysizedown
= 0;
753 int MaxLineWidth
= 0;
754 int curLineWidth
= 0;
764 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
766 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
767 else m_Width
= m_WidthFloat
* w
/ 100;
771 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
772 else m_Width
= m_WidthFloat
;
777 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
778 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
779 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
780 cell
->Layout(m_Width
- (l
+ r
));
789 // adjust indentation:
790 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
791 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
794 wxHtmlCell
*cell
= m_Cells
,
800 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
801 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
802 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
804 ydiff
= cell
->GetHeight() + ybasicpos
;
806 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
807 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
809 // layout nonbreakable run of cells:
810 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
811 xpos
+= cell
->GetWidth();
812 if (!cell
->IsTerminalCell())
814 // Container cell indicates new line
815 if (curLineWidth
> m_MaxTotalWidth
)
816 m_MaxTotalWidth
= curLineWidth
;
818 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
819 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
823 // Normal cell, add maximum cell width to line width
824 curLineWidth
+= cell
->GetMaxTotalWidth();
826 cell
= cell
->GetNext();
828 // compute length of the next word that would be added:
835 nextWordWidth
+= nextCell
->GetWidth();
836 nextCell
= nextCell
->GetNext();
837 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
840 // force new line if occurred:
841 if ((cell
== NULL
) ||
842 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
844 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
845 if (ysizeup
< 0) ysizeup
= 0;
846 if (ysizedown
< 0) ysizedown
= 0;
847 switch (m_AlignHor
) {
848 case wxHTML_ALIGN_LEFT
:
849 case wxHTML_ALIGN_JUSTIFY
:
852 case wxHTML_ALIGN_RIGHT
:
853 xdelta
= 0 + (s_width
- xpos
);
855 case wxHTML_ALIGN_CENTER
:
856 xdelta
= 0 + (s_width
- xpos
) / 2;
859 if (xdelta
< 0) xdelta
= 0;
864 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
868 line
->SetPos(line
->GetPosX() + xdelta
,
869 ypos
+ line
->GetPosY());
870 line
= line
->GetNext();
873 else // align == justify
875 // we have to distribute the extra horz space between the cells
878 // an added complication is that some cells have fixed size and
879 // shouldn't get any increment (it so happens that these cells
880 // also don't allow line break on them which provides with an
881 // easy way to test for this) -- and neither should the cells
882 // adjacent to them as this could result in a visible space
883 // between two cells separated by, e.g. font change, cell which
886 int step
= s_width
- xpos
;
889 // first count the cells which will get extra space
895 for ( c
= line
->GetNext(); c
!= cell
; c
= c
->GetNext() )
897 if ( c
->IsLinebreakAllowed() )
904 // and now extra space to those cells which merit it
907 // first cell on line is not moved:
908 line
->SetPos(line
->GetPosX() + s_indent
,
909 line
->GetPosY() + ypos
);
911 line
= line
->GetNext();
912 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
914 if ( line
->IsLinebreakAllowed() )
916 // offset the next cell relative to this one
917 // thus increasing our size
921 line
->SetPos(line
->GetPosX() + s_indent
+
922 ((n
* step
) / total
),
923 line
->GetPosY() + ypos
);
928 // this will cause the code to enter "else branch" below:
933 if ( step
<= 0 ) // no extra space to distribute
935 // just set the indent properly
938 line
->SetPos(line
->GetPosX() + s_indent
,
939 line
->GetPosY() + ypos
);
940 line
= line
->GetNext();
947 ysizeup
= ysizedown
= 0;
952 // setup height & width, depending on container layout:
953 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
955 if (m_Height
< m_MinHeight
)
957 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
959 int diff
= m_MinHeight
- m_Height
;
960 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
964 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
965 cell
= cell
->GetNext();
968 m_Height
= m_MinHeight
;
971 if (curLineWidth
> m_MaxTotalWidth
)
972 m_MaxTotalWidth
= curLineWidth
;
974 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
975 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
976 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
981 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
982 wxHtmlCell
*cell
) const
984 wxHtmlSelection
*s
= info
.GetSelection();
986 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
988 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
992 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
993 wxHtmlCell
*cell
) const
995 wxHtmlSelection
*s
= info
.GetSelection();
997 if (s
->GetToCell() == cell
)
998 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
999 else if (s
->GetFromCell() == cell
)
1000 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
1003 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
1004 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
1006 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
1007 wxHtmlRenderingInfo
& info
)
1009 #if 0 // useful for debugging
1010 dc
.SetPen(*wxRED_PEN
);
1011 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
1014 int xlocal
= x
+ m_PosX
;
1015 int ylocal
= y
+ m_PosY
;
1019 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
1021 int real_y1
= mMax(ylocal
, view_y1
);
1022 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
1025 dc
.SetPen(*wxTRANSPARENT_PEN
);
1026 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
1031 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
1032 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
1035 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
1036 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
1038 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
1039 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
1044 // draw container's contents:
1045 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1048 // optimize drawing: don't render off-screen content:
1049 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
1050 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
1052 // the cell is visible, draw it:
1053 UpdateRenderingStatePre(info
, cell
);
1055 xlocal
, ylocal
, view_y1
, view_y2
,
1057 UpdateRenderingStatePost(info
, cell
);
1061 // the cell is off-screen, proceed with font+color+etc.
1063 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
1071 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
1072 wxHtmlRenderingInfo
& info
)
1076 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1078 UpdateRenderingStatePre(info
, cell
);
1079 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
1080 UpdateRenderingStatePost(info
, cell
);
1086 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1091 return wxNullColour
;
1096 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1098 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1100 // VZ: I don't know if we should pass absolute or relative coords to
1101 // wxHtmlCell::GetLink()? As the base class version just ignores them
1102 // anyhow, it hardly matters right now but should still be clarified
1103 return cell
? cell
->GetLink(x
, y
) : NULL
;
1108 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1110 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1113 m_LastCell
->SetNext(f
);
1115 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1123 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1125 if (tag
.HasParam(wxT("ALIGN")))
1127 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1129 if (alg
== wxT("CENTER"))
1130 SetAlignHor(wxHTML_ALIGN_CENTER
);
1131 else if (alg
== wxT("LEFT"))
1132 SetAlignHor(wxHTML_ALIGN_LEFT
);
1133 else if (alg
== wxT("JUSTIFY"))
1134 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1135 else if (alg
== wxT("RIGHT"))
1136 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1143 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1145 if (tag
.HasParam(wxT("WIDTH")))
1148 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1150 if (wd
[wd
.length()-1] == wxT('%'))
1152 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1153 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1157 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1158 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1166 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1170 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1172 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1180 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1181 unsigned flags
) const
1183 if ( flags
& wxHTML_FIND_EXACT
)
1185 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1187 int cx
= cell
->GetPosX(),
1188 cy
= cell
->GetPosY();
1190 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1191 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1193 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1197 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1200 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1202 if ( cell
->IsFormattingCell() )
1204 int cellY
= cell
->GetPosY();
1205 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1206 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1209 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1213 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1215 wxHtmlCell
*c2
, *c
= NULL
;
1216 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1218 if ( cell
->IsFormattingCell() )
1220 int cellY
= cell
->GetPosY();
1221 if (!( cellY
+ cell
->GetHeight() <= y
||
1222 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1224 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1235 bool wxHtmlContainerCell::ProcessMouseClick(wxHtmlWindowInterface
*window
,
1237 const wxMouseEvent
& event
)
1239 #if WXWIN_COMPATIBILITY_2_6
1240 wxHtmlCellOnMouseClickCompatHelper
compat(window
, pos
, event
);
1241 return compat
.CallOnMouseClick(this);
1244 void wxHtmlContainerCell::OnMouseClick(wxWindow
*,
1245 int, int, const wxMouseEvent
& event
)
1247 wxCHECK_RET( gs_helperOnMouseClick
, _T("unexpected call to OnMouseClick") );
1248 wxHtmlWindowInterface
*window
= gs_helperOnMouseClick
->window
;
1249 const wxPoint
& pos
= gs_helperOnMouseClick
->pos
;
1250 #endif // WXWIN_COMPATIBILITY_2_6
1252 bool retval
= false;
1253 wxHtmlCell
*cell
= FindCellByPos(pos
.x
, pos
.y
);
1255 retval
= cell
->ProcessMouseClick(window
, pos
, event
);
1257 #if WXWIN_COMPATIBILITY_2_6
1258 gs_helperOnMouseClick
->retval
= retval
;
1261 #endif // WXWIN_COMPATIBILITY_2_6
1265 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1270 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1272 c2
= c
->GetFirstTerminal();
1280 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1284 // most common case first:
1285 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1290 wxHtmlCell
*c2
= NULL
;
1291 for (c
= m_Cells
; c
; c
= c
->GetNext())
1293 ctmp
= c
->GetLastTerminal();
1304 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1306 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1308 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1314 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1317 SetIndent(0, wxHTML_INDENT_TOP
);
1319 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1324 wxHtmlContainerCell
*cont
;
1327 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1329 if ( c
->IsTerminalCell() )
1331 if ( !c
->IsFormattingCell() )
1336 cont
= (wxHtmlContainerCell
*)c
;
1337 if ( IsEmptyContainer(cont
) )
1339 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1343 cont
->RemoveExtraSpacing(true, false);
1353 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1356 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1358 c
= (wxHtmlCell
*)arr
[i
];
1359 if ( c
->IsTerminalCell() )
1361 if ( !c
->IsFormattingCell() )
1366 cont
= (wxHtmlContainerCell
*)c
;
1367 if ( IsEmptyContainer(cont
) )
1369 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1373 cont
->RemoveExtraSpacing(false, true);
1385 // --------------------------------------------------------------------------
1387 // --------------------------------------------------------------------------
1389 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1391 void wxHtmlColourCell::Draw(wxDC
& dc
,
1393 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1394 wxHtmlRenderingInfo
& info
)
1396 DrawInvisible(dc
, x
, y
, info
);
1399 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1400 int WXUNUSED(x
), int WXUNUSED(y
),
1401 wxHtmlRenderingInfo
& info
)
1403 wxHtmlRenderingState
& state
= info
.GetState();
1404 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1406 state
.SetFgColour(m_Colour
);
1407 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1408 dc
.SetTextForeground(m_Colour
);
1410 dc
.SetTextForeground(
1411 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1413 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1415 state
.SetBgColour(m_Colour
);
1416 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1418 dc
.SetTextBackground(m_Colour
);
1419 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1423 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1424 dc
.SetTextBackground(c
);
1425 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1433 // ---------------------------------------------------------------------------
1435 // ---------------------------------------------------------------------------
1437 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1439 void wxHtmlFontCell::Draw(wxDC
& dc
,
1440 int WXUNUSED(x
), int WXUNUSED(y
),
1441 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1442 wxHtmlRenderingInfo
& WXUNUSED(info
))
1447 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1448 wxHtmlRenderingInfo
& WXUNUSED(info
))
1460 // ---------------------------------------------------------------------------
1462 // ---------------------------------------------------------------------------
1464 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1466 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1470 m_Wnd
->GetSize(&sx
, &sy
);
1471 m_Width
= sx
, m_Height
= sy
;
1476 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1477 int WXUNUSED(x
), int WXUNUSED(y
),
1478 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1479 wxHtmlRenderingInfo
& WXUNUSED(info
))
1481 int absx
= 0, absy
= 0, stx
, sty
;
1482 wxHtmlCell
*c
= this;
1486 absx
+= c
->GetPosX();
1487 absy
+= c
->GetPosY();
1491 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1492 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1497 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1498 int WXUNUSED(x
), int WXUNUSED(y
),
1499 wxHtmlRenderingInfo
& WXUNUSED(info
))
1501 int absx
= 0, absy
= 0, stx
, sty
;
1502 wxHtmlCell
*c
= this;
1506 absx
+= c
->GetPosX();
1507 absy
+= c
->GetPosY();
1511 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1512 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1517 void wxHtmlWidgetCell::Layout(int w
)
1519 if (m_WidthFloat
!= 0)
1521 m_Width
= (w
* m_WidthFloat
) / 100;
1522 m_Wnd
->SetSize(m_Width
, m_Height
);
1525 wxHtmlCell::Layout(w
);
1530 // ----------------------------------------------------------------------------
1531 // wxHtmlTerminalCellsInterator
1532 // ----------------------------------------------------------------------------
1534 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1541 if ( m_pos
== m_to
)
1547 if ( m_pos
->GetNext() )
1548 m_pos
= m_pos
->GetNext();
1551 // we must go up the hierarchy until we reach container where this
1552 // is not the last child, and then go down to first terminal cell:
1553 while ( m_pos
->GetNext() == NULL
)
1555 m_pos
= m_pos
->GetParent();
1559 m_pos
= m_pos
->GetNext();
1561 while ( m_pos
->GetFirstChild() != NULL
)
1562 m_pos
= m_pos
->GetFirstChild();
1563 } while ( !m_pos
->IsTerminalCell() );