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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "htmlcell.h"
14 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
26 #include "wx/colour.h"
30 #include "wx/html/htmlcell.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/settings.h"
33 #include "wx/module.h"
34 #include "wx/dynarray.h"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static wxCursor
*gs_cursorLink
= NULL
;
43 static wxCursor
*gs_cursorText
= NULL
;
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 void wxHtmlSelection::Set(const wxPoint
& fromPos
, const wxHtmlCell
*fromCell
,
51 const wxPoint
& toPos
, const wxHtmlCell
*toCell
)
53 m_fromCell
= fromCell
;
59 void wxHtmlSelection::Set(const wxHtmlCell
*fromCell
, const wxHtmlCell
*toCell
)
61 wxPoint p1
= fromCell
? fromCell
->GetAbsPos() : wxDefaultPosition
;
62 wxPoint p2
= toCell
? toCell
->GetAbsPos() : wxDefaultPosition
;
65 p2
.x
+= toCell
->GetWidth();
66 p2
.y
+= toCell
->GetHeight();
68 Set(p1
, fromCell
, p2
, toCell
);
72 wxDefaultHtmlRenderingStyle::
73 GetSelectedTextColour(const wxColour
& WXUNUSED(clr
))
75 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
79 wxDefaultHtmlRenderingStyle::
80 GetSelectedTextBgColour(const wxColour
& WXUNUSED(clr
))
82 return wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 IMPLEMENT_ABSTRACT_CLASS(wxHtmlCell
, wxObject
)
92 wxHtmlCell::wxHtmlCell() : wxObject()
96 m_Width
= m_Height
= m_Descent
= 0;
97 m_CanLiveOnPagebreak
= true;
101 wxHtmlCell::~wxHtmlCell()
107 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
108 const wxMouseEvent
& event
)
110 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
113 wxHtmlLinkInfo
lnk2(*lnk
);
114 lnk2
.SetEvent(&event
);
115 lnk2
.SetHtmlCell(this);
117 // note : this cast is legal because parent is *always* wxHtmlWindow
118 wxStaticCast(parent
, wxHtmlWindow
)->OnLinkClicked(lnk2
);
123 wxCursor
wxHtmlCell::GetCursor() const
127 if ( !gs_cursorLink
)
128 gs_cursorLink
= new wxCursor(wxCURSOR_HAND
);
129 return *gs_cursorLink
;
132 return *wxSTANDARD_CURSOR
;
136 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) const
138 if ((!m_CanLiveOnPagebreak
) &&
139 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
150 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
152 if (m_Link
) delete m_Link
;
154 if (link
.GetHref() != wxEmptyString
)
155 m_Link
= new wxHtmlLinkInfo(link
);
159 void wxHtmlCell::Layout(int WXUNUSED(w
))
166 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
172 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
173 unsigned flags
) const
175 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
177 return wxConstCast(this, wxHtmlCell
);
181 if ((flags
& wxHTML_FIND_NEAREST_AFTER
) &&
182 (y
< 0 || (y
< 0+m_Height
&& x
< 0+m_Width
)))
183 return wxConstCast(this, wxHtmlCell
);
184 else if ((flags
& wxHTML_FIND_NEAREST_BEFORE
) &&
185 (y
>= 0+m_Height
|| (y
>= 0 && x
>= 0)))
186 return wxConstCast(this, wxHtmlCell
);
193 wxPoint
wxHtmlCell::GetAbsPos() const
195 wxPoint
p(m_PosX
, m_PosY
);
196 for (wxHtmlCell
*parent
= m_Parent
; parent
; parent
= parent
->m_Parent
)
198 p
.x
+= parent
->m_PosX
;
199 p
.y
+= parent
->m_PosY
;
204 unsigned wxHtmlCell::GetDepth() const
207 for (wxHtmlCell
*p
= m_Parent
; p
; p
= p
->m_Parent
)
212 bool wxHtmlCell::IsBefore(wxHtmlCell
*cell
) const
214 const wxHtmlCell
*c1
= this;
215 const wxHtmlCell
*c2
= cell
;
216 unsigned d1
= GetDepth();
217 unsigned d2
= cell
->GetDepth();
220 for (; d1
!= d2
; d1
-- )
223 for (; d1
!= d2
; d2
-- )
231 if ( c1
->m_Parent
== c2
->m_Parent
)
248 wxFAIL_MSG(_T("Cells are in different trees"));
253 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
257 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell
, wxHtmlCell
)
259 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
262 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
263 SetCanLiveOnPagebreak(false);
264 m_allowLinebreak
= true;
267 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
269 if ( cell
&& m_Parent
== cell
->m_Parent
&&
270 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
272 m_allowLinebreak
= false;
276 // Splits m_Word into up to three parts according to selection, returns
277 // substring before, in and after selection and the points (in relative coords)
278 // where s2 and s3 start:
279 void wxHtmlWordCell::Split(wxDC
& dc
,
280 const wxPoint
& selFrom
, const wxPoint
& selTo
,
281 unsigned& pos1
, unsigned& pos2
) const
283 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
284 wxDefaultPosition
: selFrom
- GetAbsPos();
285 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
286 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
288 unsigned len
= m_Word
.length();
292 // adjust for cases when the start/end position is completely
296 if ( pt2
.y
>= m_Height
)
301 // implementation using PartialExtents to support fractional widths
303 dc
.GetPartialTextExtents(m_Word
,widths
) ;
304 while( i
< len
&& pt1
.x
>= widths
[i
] )
307 wxCoord charW
, charH
;
308 while ( pt1
.x
> 0 && i
< len
)
310 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
318 #endif // __WXMAC__/!__WXMAC__
323 while( j
< len
&& pt2
.x
>= widths
[j
] )
328 while ( pt2
.x
> 0 && j
< len
)
330 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
338 #endif // __WXMAC__/!__WXMAC__
344 void wxHtmlWordCell::SetSelectionPrivPos(wxDC
& dc
, wxHtmlSelection
*s
) const
349 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
350 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
353 wxPoint
p(0, m_Word
.length());
355 if ( this == s
->GetFromCell() )
356 p
.x
= p1
; // selection starts here
357 if ( this == s
->GetToCell() )
358 p
.y
= p2
; // selection ends here
360 if ( this == s
->GetFromCell() )
361 s
->SetFromPrivPos(p
);
362 if ( this == s
->GetToCell() )
367 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
370 wxColour fg
= info
.GetState().GetFgColour();
371 wxColour bg
= info
.GetState().GetBgColour();
375 dc
.SetBackgroundMode(wxSOLID
);
376 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
377 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
378 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
383 dc
.SetBackgroundMode(wxTRANSPARENT
);
384 dc
.SetTextForeground(fg
);
385 dc
.SetTextBackground(bg
);
386 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
391 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
392 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
393 wxHtmlRenderingInfo
& info
)
395 #if 0 // useful for debugging
396 dc
.SetPen(*wxBLACK_PEN
);
397 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
400 bool drawSelectionAfterCell
= false;
402 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
404 // Selection changing, we must draw the word piecewise:
405 wxHtmlSelection
*s
= info
.GetSelection();
410 wxPoint priv
= (this == s
->GetFromCell()) ?
411 s
->GetFromPrivPos() : s
->GetToPrivPos();
413 // NB: this is quite a hack: in order to compute selection boundaries
414 // (in word's characters) we must know current font, which is only
415 // possible inside rendering code. Therefore we update the
416 // information here and store it in wxHtmlSelection so that
417 // ConvertToText can use it later:
418 if ( priv
== wxDefaultPosition
)
420 SetSelectionPrivPos(dc
, s
);
421 priv
= (this == s
->GetFromCell()) ?
422 s
->GetFromPrivPos() : s
->GetToPrivPos();
430 txt
= m_Word
.Mid(0, part1
);
431 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
432 dc
.GetTextExtent(txt
, &w
, &h
);
436 SwitchSelState(dc
, info
, true);
438 txt
= m_Word
.Mid(part1
, part2
-part1
);
439 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
441 if ( (size_t)part2
< m_Word
.length() )
443 dc
.GetTextExtent(txt
, &w
, &h
);
445 SwitchSelState(dc
, info
, false);
446 txt
= m_Word
.Mid(part2
);
447 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
450 drawSelectionAfterCell
= true;
454 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
455 // Not changing selection state, draw the word in single mode:
456 if ( selstate
!= wxHTML_SEL_OUT
&&
457 dc
.GetBackgroundMode() != wxSOLID
)
459 SwitchSelState(dc
, info
, true);
461 else if ( selstate
== wxHTML_SEL_OUT
&&
462 dc
.GetBackgroundMode() == wxSOLID
)
464 SwitchSelState(dc
, info
, false);
466 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
467 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
470 // NB: If the text is justified then there is usually some free space
471 // between adjacent cells and drawing the selection only onto cells
472 // would result in ugly unselected spaces. The code below detects
473 // this special case and renders the selection *outside* the sell,
475 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
476 drawSelectionAfterCell
)
478 wxHtmlCell
*nextCell
= m_Next
;
479 while ( nextCell
&& nextCell
->IsFormattingCell() )
480 nextCell
= nextCell
->GetNext();
483 int nextX
= nextCell
->GetPosX();
484 if ( m_PosX
+ m_Width
< nextX
)
486 dc
.SetBrush(dc
.GetBackground());
487 dc
.SetPen(*wxTRANSPARENT_PEN
);
488 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
489 nextX
- m_PosX
- m_Width
, m_Height
);
496 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
498 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
500 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
503 // VZ: we may be called before we had a chance to re-render ourselves
504 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
505 // that this only happens in case of a double/triple click (which
506 // seems to be the case now) and so it makes sense to select the
507 // entire contents of the cell in this case
509 // TODO: but this really needs to be fixed in some better way later...
510 if ( priv
!= wxDefaultPosition
)
514 return m_Word
.Mid(part1
, part2
-part1
);
516 //else: return the whole word below
522 wxCursor
wxHtmlWordCell::GetCursor() const
526 if ( !gs_cursorText
)
527 gs_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
528 return *gs_cursorText
;
531 return wxHtmlCell::GetCursor();
535 //-----------------------------------------------------------------------------
536 // wxHtmlContainerCell
537 //-----------------------------------------------------------------------------
539 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
541 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
543 m_Cells
= m_LastCell
= NULL
;
546 if (m_Parent
) m_Parent
->InsertCell(this);
547 m_AlignHor
= wxHTML_ALIGN_LEFT
;
548 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
549 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
550 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
551 m_UseBkColour
= false;
554 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
558 wxHtmlContainerCell::~wxHtmlContainerCell()
560 wxHtmlCell
*cell
= m_Cells
;
563 wxHtmlCell
*cellNext
= cell
->GetNext();
571 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
573 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
574 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
575 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
576 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
577 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
583 int wxHtmlContainerCell::GetIndent(int ind
) const
585 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
586 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
587 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
588 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
589 else return -1; /* BUG! Should not be called... */
595 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
598 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
599 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
600 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
601 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
602 if (p
) return wxHTML_UNITS_PERCENT
;
603 else return wxHTML_UNITS_PIXELS
;
608 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
610 if (!m_CanLiveOnPagebreak
)
611 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
615 wxHtmlCell
*c
= GetFirstChild();
617 int pbrk
= *pagebreak
- m_PosY
;
621 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
626 *pagebreak
= pbrk
+ m_PosY
;
633 void wxHtmlContainerCell::Layout(int w
)
635 wxHtmlCell::Layout(w
);
637 if (m_LastLayout
== w
) return;
639 // VS: Any attempt to layout with negative or zero width leads to hell,
640 // but we can't ignore such attempts completely, since it sometimes
641 // happen (e.g. when trying how small a table can be). The best thing we
642 // can do is to set the width of child cells to zero
646 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
648 // this does two things: it recursively calls this code on all
649 // child contrainers and resets children's position to (0,0)
653 wxHtmlCell
*cell
= m_Cells
,
655 wxHtmlCell
*nextCell
;
656 long xpos
= 0, ypos
= m_IndentTop
;
657 int xdelta
= 0, ybasicpos
= 0, ydiff
;
658 int s_width
, nextWordWidth
, s_indent
;
659 int ysizeup
= 0, ysizedown
= 0;
660 int MaxLineWidth
= 0;
661 int curLineWidth
= 0;
671 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
673 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
674 else m_Width
= m_WidthFloat
* w
/ 100;
678 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
679 else m_Width
= m_WidthFloat
;
684 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
685 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
686 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
687 cell
->Layout(m_Width
- (l
+ r
));
696 // adjust indentation:
697 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
698 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
705 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
706 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
707 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
709 ydiff
= cell
->GetHeight() + ybasicpos
;
711 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
712 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
714 // layout nonbreakable run of cells:
715 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
716 xpos
+= cell
->GetWidth();
717 if (!cell
->IsTerminalCell())
719 // Container cell indicates new line
720 if (curLineWidth
> m_MaxTotalWidth
)
721 m_MaxTotalWidth
= curLineWidth
;
723 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
724 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
728 // Normal cell, add maximum cell width to line width
729 curLineWidth
+= cell
->GetMaxTotalWidth();
731 cell
= cell
->GetNext();
733 // compute length of the next word that would be added:
740 nextWordWidth
+= nextCell
->GetWidth();
741 nextCell
= nextCell
->GetNext();
742 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
745 // force new line if occurred:
746 if ((cell
== NULL
) ||
747 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
749 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
750 if (ysizeup
< 0) ysizeup
= 0;
751 if (ysizedown
< 0) ysizedown
= 0;
752 switch (m_AlignHor
) {
753 case wxHTML_ALIGN_LEFT
:
754 case wxHTML_ALIGN_JUSTIFY
:
757 case wxHTML_ALIGN_RIGHT
:
758 xdelta
= 0 + (s_width
- xpos
);
760 case wxHTML_ALIGN_CENTER
:
761 xdelta
= 0 + (s_width
- xpos
) / 2;
764 if (xdelta
< 0) xdelta
= 0;
769 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
773 line
->SetPos(line
->GetPosX() + xdelta
,
774 ypos
+ line
->GetPosY());
775 line
= line
->GetNext();
778 else // align == justify
780 // we have to distribute the extra horz space between the cells
783 // an added complication is that some cells have fixed size and
784 // shouldn't get any increment (it so happens that these cells
785 // also don't allow line break on them which provides with an
786 // easy way to test for this) -- and neither should the cells
787 // adjacent to them as this could result in a visible space
788 // between two cells separated by, e.g. font change, cell which
791 int step
= s_width
- xpos
;
794 // first count the cells which will get extra space
800 for ( c
= line
->GetNext(); c
!= cell
; c
= c
->GetNext() )
802 if ( c
->IsLinebreakAllowed() )
809 // and now extra space to those cells which merit it
812 // first cell on line is not moved:
813 line
->SetPos(line
->GetPosX() + s_indent
,
814 line
->GetPosY() + ypos
);
816 line
= line
->GetNext();
817 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
819 if ( line
->IsLinebreakAllowed() )
821 // offset the next cell relative to this one
822 // thus increasing our size
826 line
->SetPos(line
->GetPosX() + s_indent
+
827 ((n
* step
) / total
),
828 line
->GetPosY() + ypos
);
833 // this will cause the code to enter "else branch" below:
838 if ( step
<= 0 ) // no extra space to distribute
840 // just set the indent properly
843 line
->SetPos(line
->GetPosX() + s_indent
,
844 line
->GetPosY() + ypos
);
845 line
= line
->GetNext();
852 ysizeup
= ysizedown
= 0;
857 // setup height & width, depending on container layout:
858 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
860 if (m_Height
< m_MinHeight
)
862 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
864 int diff
= m_MinHeight
- m_Height
;
865 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
869 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
870 cell
= cell
->GetNext();
873 m_Height
= m_MinHeight
;
876 if (curLineWidth
> m_MaxTotalWidth
)
877 m_MaxTotalWidth
= curLineWidth
;
879 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
880 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
881 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
886 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
887 wxHtmlCell
*cell
) const
889 wxHtmlSelection
*s
= info
.GetSelection();
891 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
893 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
897 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
898 wxHtmlCell
*cell
) const
900 wxHtmlSelection
*s
= info
.GetSelection();
902 if (s
->GetToCell() == cell
)
903 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
904 else if (s
->GetFromCell() == cell
)
905 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
908 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
909 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
911 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
912 wxHtmlRenderingInfo
& info
)
914 #if 0 // useful for debugging
915 dc
.SetPen(*wxRED_PEN
);
916 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
919 int xlocal
= x
+ m_PosX
;
920 int ylocal
= y
+ m_PosY
;
924 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
926 int real_y1
= mMax(ylocal
, view_y1
);
927 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
930 dc
.SetPen(*wxTRANSPARENT_PEN
);
931 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
936 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
937 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
940 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
941 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
943 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
944 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
949 // draw container's contents:
950 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
953 // optimize drawing: don't render off-screen content:
954 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
955 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
957 // the cell is visible, draw it:
958 UpdateRenderingStatePre(info
, cell
);
960 xlocal
, ylocal
, view_y1
, view_y2
,
962 UpdateRenderingStatePost(info
, cell
);
966 // the cell is off-screen, proceed with font+color+etc.
968 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
976 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
977 wxHtmlRenderingInfo
& info
)
981 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
983 UpdateRenderingStatePre(info
, cell
);
984 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
985 UpdateRenderingStatePost(info
, cell
);
991 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1001 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1003 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1005 // VZ: I don't know if we should pass absolute or relative coords to
1006 // wxHtmlCell::GetLink()? As the base class version just ignores them
1007 // anyhow, it hardly matters right now but should still be clarified
1008 return cell
? cell
->GetLink(x
, y
) : NULL
;
1013 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1015 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1018 m_LastCell
->SetNext(f
);
1020 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1028 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1030 if (tag
.HasParam(wxT("ALIGN")))
1032 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1034 if (alg
== wxT("CENTER"))
1035 SetAlignHor(wxHTML_ALIGN_CENTER
);
1036 else if (alg
== wxT("LEFT"))
1037 SetAlignHor(wxHTML_ALIGN_LEFT
);
1038 else if (alg
== wxT("JUSTIFY"))
1039 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1040 else if (alg
== wxT("RIGHT"))
1041 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1048 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1050 if (tag
.HasParam(wxT("WIDTH")))
1053 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1055 if (wd
[wd
.Length()-1] == wxT('%'))
1057 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1058 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1062 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1063 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1071 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1075 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1077 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1085 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1086 unsigned flags
) const
1088 if ( flags
& wxHTML_FIND_EXACT
)
1090 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1092 int cx
= cell
->GetPosX(),
1093 cy
= cell
->GetPosY();
1095 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1096 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1098 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1102 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1105 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1107 if ( cell
->IsFormattingCell() )
1109 int cellY
= cell
->GetPosY();
1110 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1111 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1114 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1118 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1120 wxHtmlCell
*c2
, *c
= NULL
;
1121 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1123 if ( cell
->IsFormattingCell() )
1125 int cellY
= cell
->GetPosY();
1126 if (!( cellY
+ cell
->GetHeight() <= y
||
1127 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1129 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1140 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
1142 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1144 cell
->OnMouseClick(parent
, x
, y
, event
);
1149 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1154 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1156 c2
= c
->GetFirstTerminal();
1164 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1168 // most common case first:
1169 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1174 wxHtmlCell
*c2
= NULL
;
1175 for (c
= m_Cells
; c
; c
= c
->GetNext())
1177 ctmp
= c
->GetLastTerminal();
1188 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1190 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1192 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1198 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1201 SetIndent(0, wxHTML_INDENT_TOP
);
1203 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1208 wxHtmlContainerCell
*cont
;
1211 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1213 if ( c
->IsTerminalCell() )
1215 if ( !c
->IsFormattingCell() )
1220 cont
= (wxHtmlContainerCell
*)c
;
1221 if ( IsEmptyContainer(cont
) )
1223 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1227 cont
->RemoveExtraSpacing(true, false);
1237 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1240 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1242 c
= (wxHtmlCell
*)arr
[i
];
1243 if ( c
->IsTerminalCell() )
1245 if ( !c
->IsFormattingCell() )
1250 cont
= (wxHtmlContainerCell
*)c
;
1251 if ( IsEmptyContainer(cont
) )
1253 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1257 cont
->RemoveExtraSpacing(false, true);
1269 // --------------------------------------------------------------------------
1271 // --------------------------------------------------------------------------
1273 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1275 void wxHtmlColourCell::Draw(wxDC
& dc
,
1277 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1278 wxHtmlRenderingInfo
& info
)
1280 DrawInvisible(dc
, x
, y
, info
);
1283 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1284 int WXUNUSED(x
), int WXUNUSED(y
),
1285 wxHtmlRenderingInfo
& info
)
1287 wxHtmlRenderingState
& state
= info
.GetState();
1288 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1290 state
.SetFgColour(m_Colour
);
1291 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1292 dc
.SetTextForeground(m_Colour
);
1294 dc
.SetTextForeground(
1295 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1297 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1299 state
.SetBgColour(m_Colour
);
1300 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1302 dc
.SetTextBackground(m_Colour
);
1303 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1307 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1308 dc
.SetTextBackground(c
);
1309 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1317 // ---------------------------------------------------------------------------
1319 // ---------------------------------------------------------------------------
1321 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1323 void wxHtmlFontCell::Draw(wxDC
& dc
,
1324 int WXUNUSED(x
), int WXUNUSED(y
),
1325 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1326 wxHtmlRenderingInfo
& WXUNUSED(info
))
1331 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1332 wxHtmlRenderingInfo
& WXUNUSED(info
))
1344 // ---------------------------------------------------------------------------
1346 // ---------------------------------------------------------------------------
1348 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1350 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1354 m_Wnd
->GetSize(&sx
, &sy
);
1355 m_Width
= sx
, m_Height
= sy
;
1360 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1361 int WXUNUSED(x
), int WXUNUSED(y
),
1362 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1363 wxHtmlRenderingInfo
& WXUNUSED(info
))
1365 int absx
= 0, absy
= 0, stx
, sty
;
1366 wxHtmlCell
*c
= this;
1370 absx
+= c
->GetPosX();
1371 absy
+= c
->GetPosY();
1375 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1376 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1381 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1382 int WXUNUSED(x
), int WXUNUSED(y
),
1383 wxHtmlRenderingInfo
& WXUNUSED(info
))
1385 int absx
= 0, absy
= 0, stx
, sty
;
1386 wxHtmlCell
*c
= this;
1390 absx
+= c
->GetPosX();
1391 absy
+= c
->GetPosY();
1395 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1396 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1401 void wxHtmlWidgetCell::Layout(int w
)
1403 if (m_WidthFloat
!= 0)
1405 m_Width
= (w
* m_WidthFloat
) / 100;
1406 m_Wnd
->SetSize(m_Width
, m_Height
);
1409 wxHtmlCell::Layout(w
);
1414 // ----------------------------------------------------------------------------
1415 // wxHtmlTerminalCellsInterator
1416 // ----------------------------------------------------------------------------
1418 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1425 if ( m_pos
== m_to
)
1431 if ( m_pos
->GetNext() )
1432 m_pos
= m_pos
->GetNext();
1435 // we must go up the hierarchy until we reach container where this
1436 // is not the last child, and then go down to first terminal cell:
1437 while ( m_pos
->GetNext() == NULL
)
1439 m_pos
= m_pos
->GetParent();
1443 m_pos
= m_pos
->GetNext();
1445 while ( m_pos
->GetFirstChild() != NULL
)
1446 m_pos
= m_pos
->GetFirstChild();
1447 } while ( !m_pos
->IsTerminalCell() );
1458 //-----------------------------------------------------------------------------
1460 //-----------------------------------------------------------------------------
1462 class wxHtmlCellModule
: public wxModule
1464 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule
)
1466 wxHtmlCellModule() : wxModule() {}
1467 bool OnInit() { return true; }
1470 wxDELETE(gs_cursorLink
);
1471 wxDELETE(gs_cursorText
);
1475 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule
, wxModule
)