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 static wxCursor
*gs_cursorLink
= NULL
;
39 static wxCursor
*gs_cursorText
= NULL
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 void wxHtmlSelection::Set(const wxPoint
& fromPos
, const wxHtmlCell
*fromCell
,
47 const wxPoint
& toPos
, const wxHtmlCell
*toCell
)
49 m_fromCell
= fromCell
;
55 void wxHtmlSelection::Set(const wxHtmlCell
*fromCell
, const wxHtmlCell
*toCell
)
57 wxPoint p1
= fromCell
? fromCell
->GetAbsPos() : wxDefaultPosition
;
58 wxPoint p2
= toCell
? toCell
->GetAbsPos() : wxDefaultPosition
;
61 p2
.x
+= toCell
->GetWidth();
62 p2
.y
+= toCell
->GetHeight();
64 Set(p1
, fromCell
, p2
, toCell
);
68 wxDefaultHtmlRenderingStyle::
69 GetSelectedTextColour(const wxColour
& WXUNUSED(clr
))
71 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
75 wxDefaultHtmlRenderingStyle::
76 GetSelectedTextBgColour(const wxColour
& WXUNUSED(clr
))
78 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell
, wxObject
)
88 wxHtmlCell::wxHtmlCell() : wxObject()
92 m_Width
= m_Height
= m_Descent
= 0;
93 m_ScriptMode
= wxHTML_SCRIPT_NORMAL
; // <sub> or <sup> mode
94 m_ScriptBaseline
= 0; // <sub> or <sup> baseline
95 m_CanLiveOnPagebreak
= true;
99 wxHtmlCell::~wxHtmlCell()
104 // Update the descent value when whe are in a <sub> or <sup>.
105 // prevbase is the parent base
106 void wxHtmlCell::SetScriptMode(wxHtmlScriptMode mode
, long previousBase
)
110 if (mode
== wxHTML_SCRIPT_SUP
)
111 m_ScriptBaseline
= previousBase
- (m_Height
+ 1) / 2;
112 else if (mode
== wxHTML_SCRIPT_SUB
)
113 m_ScriptBaseline
= previousBase
+ (m_Height
+ 1) / 6;
115 m_ScriptBaseline
= 0;
117 m_Descent
+= m_ScriptBaseline
;
120 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
121 const wxMouseEvent
& event
)
123 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
126 wxHtmlLinkInfo
lnk2(*lnk
);
127 lnk2
.SetEvent(&event
);
128 lnk2
.SetHtmlCell(this);
130 // note : this cast is legal because parent is *always* wxHtmlWindow
131 wxStaticCast(parent
, wxHtmlWindow
)->OnLinkClicked(lnk2
);
136 wxCursor
wxHtmlCell::GetCursor() const
140 if ( !gs_cursorLink
)
141 gs_cursorLink
= new wxCursor(wxCURSOR_HAND
);
142 return *gs_cursorLink
;
145 return *wxSTANDARD_CURSOR
;
149 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) const
151 if ((!m_CanLiveOnPagebreak
) &&
152 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
163 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
165 if (m_Link
) delete m_Link
;
167 if (link
.GetHref() != wxEmptyString
)
168 m_Link
= new wxHtmlLinkInfo(link
);
172 void wxHtmlCell::Layout(int WXUNUSED(w
))
179 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
185 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
186 unsigned flags
) const
188 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
190 return wxConstCast(this, wxHtmlCell
);
194 if ((flags
& wxHTML_FIND_NEAREST_AFTER
) &&
195 (y
< 0 || (y
< 0+m_Height
&& x
< 0+m_Width
)))
196 return wxConstCast(this, wxHtmlCell
);
197 else if ((flags
& wxHTML_FIND_NEAREST_BEFORE
) &&
198 (y
>= 0+m_Height
|| (y
>= 0 && x
>= 0)))
199 return wxConstCast(this, wxHtmlCell
);
206 wxPoint
wxHtmlCell::GetAbsPos() const
208 wxPoint
p(m_PosX
, m_PosY
);
209 for (wxHtmlCell
*parent
= m_Parent
; parent
; parent
= parent
->m_Parent
)
211 p
.x
+= parent
->m_PosX
;
212 p
.y
+= parent
->m_PosY
;
217 unsigned wxHtmlCell::GetDepth() const
220 for (wxHtmlCell
*p
= m_Parent
; p
; p
= p
->m_Parent
)
225 bool wxHtmlCell::IsBefore(wxHtmlCell
*cell
) const
227 const wxHtmlCell
*c1
= this;
228 const wxHtmlCell
*c2
= cell
;
229 unsigned d1
= GetDepth();
230 unsigned d2
= cell
->GetDepth();
233 for (; d1
!= d2
; d1
-- )
236 for (; d1
!= d2
; d2
-- )
244 if ( c1
->m_Parent
== c2
->m_Parent
)
261 wxFAIL_MSG(_T("Cells are in different trees"));
266 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
270 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell
, wxHtmlCell
)
272 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, const wxDC
& dc
) : wxHtmlCell()
275 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
276 SetCanLiveOnPagebreak(false);
277 m_allowLinebreak
= true;
280 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
282 if ( cell
&& m_Parent
== cell
->m_Parent
&&
283 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
285 m_allowLinebreak
= false;
289 // Splits m_Word into up to three parts according to selection, returns
290 // substring before, in and after selection and the points (in relative coords)
291 // where s2 and s3 start:
292 void wxHtmlWordCell::Split(const wxDC
& dc
,
293 const wxPoint
& selFrom
, const wxPoint
& selTo
,
294 unsigned& pos1
, unsigned& pos2
) const
296 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
297 wxDefaultPosition
: selFrom
- GetAbsPos();
298 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
299 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
301 unsigned len
= m_Word
.length();
305 // adjust for cases when the start/end position is completely
309 if ( pt2
.y
>= m_Height
)
314 // implementation using PartialExtents to support fractional widths
316 dc
.GetPartialTextExtents(m_Word
,widths
) ;
317 while( i
< len
&& pt1
.x
>= widths
[i
] )
320 wxCoord charW
, charH
;
321 while ( pt1
.x
> 0 && i
< len
)
323 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
331 #endif // __WXMAC__/!__WXMAC__
336 while( j
< len
&& pt2
.x
>= widths
[j
] )
341 while ( pt2
.x
> 0 && j
< len
)
343 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
351 #endif // __WXMAC__/!__WXMAC__
357 void wxHtmlWordCell::SetSelectionPrivPos(const wxDC
& dc
, wxHtmlSelection
*s
) const
362 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
363 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
366 wxPoint
p(0, m_Word
.length());
368 if ( this == s
->GetFromCell() )
369 p
.x
= p1
; // selection starts here
370 if ( this == s
->GetToCell() )
371 p
.y
= p2
; // selection ends here
373 if ( this == s
->GetFromCell() )
374 s
->SetFromPrivPos(p
);
375 if ( this == s
->GetToCell() )
380 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
383 wxColour fg
= info
.GetState().GetFgColour();
384 wxColour bg
= info
.GetState().GetBgColour();
388 dc
.SetBackgroundMode(wxSOLID
);
389 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
390 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
391 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
396 dc
.SetBackgroundMode(wxTRANSPARENT
);
397 dc
.SetTextForeground(fg
);
398 dc
.SetTextBackground(bg
);
399 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
404 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
405 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
406 wxHtmlRenderingInfo
& info
)
408 #if 0 // useful for debugging
409 dc
.SetPen(*wxBLACK_PEN
);
410 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
413 bool drawSelectionAfterCell
= false;
415 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
417 // Selection changing, we must draw the word piecewise:
418 wxHtmlSelection
*s
= info
.GetSelection();
423 wxPoint priv
= (this == s
->GetFromCell()) ?
424 s
->GetFromPrivPos() : s
->GetToPrivPos();
426 // NB: this is quite a hack: in order to compute selection boundaries
427 // (in word's characters) we must know current font, which is only
428 // possible inside rendering code. Therefore we update the
429 // information here and store it in wxHtmlSelection so that
430 // ConvertToText can use it later:
431 if ( priv
== wxDefaultPosition
)
433 SetSelectionPrivPos(dc
, s
);
434 priv
= (this == s
->GetFromCell()) ?
435 s
->GetFromPrivPos() : s
->GetToPrivPos();
443 txt
= m_Word
.Mid(0, part1
);
444 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
445 dc
.GetTextExtent(txt
, &w
, &h
);
449 SwitchSelState(dc
, info
, true);
451 txt
= m_Word
.Mid(part1
, part2
-part1
);
452 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
454 if ( (size_t)part2
< m_Word
.length() )
456 dc
.GetTextExtent(txt
, &w
, &h
);
458 SwitchSelState(dc
, info
, false);
459 txt
= m_Word
.Mid(part2
);
460 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
463 drawSelectionAfterCell
= true;
467 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
468 // Not changing selection state, draw the word in single mode:
469 if ( selstate
!= wxHTML_SEL_OUT
&&
470 dc
.GetBackgroundMode() != wxSOLID
)
472 SwitchSelState(dc
, info
, true);
474 else if ( selstate
== wxHTML_SEL_OUT
&&
475 dc
.GetBackgroundMode() == wxSOLID
)
477 SwitchSelState(dc
, info
, false);
479 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
480 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
483 // NB: If the text is justified then there is usually some free space
484 // between adjacent cells and drawing the selection only onto cells
485 // would result in ugly unselected spaces. The code below detects
486 // this special case and renders the selection *outside* the sell,
488 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
489 drawSelectionAfterCell
)
491 wxHtmlCell
*nextCell
= m_Next
;
492 while ( nextCell
&& nextCell
->IsFormattingCell() )
493 nextCell
= nextCell
->GetNext();
496 int nextX
= nextCell
->GetPosX();
497 if ( m_PosX
+ m_Width
< nextX
)
499 dc
.SetBrush(dc
.GetBackground());
500 dc
.SetPen(*wxTRANSPARENT_PEN
);
501 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
502 nextX
- m_PosX
- m_Width
, m_Height
);
509 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
511 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
513 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
516 // VZ: we may be called before we had a chance to re-render ourselves
517 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
518 // that this only happens in case of a double/triple click (which
519 // seems to be the case now) and so it makes sense to select the
520 // entire contents of the cell in this case
522 // TODO: but this really needs to be fixed in some better way later...
523 if ( priv
!= wxDefaultPosition
)
527 return m_Word
.Mid(part1
, part2
-part1
);
529 //else: return the whole word below
535 wxCursor
wxHtmlWordCell::GetCursor() const
539 if ( !gs_cursorText
)
540 gs_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
541 return *gs_cursorText
;
544 return wxHtmlCell::GetCursor();
548 //-----------------------------------------------------------------------------
549 // wxHtmlContainerCell
550 //-----------------------------------------------------------------------------
552 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
554 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
556 m_Cells
= m_LastCell
= NULL
;
559 if (m_Parent
) m_Parent
->InsertCell(this);
560 m_AlignHor
= wxHTML_ALIGN_LEFT
;
561 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
562 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
563 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
564 m_UseBkColour
= false;
567 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
571 wxHtmlContainerCell::~wxHtmlContainerCell()
573 wxHtmlCell
*cell
= m_Cells
;
576 wxHtmlCell
*cellNext
= cell
->GetNext();
584 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
586 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
587 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
588 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
589 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
590 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
596 int wxHtmlContainerCell::GetIndent(int ind
) const
598 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
599 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
600 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
601 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
602 else return -1; /* BUG! Should not be called... */
608 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
611 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
612 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
613 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
614 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
615 if (p
) return wxHTML_UNITS_PERCENT
;
616 else return wxHTML_UNITS_PIXELS
;
621 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
623 if (!m_CanLiveOnPagebreak
)
624 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
628 wxHtmlCell
*c
= GetFirstChild();
630 int pbrk
= *pagebreak
- m_PosY
;
634 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
639 *pagebreak
= pbrk
+ m_PosY
;
646 void wxHtmlContainerCell::Layout(int w
)
648 wxHtmlCell::Layout(w
);
650 if (m_LastLayout
== w
) return;
652 // VS: Any attempt to layout with negative or zero width leads to hell,
653 // but we can't ignore such attempts completely, since it sometimes
654 // happen (e.g. when trying how small a table can be). The best thing we
655 // can do is to set the width of child cells to zero
659 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
661 // this does two things: it recursively calls this code on all
662 // child contrainers and resets children's position to (0,0)
666 wxHtmlCell
*nextCell
;
667 long xpos
= 0, ypos
= m_IndentTop
;
668 int xdelta
= 0, ybasicpos
= 0, ydiff
;
669 int s_width
, nextWordWidth
, s_indent
;
670 int ysizeup
= 0, ysizedown
= 0;
671 int MaxLineWidth
= 0;
672 int curLineWidth
= 0;
682 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
684 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
685 else m_Width
= m_WidthFloat
* w
/ 100;
689 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
690 else m_Width
= m_WidthFloat
;
695 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
696 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
697 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
698 cell
->Layout(m_Width
- (l
+ r
));
707 // adjust indentation:
708 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
709 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
712 wxHtmlCell
*cell
= m_Cells
,
718 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
719 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
720 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
722 ydiff
= cell
->GetHeight() + ybasicpos
;
724 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
725 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
727 // layout nonbreakable run of cells:
728 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
729 xpos
+= cell
->GetWidth();
730 if (!cell
->IsTerminalCell())
732 // Container cell indicates new line
733 if (curLineWidth
> m_MaxTotalWidth
)
734 m_MaxTotalWidth
= curLineWidth
;
736 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
737 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
741 // Normal cell, add maximum cell width to line width
742 curLineWidth
+= cell
->GetMaxTotalWidth();
744 cell
= cell
->GetNext();
746 // compute length of the next word that would be added:
753 nextWordWidth
+= nextCell
->GetWidth();
754 nextCell
= nextCell
->GetNext();
755 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
758 // force new line if occurred:
759 if ((cell
== NULL
) ||
760 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
762 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
763 if (ysizeup
< 0) ysizeup
= 0;
764 if (ysizedown
< 0) ysizedown
= 0;
765 switch (m_AlignHor
) {
766 case wxHTML_ALIGN_LEFT
:
767 case wxHTML_ALIGN_JUSTIFY
:
770 case wxHTML_ALIGN_RIGHT
:
771 xdelta
= 0 + (s_width
- xpos
);
773 case wxHTML_ALIGN_CENTER
:
774 xdelta
= 0 + (s_width
- xpos
) / 2;
777 if (xdelta
< 0) xdelta
= 0;
782 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
786 line
->SetPos(line
->GetPosX() + xdelta
,
787 ypos
+ line
->GetPosY());
788 line
= line
->GetNext();
791 else // align == justify
793 // we have to distribute the extra horz space between the cells
796 // an added complication is that some cells have fixed size and
797 // shouldn't get any increment (it so happens that these cells
798 // also don't allow line break on them which provides with an
799 // easy way to test for this) -- and neither should the cells
800 // adjacent to them as this could result in a visible space
801 // between two cells separated by, e.g. font change, cell which
804 int step
= s_width
- xpos
;
807 // first count the cells which will get extra space
813 for ( c
= line
->GetNext(); c
!= cell
; c
= c
->GetNext() )
815 if ( c
->IsLinebreakAllowed() )
822 // and now extra space to those cells which merit it
825 // first cell on line is not moved:
826 line
->SetPos(line
->GetPosX() + s_indent
,
827 line
->GetPosY() + ypos
);
829 line
= line
->GetNext();
830 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
832 if ( line
->IsLinebreakAllowed() )
834 // offset the next cell relative to this one
835 // thus increasing our size
839 line
->SetPos(line
->GetPosX() + s_indent
+
840 ((n
* step
) / total
),
841 line
->GetPosY() + ypos
);
846 // this will cause the code to enter "else branch" below:
851 if ( step
<= 0 ) // no extra space to distribute
853 // just set the indent properly
856 line
->SetPos(line
->GetPosX() + s_indent
,
857 line
->GetPosY() + ypos
);
858 line
= line
->GetNext();
865 ysizeup
= ysizedown
= 0;
870 // setup height & width, depending on container layout:
871 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
873 if (m_Height
< m_MinHeight
)
875 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
877 int diff
= m_MinHeight
- m_Height
;
878 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
882 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
883 cell
= cell
->GetNext();
886 m_Height
= m_MinHeight
;
889 if (curLineWidth
> m_MaxTotalWidth
)
890 m_MaxTotalWidth
= curLineWidth
;
892 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
893 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
894 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
899 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
900 wxHtmlCell
*cell
) const
902 wxHtmlSelection
*s
= info
.GetSelection();
904 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
906 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
910 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
911 wxHtmlCell
*cell
) const
913 wxHtmlSelection
*s
= info
.GetSelection();
915 if (s
->GetToCell() == cell
)
916 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
917 else if (s
->GetFromCell() == cell
)
918 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
921 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
922 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
924 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
925 wxHtmlRenderingInfo
& info
)
927 #if 0 // useful for debugging
928 dc
.SetPen(*wxRED_PEN
);
929 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
932 int xlocal
= x
+ m_PosX
;
933 int ylocal
= y
+ m_PosY
;
937 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
939 int real_y1
= mMax(ylocal
, view_y1
);
940 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
943 dc
.SetPen(*wxTRANSPARENT_PEN
);
944 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
949 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
950 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
953 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
954 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
956 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
957 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
962 // draw container's contents:
963 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
966 // optimize drawing: don't render off-screen content:
967 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
968 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
970 // the cell is visible, draw it:
971 UpdateRenderingStatePre(info
, cell
);
973 xlocal
, ylocal
, view_y1
, view_y2
,
975 UpdateRenderingStatePost(info
, cell
);
979 // the cell is off-screen, proceed with font+color+etc.
981 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
989 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
990 wxHtmlRenderingInfo
& info
)
994 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
996 UpdateRenderingStatePre(info
, cell
);
997 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
998 UpdateRenderingStatePost(info
, cell
);
1004 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1009 return wxNullColour
;
1014 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1016 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1018 // VZ: I don't know if we should pass absolute or relative coords to
1019 // wxHtmlCell::GetLink()? As the base class version just ignores them
1020 // anyhow, it hardly matters right now but should still be clarified
1021 return cell
? cell
->GetLink(x
, y
) : NULL
;
1026 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1028 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1031 m_LastCell
->SetNext(f
);
1033 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1041 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1043 if (tag
.HasParam(wxT("ALIGN")))
1045 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1047 if (alg
== wxT("CENTER"))
1048 SetAlignHor(wxHTML_ALIGN_CENTER
);
1049 else if (alg
== wxT("LEFT"))
1050 SetAlignHor(wxHTML_ALIGN_LEFT
);
1051 else if (alg
== wxT("JUSTIFY"))
1052 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1053 else if (alg
== wxT("RIGHT"))
1054 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1061 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1063 if (tag
.HasParam(wxT("WIDTH")))
1066 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1068 if (wd
[wd
.Length()-1] == wxT('%'))
1070 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1071 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1075 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1076 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1084 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1088 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1090 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1098 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1099 unsigned flags
) const
1101 if ( flags
& wxHTML_FIND_EXACT
)
1103 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1105 int cx
= cell
->GetPosX(),
1106 cy
= cell
->GetPosY();
1108 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1109 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1111 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1115 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1118 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1120 if ( cell
->IsFormattingCell() )
1122 int cellY
= cell
->GetPosY();
1123 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1124 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1127 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1131 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1133 wxHtmlCell
*c2
, *c
= NULL
;
1134 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1136 if ( cell
->IsFormattingCell() )
1138 int cellY
= cell
->GetPosY();
1139 if (!( cellY
+ cell
->GetHeight() <= y
||
1140 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1142 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1153 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
1155 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1157 cell
->OnMouseClick(parent
, x
, y
, event
);
1162 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1167 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1169 c2
= c
->GetFirstTerminal();
1177 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1181 // most common case first:
1182 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1187 wxHtmlCell
*c2
= NULL
;
1188 for (c
= m_Cells
; c
; c
= c
->GetNext())
1190 ctmp
= c
->GetLastTerminal();
1201 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1203 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1205 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1211 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1214 SetIndent(0, wxHTML_INDENT_TOP
);
1216 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1221 wxHtmlContainerCell
*cont
;
1224 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1226 if ( c
->IsTerminalCell() )
1228 if ( !c
->IsFormattingCell() )
1233 cont
= (wxHtmlContainerCell
*)c
;
1234 if ( IsEmptyContainer(cont
) )
1236 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1240 cont
->RemoveExtraSpacing(true, false);
1250 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1253 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1255 c
= (wxHtmlCell
*)arr
[i
];
1256 if ( c
->IsTerminalCell() )
1258 if ( !c
->IsFormattingCell() )
1263 cont
= (wxHtmlContainerCell
*)c
;
1264 if ( IsEmptyContainer(cont
) )
1266 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1270 cont
->RemoveExtraSpacing(false, true);
1282 // --------------------------------------------------------------------------
1284 // --------------------------------------------------------------------------
1286 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1288 void wxHtmlColourCell::Draw(wxDC
& dc
,
1290 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1291 wxHtmlRenderingInfo
& info
)
1293 DrawInvisible(dc
, x
, y
, info
);
1296 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1297 int WXUNUSED(x
), int WXUNUSED(y
),
1298 wxHtmlRenderingInfo
& info
)
1300 wxHtmlRenderingState
& state
= info
.GetState();
1301 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1303 state
.SetFgColour(m_Colour
);
1304 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1305 dc
.SetTextForeground(m_Colour
);
1307 dc
.SetTextForeground(
1308 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1310 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1312 state
.SetBgColour(m_Colour
);
1313 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1315 dc
.SetTextBackground(m_Colour
);
1316 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1320 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1321 dc
.SetTextBackground(c
);
1322 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1330 // ---------------------------------------------------------------------------
1332 // ---------------------------------------------------------------------------
1334 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1336 void wxHtmlFontCell::Draw(wxDC
& dc
,
1337 int WXUNUSED(x
), int WXUNUSED(y
),
1338 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1339 wxHtmlRenderingInfo
& WXUNUSED(info
))
1344 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1345 wxHtmlRenderingInfo
& WXUNUSED(info
))
1357 // ---------------------------------------------------------------------------
1359 // ---------------------------------------------------------------------------
1361 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1363 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1367 m_Wnd
->GetSize(&sx
, &sy
);
1368 m_Width
= sx
, m_Height
= sy
;
1373 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1374 int WXUNUSED(x
), int WXUNUSED(y
),
1375 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1376 wxHtmlRenderingInfo
& WXUNUSED(info
))
1378 int absx
= 0, absy
= 0, stx
, sty
;
1379 wxHtmlCell
*c
= this;
1383 absx
+= c
->GetPosX();
1384 absy
+= c
->GetPosY();
1388 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1389 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1394 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1395 int WXUNUSED(x
), int WXUNUSED(y
),
1396 wxHtmlRenderingInfo
& WXUNUSED(info
))
1398 int absx
= 0, absy
= 0, stx
, sty
;
1399 wxHtmlCell
*c
= this;
1403 absx
+= c
->GetPosX();
1404 absy
+= c
->GetPosY();
1408 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1409 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1414 void wxHtmlWidgetCell::Layout(int w
)
1416 if (m_WidthFloat
!= 0)
1418 m_Width
= (w
* m_WidthFloat
) / 100;
1419 m_Wnd
->SetSize(m_Width
, m_Height
);
1422 wxHtmlCell::Layout(w
);
1427 // ----------------------------------------------------------------------------
1428 // wxHtmlTerminalCellsInterator
1429 // ----------------------------------------------------------------------------
1431 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1438 if ( m_pos
== m_to
)
1444 if ( m_pos
->GetNext() )
1445 m_pos
= m_pos
->GetNext();
1448 // we must go up the hierarchy until we reach container where this
1449 // is not the last child, and then go down to first terminal cell:
1450 while ( m_pos
->GetNext() == NULL
)
1452 m_pos
= m_pos
->GetParent();
1456 m_pos
= m_pos
->GetNext();
1458 while ( m_pos
->GetFirstChild() != NULL
)
1459 m_pos
= m_pos
->GetFirstChild();
1460 } while ( !m_pos
->IsTerminalCell() );
1471 //-----------------------------------------------------------------------------
1473 //-----------------------------------------------------------------------------
1475 class wxHtmlCellModule
: public wxModule
1477 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule
)
1479 wxHtmlCellModule() : wxModule() {}
1480 bool OnInit() { return true; }
1483 wxDELETE(gs_cursorLink
);
1484 wxDELETE(gs_cursorText
);
1488 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule
, wxModule
)