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
, wxDefaultPosition
.y
) : 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
)
301 while ( pt1
.x
> 0 && i
< len
)
303 dc
.GetTextExtent(m_Word
[i
], &charW
, &charH
);
316 while ( pt2
.x
> 0 && j
< len
)
318 dc
.GetTextExtent(m_Word
[j
], &charW
, &charH
);
331 void wxHtmlWordCell::SetSelectionPrivPos(wxDC
& dc
, wxHtmlSelection
*s
) const
336 this == s
->GetFromCell() ? s
->GetFromPos() : wxDefaultPosition
,
337 this == s
->GetToCell() ? s
->GetToPos() : wxDefaultPosition
,
340 wxPoint
p(0, m_Word
.length());
342 if ( this == s
->GetFromCell() )
343 p
.x
= p1
; // selection starts here
344 if ( this == s
->GetToCell() )
345 p
.y
= p2
; // selection ends here
347 if ( this == s
->GetFromCell() )
348 s
->SetFromPrivPos(p
);
349 if ( this == s
->GetToCell() )
354 static void SwitchSelState(wxDC
& dc
, wxHtmlRenderingInfo
& info
,
357 wxColour fg
= info
.GetState().GetFgColour();
358 wxColour bg
= info
.GetState().GetBgColour();
362 dc
.SetBackgroundMode(wxSOLID
);
363 dc
.SetTextForeground(info
.GetStyle().GetSelectedTextColour(fg
));
364 dc
.SetTextBackground(info
.GetStyle().GetSelectedTextBgColour(bg
));
365 dc
.SetBackground(wxBrush(info
.GetStyle().GetSelectedTextBgColour(bg
),
370 dc
.SetBackgroundMode(wxTRANSPARENT
);
371 dc
.SetTextForeground(fg
);
372 dc
.SetTextBackground(bg
);
373 dc
.SetBackground(wxBrush(bg
, wxSOLID
));
378 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
379 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
380 wxHtmlRenderingInfo
& info
)
382 #if 0 // useful for debugging
383 dc
.SetPen(*wxBLACK_PEN
);
384 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
/* VZ: +1? */ ,m_Height
);
387 bool drawSelectionAfterCell
= false;
389 if ( info
.GetState().GetSelectionState() == wxHTML_SEL_CHANGING
)
391 // Selection changing, we must draw the word piecewise:
392 wxHtmlSelection
*s
= info
.GetSelection();
397 wxPoint priv
= (this == s
->GetFromCell()) ?
398 s
->GetFromPrivPos() : s
->GetToPrivPos();
400 // NB: this is quite a hack: in order to compute selection boundaries
401 // (in word's characters) we must know current font, which is only
402 // possible inside rendering code. Therefore we update the
403 // information here and store it in wxHtmlSelection so that
404 // ConvertToText can use it later:
405 if ( priv
== wxDefaultPosition
)
407 SetSelectionPrivPos(dc
, s
);
408 priv
= (this == s
->GetFromCell()) ?
409 s
->GetFromPrivPos() : s
->GetToPrivPos();
417 txt
= m_Word
.Mid(0, part1
);
418 dc
.DrawText(txt
, x
+ m_PosX
, y
+ m_PosY
);
419 dc
.GetTextExtent(txt
, &w
, &h
);
423 SwitchSelState(dc
, info
, true);
425 txt
= m_Word
.Mid(part1
, part2
-part1
);
426 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
428 if ( (size_t)part2
< m_Word
.length() )
430 dc
.GetTextExtent(txt
, &w
, &h
);
432 SwitchSelState(dc
, info
, false);
433 txt
= m_Word
.Mid(part2
);
434 dc
.DrawText(txt
, ofs
+ x
+ m_PosX
, y
+ m_PosY
);
437 drawSelectionAfterCell
= true;
441 wxHtmlSelectionState selstate
= info
.GetState().GetSelectionState();
442 // Not changing selection state, draw the word in single mode:
443 if ( selstate
!= wxHTML_SEL_OUT
&&
444 dc
.GetBackgroundMode() != wxSOLID
)
446 SwitchSelState(dc
, info
, true);
448 else if ( selstate
== wxHTML_SEL_OUT
&&
449 dc
.GetBackgroundMode() == wxSOLID
)
451 SwitchSelState(dc
, info
, false);
453 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
454 drawSelectionAfterCell
= (selstate
!= wxHTML_SEL_OUT
);
457 // NB: If the text is justified then there is usually some free space
458 // between adjacent cells and drawing the selection only onto cells
459 // would result in ugly unselected spaces. The code below detects
460 // this special case and renders the selection *outside* the sell,
462 if ( m_Parent
->GetAlignHor() == wxHTML_ALIGN_JUSTIFY
&&
463 drawSelectionAfterCell
)
465 wxHtmlCell
*nextCell
= m_Next
;
466 while ( nextCell
&& nextCell
->IsFormattingCell() )
467 nextCell
= nextCell
->GetNext();
470 int nextX
= nextCell
->GetPosX();
471 if ( m_PosX
+ m_Width
< nextX
)
473 dc
.SetBrush(dc
.GetBackground());
474 dc
.SetPen(*wxTRANSPARENT_PEN
);
475 dc
.DrawRectangle(x
+ m_PosX
+ m_Width
, y
+ m_PosY
,
476 nextX
- m_PosX
- m_Width
, m_Height
);
483 wxString
wxHtmlWordCell::ConvertToText(wxHtmlSelection
*s
) const
485 if ( s
&& (this == s
->GetFromCell() || this == s
->GetToCell()) )
487 wxPoint priv
= this == s
->GetFromCell() ? s
->GetFromPrivPos()
490 // VZ: we may be called before we had a chance to re-render ourselves
491 // and in this case GetFrom/ToPrivPos() is not set yet -- assume
492 // that this only happens in case of a double/triple click (which
493 // seems to be the case now) and so it makes sense to select the
494 // entire contents of the cell in this case
496 // TODO: but this really needs to be fixed in some better way later...
497 if ( priv
!= wxDefaultPosition
)
501 return m_Word
.Mid(part1
, part2
-part1
);
503 //else: return the whole word below
509 wxCursor
wxHtmlWordCell::GetCursor() const
513 if ( !gs_cursorText
)
514 gs_cursorText
= new wxCursor(wxCURSOR_IBEAM
);
515 return *gs_cursorText
;
518 return wxHtmlCell::GetCursor();
522 //-----------------------------------------------------------------------------
523 // wxHtmlContainerCell
524 //-----------------------------------------------------------------------------
526 IMPLEMENT_ABSTRACT_CLASS(wxHtmlContainerCell
, wxHtmlCell
)
528 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
530 m_Cells
= m_LastCell
= NULL
;
533 if (m_Parent
) m_Parent
->InsertCell(this);
534 m_AlignHor
= wxHTML_ALIGN_LEFT
;
535 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
536 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
537 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
538 m_UseBkColour
= false;
541 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
545 wxHtmlContainerCell::~wxHtmlContainerCell()
547 wxHtmlCell
*cell
= m_Cells
;
550 wxHtmlCell
*cellNext
= cell
->GetNext();
558 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
560 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
561 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
562 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
563 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
564 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
570 int wxHtmlContainerCell::GetIndent(int ind
) const
572 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
573 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
574 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
575 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
576 else return -1; /* BUG! Should not be called... */
582 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
585 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
586 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
587 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
588 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
589 if (p
) return wxHTML_UNITS_PERCENT
;
590 else return wxHTML_UNITS_PIXELS
;
595 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
597 if (!m_CanLiveOnPagebreak
)
598 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
602 wxHtmlCell
*c
= GetFirstChild();
604 int pbrk
= *pagebreak
- m_PosY
;
608 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
613 *pagebreak
= pbrk
+ m_PosY
;
620 void wxHtmlContainerCell::Layout(int w
)
622 wxHtmlCell::Layout(w
);
624 if (m_LastLayout
== w
) return;
626 // VS: Any attempt to layout with negative or zero width leads to hell,
627 // but we can't ignore such attempts completely, since it sometimes
628 // happen (e.g. when trying how small a table can be). The best thing we
629 // can do is to set the width of child cells to zero
633 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
635 // this does two things: it recursively calls this code on all
636 // child contrainers and resets children's position to (0,0)
640 wxHtmlCell
*cell
= m_Cells
,
642 wxHtmlCell
*nextCell
;
643 long xpos
= 0, ypos
= m_IndentTop
;
644 int xdelta
= 0, ybasicpos
= 0, ydiff
;
645 int s_width
, nextWordWidth
, s_indent
;
646 int ysizeup
= 0, ysizedown
= 0;
647 int MaxLineWidth
= 0;
648 int curLineWidth
= 0;
658 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
660 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
661 else m_Width
= m_WidthFloat
* w
/ 100;
665 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
666 else m_Width
= m_WidthFloat
;
671 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
672 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
673 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
674 cell
->Layout(m_Width
- (l
+ r
));
683 // adjust indentation:
684 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
685 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
692 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
693 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
694 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
696 ydiff
= cell
->GetHeight() + ybasicpos
;
698 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
699 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
701 // layout nonbreakable run of cells:
702 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
703 xpos
+= cell
->GetWidth();
704 if (!cell
->IsTerminalCell())
706 // Container cell indicates new line
707 if (curLineWidth
> m_MaxTotalWidth
)
708 m_MaxTotalWidth
= curLineWidth
;
710 if (wxMax(cell
->GetWidth(), cell
->GetMaxTotalWidth()) > m_MaxTotalWidth
)
711 m_MaxTotalWidth
= cell
->GetMaxTotalWidth();
715 // Normal cell, add maximum cell width to line width
716 curLineWidth
+= cell
->GetMaxTotalWidth();
718 cell
= cell
->GetNext();
720 // compute length of the next word that would be added:
727 nextWordWidth
+= nextCell
->GetWidth();
728 nextCell
= nextCell
->GetNext();
729 } while (nextCell
&& !nextCell
->IsLinebreakAllowed());
732 // force new line if occured:
733 if ((cell
== NULL
) ||
734 (xpos
+ nextWordWidth
> s_width
&& cell
->IsLinebreakAllowed()))
736 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
737 if (ysizeup
< 0) ysizeup
= 0;
738 if (ysizedown
< 0) ysizedown
= 0;
739 switch (m_AlignHor
) {
740 case wxHTML_ALIGN_LEFT
:
741 case wxHTML_ALIGN_JUSTIFY
:
744 case wxHTML_ALIGN_RIGHT
:
745 xdelta
= 0 + (s_width
- xpos
);
747 case wxHTML_ALIGN_CENTER
:
748 xdelta
= 0 + (s_width
- xpos
) / 2;
751 if (xdelta
< 0) xdelta
= 0;
756 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
760 line
->SetPos(line
->GetPosX() + xdelta
,
761 ypos
+ line
->GetPosY());
762 line
= line
->GetNext();
765 else // align == justify
767 // we have to distribute the extra horz space between the cells
770 // an added complication is that some cells have fixed size and
771 // shouldn't get any increment (it so happens that these cells
772 // also don't allow line break on them which provides with an
773 // easy way to test for this) -- and neither should the cells
774 // adjacent to them as this could result in a visible space
775 // between two cells separated by, e.g. font change, cell which
778 int step
= s_width
- xpos
;
781 // first count the cells which will get extra space
787 for ( c
= line
; c
!= cell
; prev
= c
, c
= next
)
790 if ( c
->IsLinebreakAllowed() &&
791 (next
== cell
|| next
->IsLinebreakAllowed()) &&
792 (!prev
|| prev
->IsLinebreakAllowed()) )
798 // and now extra space to those cells which merit it
803 for ( int n
= 0; line
!= cell
; prev
= line
, line
= line
->GetNext() )
805 line
->SetPos(line
->GetPosX() + s_indent
+
806 ((n
* step
) / total
),
807 line
->GetPosY() + ypos
);
809 next
= line
->GetNext();
810 if ( line
->IsLinebreakAllowed() &&
812 next
->IsLinebreakAllowed()) &&
813 (!prev
|| prev
->IsLinebreakAllowed()) )
815 // offset the next cell relative to this one
816 // thus increasing our size
823 // this will cause the code to enter "else branch" below:
828 if ( step
<= 0 ) // no extra space to distribute
830 // just set the indent properly
833 line
->SetPos(line
->GetPosX() + s_indent
,
834 line
->GetPosY() + ypos
);
835 line
= line
->GetNext();
842 ysizeup
= ysizedown
= 0;
847 // setup height & width, depending on container layout:
848 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
850 if (m_Height
< m_MinHeight
)
852 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
854 int diff
= m_MinHeight
- m_Height
;
855 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
859 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
860 cell
= cell
->GetNext();
863 m_Height
= m_MinHeight
;
866 if (curLineWidth
> m_MaxTotalWidth
)
867 m_MaxTotalWidth
= curLineWidth
;
869 m_MaxTotalWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
870 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
871 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
876 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingInfo
& info
,
877 wxHtmlCell
*cell
) const
879 wxHtmlSelection
*s
= info
.GetSelection();
881 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
883 info
.GetState().SetSelectionState(wxHTML_SEL_CHANGING
);
887 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingInfo
& info
,
888 wxHtmlCell
*cell
) const
890 wxHtmlSelection
*s
= info
.GetSelection();
892 if (s
->GetToCell() == cell
)
893 info
.GetState().SetSelectionState(wxHTML_SEL_OUT
);
894 else if (s
->GetFromCell() == cell
)
895 info
.GetState().SetSelectionState(wxHTML_SEL_IN
);
898 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
899 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
901 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
902 wxHtmlRenderingInfo
& info
)
904 #if 0 // useful for debugging
905 dc
.SetPen(*wxRED_PEN
);
906 dc
.DrawRectangle(x
+m_PosX
,y
+m_PosY
,m_Width
,m_Height
);
908 // container visible, draw it:
909 if ((y
+ m_PosY
<= view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
913 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
915 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
916 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
919 dc
.SetPen(*wxTRANSPARENT_PEN
);
920 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
925 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
926 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
929 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
930 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
, y
+ m_PosY
);
932 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
933 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
, y
+ m_PosY
+ m_Height
- 1);
938 // draw container's contents:
939 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
941 UpdateRenderingStatePre(info
, cell
);
943 x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
,
945 UpdateRenderingStatePost(info
, cell
);
949 // container invisible, just proceed font+color changing:
952 DrawInvisible(dc
, x
, y
, info
);
958 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
959 wxHtmlRenderingInfo
& info
)
963 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
965 UpdateRenderingStatePre(info
, cell
);
966 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, info
);
967 UpdateRenderingStatePost(info
, cell
);
973 wxColour
wxHtmlContainerCell::GetBackgroundColour()
983 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
985 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
987 // VZ: I don't know if we should pass absolute or relative coords to
988 // wxHtmlCell::GetLink()? As the base class version just ignores them
989 // anyhow, it hardly matters right now but should still be clarified
990 return cell
? cell
->GetLink(x
, y
) : NULL
;
995 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
997 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
1000 m_LastCell
->SetNext(f
);
1002 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
1010 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
1012 if (tag
.HasParam(wxT("ALIGN")))
1014 wxString alg
= tag
.GetParam(wxT("ALIGN"));
1016 if (alg
== wxT("CENTER"))
1017 SetAlignHor(wxHTML_ALIGN_CENTER
);
1018 else if (alg
== wxT("LEFT"))
1019 SetAlignHor(wxHTML_ALIGN_LEFT
);
1020 else if (alg
== wxT("JUSTIFY"))
1021 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
1022 else if (alg
== wxT("RIGHT"))
1023 SetAlignHor(wxHTML_ALIGN_RIGHT
);
1030 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
1032 if (tag
.HasParam(wxT("WIDTH")))
1035 wxString wd
= tag
.GetParam(wxT("WIDTH"));
1037 if (wd
[wd
.Length()-1] == wxT('%'))
1039 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
1040 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
1044 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
1045 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
1053 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
1057 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
1059 const wxHtmlCell
*r
= cell
->Find(condition
, param
);
1067 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
1068 unsigned flags
) const
1070 if ( flags
& wxHTML_FIND_EXACT
)
1072 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1074 int cx
= cell
->GetPosX(),
1075 cy
= cell
->GetPosY();
1077 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
1078 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
1080 return cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
1084 else if ( flags
& wxHTML_FIND_NEAREST_AFTER
)
1087 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1089 if ( cell
->IsFormattingCell() )
1091 int cellY
= cell
->GetPosY();
1092 if (!( y
< cellY
|| (y
< cellY
+ cell
->GetHeight() &&
1093 x
< cell
->GetPosX() + cell
->GetWidth()) ))
1096 c
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1100 else if ( flags
& wxHTML_FIND_NEAREST_BEFORE
)
1102 wxHtmlCell
*c2
, *c
= NULL
;
1103 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
1105 if ( cell
->IsFormattingCell() )
1107 int cellY
= cell
->GetPosY();
1108 if (!( cellY
+ cell
->GetHeight() <= y
||
1109 (y
>= cellY
&& x
>= cell
->GetPosX()) ))
1111 c2
= cell
->FindCellByPos(x
- cell
->GetPosX(), y
- cellY
, flags
);
1122 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
1124 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
1126 cell
->OnMouseClick(parent
, x
, y
, event
);
1131 wxHtmlCell
*wxHtmlContainerCell::GetFirstTerminal() const
1136 for (wxHtmlCell
*c
= m_Cells
; c
; c
= c
->GetNext())
1138 c2
= c
->GetFirstTerminal();
1146 wxHtmlCell
*wxHtmlContainerCell::GetLastTerminal() const
1150 // most common case first:
1151 wxHtmlCell
*c
= m_LastCell
->GetLastTerminal();
1156 wxHtmlCell
*c2
= NULL
;
1157 for (c
= m_Cells
; c
; c
= c
->GetNext())
1159 ctmp
= c
->GetLastTerminal();
1170 static bool IsEmptyContainer(wxHtmlContainerCell
*cell
)
1172 for ( wxHtmlCell
*c
= cell
->GetFirstChild(); c
; c
= c
->GetNext() )
1174 if ( !c
->IsTerminalCell() || !c
->IsFormattingCell() )
1180 void wxHtmlContainerCell::RemoveExtraSpacing(bool top
, bool bottom
)
1183 SetIndent(0, wxHTML_INDENT_TOP
);
1185 SetIndent(0, wxHTML_INDENT_BOTTOM
);
1190 wxHtmlContainerCell
*cont
;
1193 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1195 if ( c
->IsTerminalCell() )
1197 if ( !c
->IsFormattingCell() )
1202 cont
= (wxHtmlContainerCell
*)c
;
1203 if ( IsEmptyContainer(cont
) )
1205 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
);
1209 cont
->RemoveExtraSpacing(true, false);
1219 for ( c
= m_Cells
; c
; c
= c
->GetNext() )
1222 for ( int i
= arr
.GetCount() - 1; i
>= 0; i
--)
1224 c
= (wxHtmlCell
*)arr
[i
];
1225 if ( c
->IsTerminalCell() )
1227 if ( !c
->IsFormattingCell() )
1232 cont
= (wxHtmlContainerCell
*)c
;
1233 if ( IsEmptyContainer(cont
) )
1235 cont
->SetIndent(0, wxHTML_INDENT_VERTICAL
); }
1238 cont
->RemoveExtraSpacing(false, true);
1250 // --------------------------------------------------------------------------
1252 // --------------------------------------------------------------------------
1254 IMPLEMENT_ABSTRACT_CLASS(wxHtmlColourCell
, wxHtmlCell
)
1256 void wxHtmlColourCell::Draw(wxDC
& dc
,
1258 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1259 wxHtmlRenderingInfo
& info
)
1261 DrawInvisible(dc
, x
, y
, info
);
1264 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
1265 int WXUNUSED(x
), int WXUNUSED(y
),
1266 wxHtmlRenderingInfo
& info
)
1268 wxHtmlRenderingState
& state
= info
.GetState();
1269 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
1271 state
.SetFgColour(m_Colour
);
1272 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1273 dc
.SetTextForeground(m_Colour
);
1275 dc
.SetTextForeground(
1276 info
.GetStyle().GetSelectedTextColour(m_Colour
));
1278 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
1280 state
.SetBgColour(m_Colour
);
1281 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
1283 dc
.SetTextBackground(m_Colour
);
1284 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
1288 wxColour c
= info
.GetStyle().GetSelectedTextBgColour(m_Colour
);
1289 dc
.SetTextBackground(c
);
1290 dc
.SetBackground(wxBrush(c
, wxSOLID
));
1298 // ---------------------------------------------------------------------------
1300 // ---------------------------------------------------------------------------
1302 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFontCell
, wxHtmlCell
)
1304 void wxHtmlFontCell::Draw(wxDC
& dc
,
1305 int WXUNUSED(x
), int WXUNUSED(y
),
1306 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1307 wxHtmlRenderingInfo
& WXUNUSED(info
))
1312 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
1313 wxHtmlRenderingInfo
& WXUNUSED(info
))
1325 // ---------------------------------------------------------------------------
1327 // ---------------------------------------------------------------------------
1329 IMPLEMENT_ABSTRACT_CLASS(wxHtmlWidgetCell
, wxHtmlCell
)
1331 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
1335 m_Wnd
->GetSize(&sx
, &sy
);
1336 m_Width
= sx
, m_Height
= sy
;
1341 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
1342 int WXUNUSED(x
), int WXUNUSED(y
),
1343 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
1344 wxHtmlRenderingInfo
& WXUNUSED(info
))
1346 int absx
= 0, absy
= 0, stx
, sty
;
1347 wxHtmlCell
*c
= this;
1351 absx
+= c
->GetPosX();
1352 absy
+= c
->GetPosY();
1356 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1357 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1362 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
1363 int WXUNUSED(x
), int WXUNUSED(y
),
1364 wxHtmlRenderingInfo
& WXUNUSED(info
))
1366 int absx
= 0, absy
= 0, stx
, sty
;
1367 wxHtmlCell
*c
= this;
1371 absx
+= c
->GetPosX();
1372 absy
+= c
->GetPosY();
1376 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
1377 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
1382 void wxHtmlWidgetCell::Layout(int w
)
1384 if (m_WidthFloat
!= 0)
1386 m_Width
= (w
* m_WidthFloat
) / 100;
1387 m_Wnd
->SetSize(m_Width
, m_Height
);
1390 wxHtmlCell::Layout(w
);
1395 // ----------------------------------------------------------------------------
1396 // wxHtmlTerminalCellsInterator
1397 // ----------------------------------------------------------------------------
1399 const wxHtmlCell
* wxHtmlTerminalCellsInterator::operator++()
1406 if ( m_pos
== m_to
)
1412 if ( m_pos
->GetNext() )
1413 m_pos
= m_pos
->GetNext();
1416 // we must go up the hierarchy until we reach container where this
1417 // is not the last child, and then go down to first terminal cell:
1418 while ( m_pos
->GetNext() == NULL
)
1420 m_pos
= m_pos
->GetParent();
1424 m_pos
= m_pos
->GetNext();
1426 while ( m_pos
->GetFirstChild() != NULL
)
1427 m_pos
= m_pos
->GetFirstChild();
1428 } while ( !m_pos
->IsTerminalCell() );
1439 //-----------------------------------------------------------------------------
1441 //-----------------------------------------------------------------------------
1443 class wxHtmlCellModule
: public wxModule
1445 DECLARE_DYNAMIC_CLASS(wxHtmlCellModule
)
1447 wxHtmlCellModule() : wxModule() {}
1448 bool OnInit() { return true; }
1451 wxDELETE(gs_cursorLink
);
1452 wxDELETE(gs_cursorText
);
1456 IMPLEMENT_DYNAMIC_CLASS(wxHtmlCellModule
, wxModule
)