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 wxCoord charW
, charH
;
289 unsigned len
= m_Word
.length();
293 // adjust for cases when the start/end position is completely
297 if ( pt2
.y
>= m_Height
)
300 // implementation using PartialExtents to support fractional widths
302 dc
.GetPartialTextExtents(m_Word
,widths
) ;
307 while( i
< len
&& pt1
.x
>= widths
[i
] )
310 while ( pt1
.x
> 0 && i
< len
)
312 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
325 while( j
< len
&& pt2
.x
>= widths
[j
] )
330 while ( pt2
.x
> 0 && j
< len
)
332 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
346 void wxHtmlWordCell::SetSelectionPrivPos(wxDC
& dc
, wxHtmlSelection
*s
) const
351 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
352 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
355 wxPoint
p(0, m_Word
.length());
357 if ( this == s
->GetFromCell() )
358 p
.x
= p1
; // selection starts here
359 if ( this == s
->GetToCell() )
360 p
.y
= p2
; // selection ends here
362 if ( this == s
->GetFromCell() )
363 s
->SetFromPrivPos(p
);
364 if ( this == s
->GetToCell() )
369 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
372 wxColour fg
= info
.GetState().GetFgColour();
373 wxColour bg
= info
.GetState().GetBgColour();
377 dc
.SetBackgroundMode(wxSOLID
);
378 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
379 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
380 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
385 dc
.SetBackgroundMode(wxTRANSPARENT
);
386 dc
.SetTextForeground(fg
);
387 dc
.SetTextBackground(bg
);
388 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
393 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
394 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
395 wxHtmlRenderingInfo
& info
)
397 #if 0 // useful for debugging
398 dc
.SetPen(*wxBLACK_PEN
);
399 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
402 bool drawSelectionAfterCell
= false;
404 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
406 // Selection changing, we must draw the word piecewise:
407 wxHtmlSelection
*s
= info
.GetSelection();
412 wxPoint priv
= (this == s
->GetFromCell()) ?
413 s
->GetFromPrivPos() : s
->GetToPrivPos();
415 // NB: this is quite a hack: in order to compute selection boundaries
416 // (in word's characters) we must know current font, which is only
417 // possible inside rendering code. Therefore we update the
418 // information here and store it in wxHtmlSelection so that
419 // ConvertToText can use it later:
420 if ( priv
== wxDefaultPosition
)
422 SetSelectionPrivPos(dc
, s
);
423 priv
= (this == s
->GetFromCell()) ?
424 s
->GetFromPrivPos() : s
->GetToPrivPos();
432 txt
= m_Word
.Mid(0, part1
);
433 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
434 dc
.GetTextExtent(txt
, &w
, &h
);
438 SwitchSelState(dc
, info
, true);
440 txt
= m_Word
.Mid(part1
, part2
-part1
);
441 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
443 if ( (size_t)part2
< m_Word
.length() )
445 dc
.GetTextExtent(txt
, &w
, &h
);
447 SwitchSelState(dc
, info
, false);
448 txt
= m_Word
.Mid(part2
);
449 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
452 drawSelectionAfterCell
= true;
456 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
457 // Not changing selection state, draw the word in single mode:
458 if ( selstate
!= wxHTML_SEL_OUT
&&
459 dc
.GetBackgroundMode() != wxSOLID
)
461 SwitchSelState(dc
, info
, true);
463 else if ( selstate
== wxHTML_SEL_OUT
&&
464 dc
.GetBackgroundMode() == wxSOLID
)
466 SwitchSelState(dc
, info
, false);
468 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
469 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
472 // NB: If the text is justified then there is usually some free space
473 // between adjacent cells and drawing the selection only onto cells
474 // would result in ugly unselected spaces. The code below detects
475 // this special case and renders the selection *outside* the sell,
477 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
478 drawSelectionAfterCell
)
480 wxHtmlCell
*nextCell
= m_Next
;
481 while ( nextCell
&& nextCell
->IsFormattingCell() )
482 nextCell
= nextCell
->GetNext();
485 int nextX
= nextCell
->GetPosX();
486 if ( m_PosX
+ m_Width
< nextX
)
488 dc
.SetBrush(dc
.GetBackground());
489 dc
.SetPen(*wxTRANSPARENT_PEN
);
490 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
491 nextX
- m_PosX
- m_Width
, m_Height
);
498 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
500 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
502 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
505 // VZ: we may be called before we had a chance to re-render ourselves
506 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
507 // that this only happens in case of a double/triple click (which
508 // seems to be the case now) and so it makes sense to select the
509 // entire contents of the cell in this case
511 // TODO: but this really needs to be fixed in some better way later...
512 if ( priv
!= wxDefaultPosition
)
516 return m_Word
.Mid(part1
, part2
-part1
);
518 //else: return the whole word below
524 wxCursor
wxHtmlWordCell::GetCursor() const
528 if ( !gs_cursorText
)
529 gs_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
530 return *gs_cursorText
;
533 return wxHtmlCell::GetCursor();
537 //-----------------------------------------------------------------------------
538 // wxHtmlContainerCell
539 //-----------------------------------------------------------------------------
541 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
543 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
545 m_Cells
= m_LastCell
= NULL
;
548 if (m_Parent
) m_Parent
->InsertCell(this);
549 m_AlignHor
= wxHTML_ALIGN_LEFT
;
550 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
551 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
552 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
553 m_UseBkColour
= false;
556 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
560 wxHtmlContainerCell::~wxHtmlContainerCell()
562 wxHtmlCell
*cell
= m_Cells
;
565 wxHtmlCell
*cellNext
= cell
->GetNext();
573 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
575 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
576 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
577 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
578 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
579 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
585 int wxHtmlContainerCell::GetIndent(int ind
) const
587 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
588 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
589 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
590 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
591 else return -1; /* BUG! Should not be called... */
597 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
600 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
601 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
602 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
603 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
604 if (p
) return wxHTML_UNITS_PERCENT
;
605 else return wxHTML_UNITS_PIXELS
;
610 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
612 if (!m_CanLiveOnPagebreak
)
613 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
617 wxHtmlCell
*c
= GetFirstChild();
619 int pbrk
= *pagebreak
- m_PosY
;
623 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
628 *pagebreak
= pbrk
+ m_PosY
;
635 void wxHtmlContainerCell::Layout(int w
)
637 wxHtmlCell::Layout(w
);
639 if (m_LastLayout
== w
) return;
641 // VS: Any attempt to layout with negative or zero width leads to hell,
642 // but we can't ignore such attempts completely, since it sometimes
643 // happen (e.g. when trying how small a table can be). The best thing we
644 // can do is to set the width of child cells to zero
648 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
650 // this does two things: it recursively calls this code on all
651 // child contrainers and resets children's position to (0,0)
655 wxHtmlCell
*cell
= m_Cells
,
657 wxHtmlCell
*nextCell
;
658 long xpos
= 0, ypos
= m_IndentTop
;
659 int xdelta
= 0, ybasicpos
= 0, ydiff
;
660 int s_width
, nextWordWidth
, s_indent
;
661 int ysizeup
= 0, ysizedown
= 0;
662 int MaxLineWidth
= 0;
663 int curLineWidth
= 0;
673 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
675 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
676 else m_Width
= m_WidthFloat
* w
/ 100;
680 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
681 else m_Width
= m_WidthFloat
;
686 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
687 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
688 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
689 cell
->Layout(m_Width
- (l
+ r
));
698 // adjust indentation:
699 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
700 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
707 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
708 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
709 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
711 ydiff
= cell
->GetHeight() + ybasicpos
;
713 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
714 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
716 // layout nonbreakable run of cells:
717 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
718 xpos
+= cell
->GetWidth();
719 if (!cell
->IsTerminalCell())
721 // Container cell indicates new line
722 if (curLineWidth
> m_MaxTotalWidth
)
723 m_MaxTotalWidth
= curLineWidth
;
725 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
726 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
730 // Normal cell, add maximum cell width to line width
731 curLineWidth
+= cell
->GetMaxTotalWidth();
733 cell
= cell
->GetNext();
735 // compute length of the next word that would be added:
742 nextWordWidth
+= nextCell
->GetWidth();
743 nextCell
= nextCell
->GetNext();
744 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
747 // force new line if occurred:
748 if ((cell
== NULL
) ||
749 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
751 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
752 if (ysizeup
< 0) ysizeup
= 0;
753 if (ysizedown
< 0) ysizedown
= 0;
754 switch (m_AlignHor
) {
755 case wxHTML_ALIGN_LEFT
:
756 case wxHTML_ALIGN_JUSTIFY
:
759 case wxHTML_ALIGN_RIGHT
:
760 xdelta
= 0 + (s_width
- xpos
);
762 case wxHTML_ALIGN_CENTER
:
763 xdelta
= 0 + (s_width
- xpos
) / 2;
766 if (xdelta
< 0) xdelta
= 0;
771 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
775 line
->SetPos(line
->GetPosX() + xdelta
,
776 ypos
+ line
->GetPosY());
777 line
= line
->GetNext();
780 else // align == justify
782 // we have to distribute the extra horz space between the cells
785 // an added complication is that some cells have fixed size and
786 // shouldn't get any increment (it so happens that these cells
787 // also don't allow line break on them which provides with an
788 // easy way to test for this) -- and neither should the cells
789 // adjacent to them as this could result in a visible space
790 // between two cells separated by, e.g. font change, cell which
793 int step
= s_width
- xpos
;
796 // first count the cells which will get extra space
802 for ( c
= line
->GetNext(); c
!= cell
; c
= c
->GetNext() )
804 if ( c
->IsLinebreakAllowed() )
811 // and now extra space to those cells which merit it
814 // first cell on line is not moved:
815 line
->SetPos(line
->GetPosX() + s_indent
,
816 line
->GetPosY() + ypos
);
818 line
= line
->GetNext();
819 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
821 if ( line
->IsLinebreakAllowed() )
823 // offset the next cell relative to this one
824 // thus increasing our size
828 line
->SetPos(line
->GetPosX() + s_indent
+
829 ((n
* step
) / total
),
830 line
->GetPosY() + ypos
);
835 // this will cause the code to enter "else branch" below:
840 if ( step
<= 0 ) // no extra space to distribute
842 // just set the indent properly
845 line
->SetPos(line
->GetPosX() + s_indent
,
846 line
->GetPosY() + ypos
);
847 line
= line
->GetNext();
854 ysizeup
= ysizedown
= 0;
859 // setup height & width, depending on container layout:
860 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
862 if (m_Height
< m_MinHeight
)
864 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
866 int diff
= m_MinHeight
- m_Height
;
867 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
871 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
872 cell
= cell
->GetNext();
875 m_Height
= m_MinHeight
;
878 if (curLineWidth
> m_MaxTotalWidth
)
879 m_MaxTotalWidth
= curLineWidth
;
881 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
882 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
883 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
888 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
889 wxHtmlCell
*cell
) const
891 wxHtmlSelection
*s
= info
.GetSelection();
893 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
895 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
899 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
900 wxHtmlCell
*cell
) const
902 wxHtmlSelection
*s
= info
.GetSelection();
904 if (s
->GetToCell() == cell
)
905 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
906 else if (s
->GetFromCell() == cell
)
907 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
910 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
911 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
913 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
914 wxHtmlRenderingInfo
& info
)
916 #if 0 // useful for debugging
917 dc
.SetPen(*wxRED_PEN
);
918 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
921 int xlocal
= x
+ m_PosX
;
922 int ylocal
= y
+ m_PosY
;
926 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
928 int real_y1
= mMax(ylocal
, view_y1
);
929 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
932 dc
.SetPen(*wxTRANSPARENT_PEN
);
933 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
938 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
939 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
942 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
943 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
945 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
946 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
951 // draw container's contents:
952 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
955 // optimize drawing: don't render off-screen content:
956 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
957 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
959 // the cell is visible, draw it:
960 UpdateRenderingStatePre(info
, cell
);
962 xlocal
, ylocal
, view_y1
, view_y2
,
964 UpdateRenderingStatePost(info
, cell
);
968 // the cell is off-screen, proceed with font+color+etc.
970 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
978 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
979 wxHtmlRenderingInfo
& info
)
983 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
985 UpdateRenderingStatePre(info
, cell
);
986 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
987 UpdateRenderingStatePost(info
, cell
);
993 wxColour
wxHtmlContainerCell::GetBackgroundColour()
1003 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
1005 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1007 // VZ: I don't know if we should pass absolute or relative coords to
1008 // wxHtmlCell::GetLink()? As the base class version just ignores them
1009 // anyhow, it hardly matters right now but should still be clarified
1010 return cell
? cell
->GetLink(x
, y
) : NULL
;
1015 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1017 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1020 m_LastCell
->SetNext(f
);
1022 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1030 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1032 if (tag
.HasParam(wxT("ALIGN")))
1034 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1036 if (alg
== wxT("CENTER"))
1037 SetAlignHor(wxHTML_ALIGN_CENTER
);
1038 else if (alg
== wxT("LEFT"))
1039 SetAlignHor(wxHTML_ALIGN_LEFT
);
1040 else if (alg
== wxT("JUSTIFY"))
1041 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1042 else if (alg
== wxT("RIGHT"))
1043 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1050 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1052 if (tag
.HasParam(wxT("WIDTH")))
1055 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1057 if (wd
[wd
.Length()-1] == wxT('%'))
1059 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1060 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1064 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1065 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1073 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1077 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1079 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1087 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1088 unsigned flags
) const
1090 if ( flags
& wxHTML_FIND_EXACT
)
1092 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1094 int cx
= cell
->GetPosX(),
1095 cy
= cell
->GetPosY();
1097 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1098 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1100 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1104 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1107 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1109 if ( cell
->IsFormattingCell() )
1111 int cellY
= cell
->GetPosY();
1112 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1113 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1116 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1120 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1122 wxHtmlCell
*c2
, *c
= NULL
;
1123 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1125 if ( cell
->IsFormattingCell() )
1127 int cellY
= cell
->GetPosY();
1128 if (!( cellY
+ cell
->GetHeight() <= y
||
1129 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1131 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1142 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
1144 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1146 cell
->OnMouseClick(parent
, x
, y
, event
);
1151 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1156 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1158 c2
= c
->GetFirstTerminal();
1166 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1170 // most common case first:
1171 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1176 wxHtmlCell
*c2
= NULL
;
1177 for (c
= m_Cells
; c
; c
= c
->GetNext())
1179 ctmp
= c
->GetLastTerminal();
1190 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1192 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1194 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1200 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1203 SetIndent(0, wxHTML_INDENT_TOP
);
1205 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1210 wxHtmlContainerCell
*cont
;
1213 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1215 if ( c
->IsTerminalCell() )
1217 if ( !c
->IsFormattingCell() )
1222 cont
= (wxHtmlContainerCell
*)c
;
1223 if ( IsEmptyContainer(cont
) )
1225 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1229 cont
->RemoveExtraSpacing(true, false);
1239 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1242 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1244 c
= (wxHtmlCell
*)arr
[i
];
1245 if ( c
->IsTerminalCell() )
1247 if ( !c
->IsFormattingCell() )
1252 cont
= (wxHtmlContainerCell
*)c
;
1253 if ( IsEmptyContainer(cont
) )
1255 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1259 cont
->RemoveExtraSpacing(false, true);
1271 // --------------------------------------------------------------------------
1273 // --------------------------------------------------------------------------
1275 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1277 void wxHtmlColourCell::Draw(wxDC
& dc
,
1279 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1280 wxHtmlRenderingInfo
& info
)
1282 DrawInvisible(dc
, x
, y
, info
);
1285 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1286 int WXUNUSED(x
), int WXUNUSED(y
),
1287 wxHtmlRenderingInfo
& info
)
1289 wxHtmlRenderingState
& state
= info
.GetState();
1290 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1292 state
.SetFgColour(m_Colour
);
1293 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1294 dc
.SetTextForeground(m_Colour
);
1296 dc
.SetTextForeground(
1297 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1299 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1301 state
.SetBgColour(m_Colour
);
1302 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1304 dc
.SetTextBackground(m_Colour
);
1305 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1309 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1310 dc
.SetTextBackground(c
);
1311 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1319 // ---------------------------------------------------------------------------
1321 // ---------------------------------------------------------------------------
1323 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1325 void wxHtmlFontCell::Draw(wxDC
& dc
,
1326 int WXUNUSED(x
), int WXUNUSED(y
),
1327 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1328 wxHtmlRenderingInfo
& WXUNUSED(info
))
1333 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1334 wxHtmlRenderingInfo
& WXUNUSED(info
))
1346 // ---------------------------------------------------------------------------
1348 // ---------------------------------------------------------------------------
1350 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1352 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1356 m_Wnd
->GetSize(&sx
, &sy
);
1357 m_Width
= sx
, m_Height
= sy
;
1362 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1363 int WXUNUSED(x
), int WXUNUSED(y
),
1364 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1365 wxHtmlRenderingInfo
& WXUNUSED(info
))
1367 int absx
= 0, absy
= 0, stx
, sty
;
1368 wxHtmlCell
*c
= this;
1372 absx
+= c
->GetPosX();
1373 absy
+= c
->GetPosY();
1377 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1378 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1383 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1384 int WXUNUSED(x
), int WXUNUSED(y
),
1385 wxHtmlRenderingInfo
& WXUNUSED(info
))
1387 int absx
= 0, absy
= 0, stx
, sty
;
1388 wxHtmlCell
*c
= this;
1392 absx
+= c
->GetPosX();
1393 absy
+= c
->GetPosY();
1397 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1398 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1403 void wxHtmlWidgetCell::Layout(int w
)
1405 if (m_WidthFloat
!= 0)
1407 m_Width
= (w
* m_WidthFloat
) / 100;
1408 m_Wnd
->SetSize(m_Width
, m_Height
);
1411 wxHtmlCell::Layout(w
);
1416 // ----------------------------------------------------------------------------
1417 // wxHtmlTerminalCellsInterator
1418 // ----------------------------------------------------------------------------
1420 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1427 if ( m_pos
== m_to
)
1433 if ( m_pos
->GetNext() )
1434 m_pos
= m_pos
->GetNext();
1437 // we must go up the hierarchy until we reach container where this
1438 // is not the last child, and then go down to first terminal cell:
1439 while ( m_pos
->GetNext() == NULL
)
1441 m_pos
= m_pos
->GetParent();
1445 m_pos
= m_pos
->GetNext();
1447 while ( m_pos
->GetFirstChild() != NULL
)
1448 m_pos
= m_pos
->GetFirstChild();
1449 } while ( !m_pos
->IsTerminalCell() );
1460 //-----------------------------------------------------------------------------
1462 //-----------------------------------------------------------------------------
1464 class wxHtmlCellModule
: public wxModule
1466 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule
)
1468 wxHtmlCellModule() : wxModule() {}
1469 bool OnInit() { return true; }
1472 wxDELETE(gs_cursorLink
);
1473 wxDELETE(gs_cursorText
);
1477 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule
, wxModule
)