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_CanLiveOnPagebreak
= true;
97 wxHtmlCell::~wxHtmlCell()
103 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
104 const wxMouseEvent
& event
)
106 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
109 wxHtmlLinkInfo
lnk2(*lnk
);
110 lnk2
.SetEvent(&event
);
111 lnk2
.SetHtmlCell(this);
113 // note : this cast is legal because parent is *always* wxHtmlWindow
114 wxStaticCast(parent
, wxHtmlWindow
)->OnLinkClicked(lnk2
);
119 wxCursor
wxHtmlCell::GetCursor() const
123 if ( !gs_cursorLink
)
124 gs_cursorLink
= new wxCursor(wxCURSOR_HAND
);
125 return *gs_cursorLink
;
128 return *wxSTANDARD_CURSOR
;
132 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) const
134 if ((!m_CanLiveOnPagebreak
) &&
135 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
146 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
148 if (m_Link
) delete m_Link
;
150 if (link
.GetHref() != wxEmptyString
)
151 m_Link
= new wxHtmlLinkInfo(link
);
155 void wxHtmlCell::Layout(int WXUNUSED(w
))
162 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
168 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
169 unsigned flags
) const
171 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
173 return wxConstCast(this, wxHtmlCell
);
177 if ((flags
& wxHTML_FIND_NEAREST_AFTER
) &&
178 (y
< 0 || (y
< 0+m_Height
&& x
< 0+m_Width
)))
179 return wxConstCast(this, wxHtmlCell
);
180 else if ((flags
& wxHTML_FIND_NEAREST_BEFORE
) &&
181 (y
>= 0+m_Height
|| (y
>= 0 && x
>= 0)))
182 return wxConstCast(this, wxHtmlCell
);
189 wxPoint
wxHtmlCell::GetAbsPos() const
191 wxPoint
p(m_PosX
, m_PosY
);
192 for (wxHtmlCell
*parent
= m_Parent
; parent
; parent
= parent
->m_Parent
)
194 p
.x
+= parent
->m_PosX
;
195 p
.y
+= parent
->m_PosY
;
200 unsigned wxHtmlCell::GetDepth() const
203 for (wxHtmlCell
*p
= m_Parent
; p
; p
= p
->m_Parent
)
208 bool wxHtmlCell::IsBefore(wxHtmlCell
*cell
) const
210 const wxHtmlCell
*c1
= this;
211 const wxHtmlCell
*c2
= cell
;
212 unsigned d1
= GetDepth();
213 unsigned d2
= cell
->GetDepth();
216 for (; d1
!= d2
; d1
-- )
219 for (; d1
!= d2
; d2
-- )
227 if ( c1
->m_Parent
== c2
->m_Parent
)
244 wxFAIL_MSG(_T("Cells are in different trees"));
249 //-----------------------------------------------------------------------------
251 //-----------------------------------------------------------------------------
253 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWordCell
, wxHtmlCell
)
255 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
258 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
259 SetCanLiveOnPagebreak(false);
260 m_allowLinebreak
= true;
263 void wxHtmlWordCell::SetPreviousWord(wxHtmlWordCell
*cell
)
265 if ( cell
&& m_Parent
== cell
->m_Parent
&&
266 !wxIsspace(cell
->m_Word
.Last()) && !wxIsspace(m_Word
[0u]) )
268 m_allowLinebreak
= false;
272 // Splits m_Word into up to three parts according to selection, returns
273 // substring before, in and after selection and the points (in relative coords)
274 // where s2 and s3 start:
275 void wxHtmlWordCell::Split(wxDC
& dc
,
276 const wxPoint
& selFrom
, const wxPoint
& selTo
,
277 unsigned& pos1
, unsigned& pos2
) const
279 wxPoint pt1
= (selFrom
== wxDefaultPosition
) ?
280 wxDefaultPosition
: selFrom
- GetAbsPos();
281 wxPoint pt2
= (selTo
== wxDefaultPosition
) ?
282 wxPoint(m_Width
, wxDefaultCoord
) : selTo
- GetAbsPos();
284 unsigned len
= m_Word
.length();
288 // adjust for cases when the start/end position is completely
292 if ( pt2
.y
>= m_Height
)
297 // implementation using PartialExtents to support fractional widths
299 dc
.GetPartialTextExtents(m_Word
,widths
) ;
300 while( i
< len
&& pt1
.x
>= widths
[i
] )
303 wxCoord charW
, charH
;
304 while ( pt1
.x
> 0 && i
< len
)
306 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
314 #endif // __WXMAC__/!__WXMAC__
319 while( j
< len
&& pt2
.x
>= widths
[j
] )
324 while ( pt2
.x
> 0 && j
< len
)
326 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
334 #endif // __WXMAC__/!__WXMAC__
340 void wxHtmlWordCell::SetSelectionPrivPos(wxDC
& dc
, wxHtmlSelection
*s
) const
345 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
346 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
349 wxPoint
p(0, m_Word
.length());
351 if ( this == s
->GetFromCell() )
352 p
.x
= p1
; // selection starts here
353 if ( this == s
->GetToCell() )
354 p
.y
= p2
; // selection ends here
356 if ( this == s
->GetFromCell() )
357 s
->SetFromPrivPos(p
);
358 if ( this == s
->GetToCell() )
363 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
366 wxColour fg
= info
.GetState().GetFgColour();
367 wxColour bg
= info
.GetState().GetBgColour();
371 dc
.SetBackgroundMode(wxSOLID
);
372 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
373 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
374 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
379 dc
.SetBackgroundMode(wxTRANSPARENT
);
380 dc
.SetTextForeground(fg
);
381 dc
.SetTextBackground(bg
);
382 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
387 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
388 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
389 wxHtmlRenderingInfo
& info
)
391 #if 0 // useful for debugging
392 dc
.SetPen(*wxBLACK_PEN
);
393 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
396 bool drawSelectionAfterCell
= false;
398 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
400 // Selection changing, we must draw the word piecewise:
401 wxHtmlSelection
*s
= info
.GetSelection();
406 wxPoint priv
= (this == s
->GetFromCell()) ?
407 s
->GetFromPrivPos() : s
->GetToPrivPos();
409 // NB: this is quite a hack: in order to compute selection boundaries
410 // (in word's characters) we must know current font, which is only
411 // possible inside rendering code. Therefore we update the
412 // information here and store it in wxHtmlSelection so that
413 // ConvertToText can use it later:
414 if ( priv
== wxDefaultPosition
)
416 SetSelectionPrivPos(dc
, s
);
417 priv
= (this == s
->GetFromCell()) ?
418 s
->GetFromPrivPos() : s
->GetToPrivPos();
426 txt
= m_Word
.Mid(0, part1
);
427 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
428 dc
.GetTextExtent(txt
, &w
, &h
);
432 SwitchSelState(dc
, info
, true);
434 txt
= m_Word
.Mid(part1
, part2
-part1
);
435 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
437 if ( (size_t)part2
< m_Word
.length() )
439 dc
.GetTextExtent(txt
, &w
, &h
);
441 SwitchSelState(dc
, info
, false);
442 txt
= m_Word
.Mid(part2
);
443 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
446 drawSelectionAfterCell
= true;
450 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
451 // Not changing selection state, draw the word in single mode:
452 if ( selstate
!= wxHTML_SEL_OUT
&&
453 dc
.GetBackgroundMode() != wxSOLID
)
455 SwitchSelState(dc
, info
, true);
457 else if ( selstate
== wxHTML_SEL_OUT
&&
458 dc
.GetBackgroundMode() == wxSOLID
)
460 SwitchSelState(dc
, info
, false);
462 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
463 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
466 // NB: If the text is justified then there is usually some free space
467 // between adjacent cells and drawing the selection only onto cells
468 // would result in ugly unselected spaces. The code below detects
469 // this special case and renders the selection *outside* the sell,
471 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
472 drawSelectionAfterCell
)
474 wxHtmlCell
*nextCell
= m_Next
;
475 while ( nextCell
&& nextCell
->IsFormattingCell() )
476 nextCell
= nextCell
->GetNext();
479 int nextX
= nextCell
->GetPosX();
480 if ( m_PosX
+ m_Width
< nextX
)
482 dc
.SetBrush(dc
.GetBackground());
483 dc
.SetPen(*wxTRANSPARENT_PEN
);
484 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
485 nextX
- m_PosX
- m_Width
, m_Height
);
492 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
494 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
496 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
499 // VZ: we may be called before we had a chance to re-render ourselves
500 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
501 // that this only happens in case of a double/triple click (which
502 // seems to be the case now) and so it makes sense to select the
503 // entire contents of the cell in this case
505 // TODO: but this really needs to be fixed in some better way later...
506 if ( priv
!= wxDefaultPosition
)
510 return m_Word
.Mid(part1
, part2
-part1
);
512 //else: return the whole word below
518 wxCursor
wxHtmlWordCell::GetCursor() const
522 if ( !gs_cursorText
)
523 gs_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
524 return *gs_cursorText
;
527 return wxHtmlCell::GetCursor();
531 //-----------------------------------------------------------------------------
532 // wxHtmlContainerCell
533 //-----------------------------------------------------------------------------
535 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
537 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
539 m_Cells
= m_LastCell
= NULL
;
542 if (m_Parent
) m_Parent
->InsertCell(this);
543 m_AlignHor
= wxHTML_ALIGN_LEFT
;
544 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
545 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
546 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
547 m_UseBkColour
= false;
550 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
554 wxHtmlContainerCell::~wxHtmlContainerCell()
556 wxHtmlCell
*cell
= m_Cells
;
559 wxHtmlCell
*cellNext
= cell
->GetNext();
567 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
569 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
570 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
571 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
572 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
573 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
579 int wxHtmlContainerCell::GetIndent(int ind
) const
581 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
582 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
583 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
584 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
585 else return -1; /* BUG! Should not be called... */
591 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
594 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
595 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
596 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
597 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
598 if (p
) return wxHTML_UNITS_PERCENT
;
599 else return wxHTML_UNITS_PIXELS
;
604 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
606 if (!m_CanLiveOnPagebreak
)
607 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
611 wxHtmlCell
*c
= GetFirstChild();
613 int pbrk
= *pagebreak
- m_PosY
;
617 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
622 *pagebreak
= pbrk
+ m_PosY
;
629 void wxHtmlContainerCell::Layout(int w
)
631 wxHtmlCell::Layout(w
);
633 if (m_LastLayout
== w
) return;
635 // VS: Any attempt to layout with negative or zero width leads to hell,
636 // but we can't ignore such attempts completely, since it sometimes
637 // happen (e.g. when trying how small a table can be). The best thing we
638 // can do is to set the width of child cells to zero
642 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
644 // this does two things: it recursively calls this code on all
645 // child contrainers and resets children's position to (0,0)
649 wxHtmlCell
*cell
= m_Cells
,
651 wxHtmlCell
*nextCell
;
652 long xpos
= 0, ypos
= m_IndentTop
;
653 int xdelta
= 0, ybasicpos
= 0, ydiff
;
654 int s_width
, nextWordWidth
, s_indent
;
655 int ysizeup
= 0, ysizedown
= 0;
656 int MaxLineWidth
= 0;
657 int curLineWidth
= 0;
667 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
669 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
670 else m_Width
= m_WidthFloat
* w
/ 100;
674 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
675 else m_Width
= m_WidthFloat
;
680 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
681 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
682 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
683 cell
->Layout(m_Width
- (l
+ r
));
692 // adjust indentation:
693 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
694 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
701 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
702 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
703 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
705 ydiff
= cell
->GetHeight() + ybasicpos
;
707 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
708 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
710 // layout nonbreakable run of cells:
711 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
712 xpos
+= cell
->GetWidth();
713 if (!cell
->IsTerminalCell())
715 // Container cell indicates new line
716 if (curLineWidth
> m_MaxTotalWidth
)
717 m_MaxTotalWidth
= curLineWidth
;
719 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
720 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
724 // Normal cell, add maximum cell width to line width
725 curLineWidth
+= cell
->GetMaxTotalWidth();
727 cell
= cell
->GetNext();
729 // compute length of the next word that would be added:
736 nextWordWidth
+= nextCell
->GetWidth();
737 nextCell
= nextCell
->GetNext();
738 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
741 // force new line if occurred:
742 if ((cell
== NULL
) ||
743 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
745 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
746 if (ysizeup
< 0) ysizeup
= 0;
747 if (ysizedown
< 0) ysizedown
= 0;
748 switch (m_AlignHor
) {
749 case wxHTML_ALIGN_LEFT
:
750 case wxHTML_ALIGN_JUSTIFY
:
753 case wxHTML_ALIGN_RIGHT
:
754 xdelta
= 0 + (s_width
- xpos
);
756 case wxHTML_ALIGN_CENTER
:
757 xdelta
= 0 + (s_width
- xpos
) / 2;
760 if (xdelta
< 0) xdelta
= 0;
765 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
769 line
->SetPos(line
->GetPosX() + xdelta
,
770 ypos
+ line
->GetPosY());
771 line
= line
->GetNext();
774 else // align == justify
776 // we have to distribute the extra horz space between the cells
779 // an added complication is that some cells have fixed size and
780 // shouldn't get any increment (it so happens that these cells
781 // also don't allow line break on them which provides with an
782 // easy way to test for this) -- and neither should the cells
783 // adjacent to them as this could result in a visible space
784 // between two cells separated by, e.g. font change, cell which
787 int step
= s_width
- xpos
;
790 // first count the cells which will get extra space
796 for ( c
= line
->GetNext(); c
!= cell
; c
= c
->GetNext() )
798 if ( c
->IsLinebreakAllowed() )
805 // and now extra space to those cells which merit it
808 // first cell on line is not moved:
809 line
->SetPos(line
->GetPosX() + s_indent
,
810 line
->GetPosY() + ypos
);
812 line
= line
->GetNext();
813 for ( int n
= 0; line
!= cell
; line
= line
->GetNext() )
815 if ( line
->IsLinebreakAllowed() )
817 // offset the next cell relative to this one
818 // thus increasing our size
822 line
->SetPos(line
->GetPosX() + s_indent
+
823 ((n
* step
) / total
),
824 line
->GetPosY() + ypos
);
829 // this will cause the code to enter "else branch" below:
834 if ( step
<= 0 ) // no extra space to distribute
836 // just set the indent properly
839 line
->SetPos(line
->GetPosX() + s_indent
,
840 line
->GetPosY() + ypos
);
841 line
= line
->GetNext();
848 ysizeup
= ysizedown
= 0;
853 // setup height & width, depending on container layout:
854 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
856 if (m_Height
< m_MinHeight
)
858 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
860 int diff
= m_MinHeight
- m_Height
;
861 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
865 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
866 cell
= cell
->GetNext();
869 m_Height
= m_MinHeight
;
872 if (curLineWidth
> m_MaxTotalWidth
)
873 m_MaxTotalWidth
= curLineWidth
;
875 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
876 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
877 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
882 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
883 wxHtmlCell
*cell
) const
885 wxHtmlSelection
*s
= info
.GetSelection();
887 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
889 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
893 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
894 wxHtmlCell
*cell
) const
896 wxHtmlSelection
*s
= info
.GetSelection();
898 if (s
->GetToCell() == cell
)
899 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
900 else if (s
->GetFromCell() == cell
)
901 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
904 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
905 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
907 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
908 wxHtmlRenderingInfo
& info
)
910 #if 0 // useful for debugging
911 dc
.SetPen(*wxRED_PEN
);
912 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
915 int xlocal
= x
+ m_PosX
;
916 int ylocal
= y
+ m_PosY
;
920 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
922 int real_y1
= mMax(ylocal
, view_y1
);
923 int real_y2
= mMin(ylocal
+ m_Height
- 1, view_y2
);
926 dc
.SetPen(*wxTRANSPARENT_PEN
);
927 dc
.DrawRectangle(xlocal
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
932 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
933 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
936 dc
.DrawLine(xlocal
, ylocal
, xlocal
, ylocal
+ m_Height
- 1);
937 dc
.DrawLine(xlocal
, ylocal
, xlocal
+ m_Width
, ylocal
);
939 dc
.DrawLine(xlocal
+ m_Width
- 1, ylocal
, xlocal
+ m_Width
- 1, ylocal
+ m_Height
- 1);
940 dc
.DrawLine(xlocal
, ylocal
+ m_Height
- 1, xlocal
+ m_Width
, ylocal
+ m_Height
- 1);
945 // draw container's contents:
946 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
949 // optimize drawing: don't render off-screen content:
950 if ((ylocal
+ cell
->GetPosY() <= view_y2
) &&
951 (ylocal
+ cell
->GetPosY() + cell
->GetHeight() > view_y1
))
953 // the cell is visible, draw it:
954 UpdateRenderingStatePre(info
, cell
);
956 xlocal
, ylocal
, view_y1
, view_y2
,
958 UpdateRenderingStatePost(info
, cell
);
962 // the cell is off-screen, proceed with font+color+etc.
964 cell
->DrawInvisible(dc
, xlocal
, ylocal
, info
);
972 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
973 wxHtmlRenderingInfo
& info
)
977 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
979 UpdateRenderingStatePre(info
, cell
);
980 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
981 UpdateRenderingStatePost(info
, cell
);
987 wxColour
wxHtmlContainerCell::GetBackgroundColour()
997 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
999 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1001 // VZ: I don't know if we should pass absolute or relative coords to
1002 // wxHtmlCell::GetLink()? As the base class version just ignores them
1003 // anyhow, it hardly matters right now but should still be clarified
1004 return cell
? cell
->GetLink(x
, y
) : NULL
;
1009 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
1011 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1014 m_LastCell
->SetNext(f
);
1016 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1024 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1026 if (tag
.HasParam(wxT("ALIGN")))
1028 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1030 if (alg
== wxT("CENTER"))
1031 SetAlignHor(wxHTML_ALIGN_CENTER
);
1032 else if (alg
== wxT("LEFT"))
1033 SetAlignHor(wxHTML_ALIGN_LEFT
);
1034 else if (alg
== wxT("JUSTIFY"))
1035 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1036 else if (alg
== wxT("RIGHT"))
1037 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1044 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1046 if (tag
.HasParam(wxT("WIDTH")))
1049 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1051 if (wd
[wd
.Length()-1] == wxT('%'))
1053 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1054 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1058 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1059 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1067 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1071 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1073 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1081 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1082 unsigned flags
) const
1084 if ( flags
& wxHTML_FIND_EXACT
)
1086 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1088 int cx
= cell
->GetPosX(),
1089 cy
= cell
->GetPosY();
1091 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1092 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1094 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1098 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1101 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1103 if ( cell
->IsFormattingCell() )
1105 int cellY
= cell
->GetPosY();
1106 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1107 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1110 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1114 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1116 wxHtmlCell
*c2
, *c
= NULL
;
1117 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1119 if ( cell
->IsFormattingCell() )
1121 int cellY
= cell
->GetPosY();
1122 if (!( cellY
+ cell
->GetHeight() <= y
||
1123 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1125 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1136 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
1138 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1140 cell
->OnMouseClick(parent
, x
, y
, event
);
1145 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1150 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1152 c2
= c
->GetFirstTerminal();
1160 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1164 // most common case first:
1165 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1170 wxHtmlCell
*c2
= NULL
;
1171 for (c
= m_Cells
; c
; c
= c
->GetNext())
1173 ctmp
= c
->GetLastTerminal();
1184 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1186 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1188 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1194 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1197 SetIndent(0, wxHTML_INDENT_TOP
);
1199 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1204 wxHtmlContainerCell
*cont
;
1207 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1209 if ( c
->IsTerminalCell() )
1211 if ( !c
->IsFormattingCell() )
1216 cont
= (wxHtmlContainerCell
*)c
;
1217 if ( IsEmptyContainer(cont
) )
1219 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1223 cont
->RemoveExtraSpacing(true, false);
1233 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1236 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1238 c
= (wxHtmlCell
*)arr
[i
];
1239 if ( c
->IsTerminalCell() )
1241 if ( !c
->IsFormattingCell() )
1246 cont
= (wxHtmlContainerCell
*)c
;
1247 if ( IsEmptyContainer(cont
) )
1249 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1253 cont
->RemoveExtraSpacing(false, true);
1265 // --------------------------------------------------------------------------
1267 // --------------------------------------------------------------------------
1269 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1271 void wxHtmlColourCell::Draw(wxDC
& dc
,
1273 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1274 wxHtmlRenderingInfo
& info
)
1276 DrawInvisible(dc
, x
, y
, info
);
1279 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1280 int WXUNUSED(x
), int WXUNUSED(y
),
1281 wxHtmlRenderingInfo
& info
)
1283 wxHtmlRenderingState
& state
= info
.GetState();
1284 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1286 state
.SetFgColour(m_Colour
);
1287 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1288 dc
.SetTextForeground(m_Colour
);
1290 dc
.SetTextForeground(
1291 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1293 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1295 state
.SetBgColour(m_Colour
);
1296 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1298 dc
.SetTextBackground(m_Colour
);
1299 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1303 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1304 dc
.SetTextBackground(c
);
1305 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1313 // ---------------------------------------------------------------------------
1315 // ---------------------------------------------------------------------------
1317 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1319 void wxHtmlFontCell::Draw(wxDC
& dc
,
1320 int WXUNUSED(x
), int WXUNUSED(y
),
1321 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1322 wxHtmlRenderingInfo
& WXUNUSED(info
))
1327 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1328 wxHtmlRenderingInfo
& WXUNUSED(info
))
1340 // ---------------------------------------------------------------------------
1342 // ---------------------------------------------------------------------------
1344 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1346 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1350 m_Wnd
->GetSize(&sx
, &sy
);
1351 m_Width
= sx
, m_Height
= sy
;
1356 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1357 int WXUNUSED(x
), int WXUNUSED(y
),
1358 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1359 wxHtmlRenderingInfo
& WXUNUSED(info
))
1361 int absx
= 0, absy
= 0, stx
, sty
;
1362 wxHtmlCell
*c
= this;
1366 absx
+= c
->GetPosX();
1367 absy
+= c
->GetPosY();
1371 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1372 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1377 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1378 int WXUNUSED(x
), int WXUNUSED(y
),
1379 wxHtmlRenderingInfo
& WXUNUSED(info
))
1381 int absx
= 0, absy
= 0, stx
, sty
;
1382 wxHtmlCell
*c
= this;
1386 absx
+= c
->GetPosX();
1387 absy
+= c
->GetPosY();
1391 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1392 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1397 void wxHtmlWidgetCell::Layout(int w
)
1399 if (m_WidthFloat
!= 0)
1401 m_Width
= (w
* m_WidthFloat
) / 100;
1402 m_Wnd
->SetSize(m_Width
, m_Height
);
1405 wxHtmlCell::Layout(w
);
1410 // ----------------------------------------------------------------------------
1411 // wxHtmlTerminalCellsInterator
1412 // ----------------------------------------------------------------------------
1414 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1421 if ( m_pos
== m_to
)
1427 if ( m_pos
->GetNext() )
1428 m_pos
= m_pos
->GetNext();
1431 // we must go up the hierarchy until we reach container where this
1432 // is not the last child, and then go down to first terminal cell:
1433 while ( m_pos
->GetNext() == NULL
)
1435 m_pos
= m_pos
->GetParent();
1439 m_pos
= m_pos
->GetNext();
1441 while ( m_pos
->GetFirstChild() != NULL
)
1442 m_pos
= m_pos
->GetFirstChild();
1443 } while ( !m_pos
->IsTerminalCell() );
1454 //-----------------------------------------------------------------------------
1456 //-----------------------------------------------------------------------------
1458 class wxHtmlCellModule
: public wxModule
1460 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule
)
1462 wxHtmlCellModule() : wxModule() {}
1463 bool OnInit() { return true; }
1466 wxDELETE(gs_cursorLink
);
1467 wxDELETE(gs_cursorText
);
1471 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule
, wxModule
)