1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlCell - basic element of HTML output
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
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"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 wxHtmlCell::wxHtmlCell() : wxObject()
44 m_Width
= m_Height
= m_Descent
= 0;
45 m_CanLiveOnPagebreak
= TRUE
;
49 wxHtmlCell::~wxHtmlCell()
55 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
56 const wxMouseEvent
& event
)
58 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
61 wxHtmlLinkInfo
lnk2(*lnk
);
62 lnk2
.SetEvent(&event
);
63 lnk2
.SetHtmlCell(this);
65 // note : this cast is legal because parent is *always* wxHtmlWindow
66 wxStaticCast(parent
, wxHtmlWindow
)->OnLinkClicked(lnk2
);
72 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
, int* WXUNUSED(known_pagebreaks
), int WXUNUSED(number_of_pages
)) const
74 if ((!m_CanLiveOnPagebreak
) &&
75 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
86 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
88 if (m_Link
) delete m_Link
;
90 if (link
.GetHref() != wxEmptyString
)
91 m_Link
= new wxHtmlLinkInfo(link
);
96 void wxHtmlCell::Layout(int WXUNUSED(w
))
103 void wxHtmlCell::GetHorizontalConstraints(int *left
, int *right
) const
108 *right
= m_PosX
+ m_Width
;
113 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
119 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
,
120 unsigned flags
) const
122 if ( (flags
& wxHTML_FIND_TERMINAL
) &&
123 x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
125 return wxConstCast(this, wxHtmlCell
);
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
138 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
139 SetCanLiveOnPagebreak(FALSE
);
144 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
,
145 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
146 wxHtmlRenderingState
& state
)
148 if (state
.GetSelectionState() == wxHTML_SEL_IN
&&
149 dc
.GetBackgroundMode() != wxSOLID
)
151 dc
.SetBackgroundMode(wxSOLID
);
152 dc
.SetTextBackground(
153 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
154 dc
.SetTextForeground(
155 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
));
157 else if (state
.GetSelectionState() == wxHTML_SEL_OUT
&&
158 dc
.GetBackgroundMode() == wxSOLID
)
160 dc
.SetBackgroundMode(wxTRANSPARENT
);
161 dc
.SetTextForeground(state
.GetFgColour());
162 dc
.SetTextBackground(state
.GetBgColour());
165 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
170 //-----------------------------------------------------------------------------
171 // wxHtmlContainerCell
172 //-----------------------------------------------------------------------------
175 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
177 m_Cells
= m_LastCell
= NULL
;
179 if (m_Parent
) m_Parent
->InsertCell(this);
180 m_AlignHor
= wxHTML_ALIGN_LEFT
;
181 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
182 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
183 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
184 m_UseBkColour
= FALSE
;
187 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
191 wxHtmlContainerCell::~wxHtmlContainerCell()
193 wxHtmlCell
*cell
= m_Cells
;
196 wxHtmlCell
*cellNext
= cell
->GetNext();
204 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
206 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
207 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
208 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
209 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
210 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
216 int wxHtmlContainerCell::GetIndent(int ind
) const
218 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
219 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
220 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
221 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
222 else return -1; /* BUG! Should not be called... */
228 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
231 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
232 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
233 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
234 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
235 if (p
) return wxHTML_UNITS_PERCENT
;
236 else return wxHTML_UNITS_PIXELS
;
241 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
, int* known_pagebreaks
, int number_of_pages
) const
243 if (!m_CanLiveOnPagebreak
)
244 return wxHtmlCell::AdjustPagebreak(pagebreak
, known_pagebreaks
, number_of_pages
);
248 wxHtmlCell
*c
= GetFirstCell();
250 int pbrk
= *pagebreak
- m_PosY
;
254 if (c
->AdjustPagebreak(&pbrk
, known_pagebreaks
, number_of_pages
))
259 *pagebreak
= pbrk
+ m_PosY
;
266 void wxHtmlContainerCell::Layout(int w
)
268 wxHtmlCell::Layout(w
);
270 if (m_LastLayout
== w
) return;
272 // VS: Any attempt to layout with negative or zero width leads to hell,
273 // but we can't ignore such attempts completely, since it sometimes
274 // happen (e.g. when trying how small a table can be). The best thing we
275 // can do is to set the width of child cells to zero
279 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
281 // this does two things: it recursively calls this code on all
282 // child contrainers and resets children's position to (0,0)
286 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
287 long xpos
= 0, ypos
= m_IndentTop
;
288 int xdelta
= 0, ybasicpos
= 0, ydiff
;
289 int s_width
, s_indent
;
290 int ysizeup
= 0, ysizedown
= 0;
291 int MaxLineWidth
= 0;
301 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
303 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
304 else m_Width
= m_WidthFloat
* w
/ 100;
308 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
309 else m_Width
= m_WidthFloat
;
314 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
315 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
316 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
317 cell
->Layout(m_Width
- (l
+ r
));
326 // adjust indentation:
327 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
328 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
335 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
336 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
337 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
339 ydiff
= cell
->GetHeight() + ybasicpos
;
341 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
342 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
344 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
345 xpos
+= cell
->GetWidth();
346 cell
= cell
->GetNext();
349 // force new line if occured:
350 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
352 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
353 if (ysizeup
< 0) ysizeup
= 0;
354 if (ysizedown
< 0) ysizedown
= 0;
355 switch (m_AlignHor
) {
356 case wxHTML_ALIGN_LEFT
:
357 case wxHTML_ALIGN_JUSTIFY
:
360 case wxHTML_ALIGN_RIGHT
:
361 xdelta
= 0 + (s_width
- xpos
);
363 case wxHTML_ALIGN_CENTER
:
364 xdelta
= 0 + (s_width
- xpos
) / 2;
367 if (xdelta
< 0) xdelta
= 0;
372 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
375 line
->SetPos(line
->GetPosX() + xdelta
,
376 ypos
+ line
->GetPosY());
377 line
= line
->GetNext();
382 int step
= (s_width
- xpos
);
383 if (step
< 0) step
= 0;
385 if (xcnt
> 0) while (line
!= cell
)
387 line
->SetPos(line
->GetPosX() + s_indent
+
388 (counter
++ * step
/ xcnt
),
389 ypos
+ line
->GetPosY());
390 line
= line
->GetNext();
397 ysizeup
= ysizedown
= 0;
402 // setup height & width, depending on container layout:
403 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
405 if (m_Height
< m_MinHeight
)
407 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
409 int diff
= m_MinHeight
- m_Height
;
410 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
414 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
415 cell
= cell
->GetNext();
418 m_Height
= m_MinHeight
;
421 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
422 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
427 void wxHtmlContainerCell::UpdateRenderingStatePre(wxHtmlRenderingState
& state
,
428 wxHtmlCell
*cell
) const
430 wxHtmlSelection
*s
= state
.GetSelection();
432 if (s
->GetFromCell() == cell
|| s
->GetToCell() == cell
)
434 state
.SetSelectionState(wxHTML_SEL_CHANGING
);
438 void wxHtmlContainerCell::UpdateRenderingStatePost(wxHtmlRenderingState
& state
,
439 wxHtmlCell
*cell
) const
441 wxHtmlSelection
*s
= state
.GetSelection();
443 if (s
->GetFromCell() == cell
)
444 state
.SetSelectionState(wxHTML_SEL_IN
);
445 else if (s
->GetToCell() == cell
)
446 state
.SetSelectionState(wxHTML_SEL_OUT
);
449 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
450 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
452 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
,
453 wxHtmlRenderingState
& state
)
455 // container visible, draw it:
456 if ((y
+ m_PosY
<= view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
460 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
462 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
463 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
466 dc
.SetPen(*wxTRANSPARENT_PEN
);
467 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
472 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
473 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
476 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
477 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
, y
+ m_PosY
);
479 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
480 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
, y
+ m_PosY
+ m_Height
- 1);
485 // draw container's contents:
486 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
488 UpdateRenderingStatePre(state
, cell
);
490 x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
,
492 UpdateRenderingStatePost(state
, cell
);
496 // container invisible, just proceed font+color changing:
499 DrawInvisible(dc
, x
, y
, state
);
505 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
,
506 wxHtmlRenderingState
& state
)
510 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
512 UpdateRenderingStatePre(state
, cell
);
513 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
, state
);
514 UpdateRenderingStatePost(state
, cell
);
520 wxColour
wxHtmlContainerCell::GetBackgroundColour()
530 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
532 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
534 // VZ: I don't know if we should pass absolute or relative coords to
535 // wxHtmlCell::GetLink()? As the base class version just ignores them
536 // anyhow, it hardly matters right now but should still be clarified
537 return cell
? cell
->GetLink(x
, y
) : NULL
;
542 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
544 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
547 m_LastCell
->SetNext(f
);
549 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
557 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
559 if (tag
.HasParam(wxT("ALIGN")))
561 wxString alg
= tag
.GetParam(wxT("ALIGN"));
563 if (alg
== wxT("CENTER"))
564 SetAlignHor(wxHTML_ALIGN_CENTER
);
565 else if (alg
== wxT("LEFT"))
566 SetAlignHor(wxHTML_ALIGN_LEFT
);
567 else if (alg
== wxT("JUSTIFY"))
568 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
569 else if (alg
== wxT("RIGHT"))
570 SetAlignHor(wxHTML_ALIGN_RIGHT
);
577 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
579 if (tag
.HasParam(wxT("WIDTH")))
582 wxString wd
= tag
.GetParam(wxT("WIDTH"));
584 if (wd
[wd
.Length()-1] == wxT('%'))
586 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
587 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
591 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
592 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
600 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
604 const wxHtmlCell
*r
= NULL
;
606 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
608 r
= cell
->Find(condition
, param
);
616 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
,
617 unsigned flags
) const
619 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
621 int cx
= cell
->GetPosX(),
622 cy
= cell
->GetPosY();
624 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
625 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
627 wxHtmlCell
*c
= cell
->FindCellByPos(x
- cx
, y
- cy
, flags
);
628 if (c
== NULL
&& (flags
& wxHTML_FIND_NONTERMINAL
))
629 return wxConstCast(this, wxHtmlContainerCell
);
635 return (flags
& wxHTML_FIND_NONTERMINAL
) ?
636 wxConstCast(this, wxHtmlContainerCell
) : NULL
;
640 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
642 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
644 cell
->OnMouseClick(parent
, x
, y
, event
);
649 void wxHtmlContainerCell::GetHorizontalConstraints(int *left
, int *right
) const
651 int cleft
= m_PosX
+ m_Width
, cright
= m_PosX
; // worst case
654 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
656 cell
->GetHorizontalConstraints(&l
, &r
);
663 cleft
-= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
664 cright
+= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
676 // --------------------------------------------------------------------------
678 // --------------------------------------------------------------------------
680 void wxHtmlColourCell::Draw(wxDC
& dc
,
682 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
683 wxHtmlRenderingState
& state
)
685 DrawInvisible(dc
, x
, y
, state
);
688 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
,
689 int WXUNUSED(x
), int WXUNUSED(y
),
690 wxHtmlRenderingState
& state
)
692 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
694 state
.SetFgColour(m_Colour
);
695 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
696 dc
.SetTextForeground(m_Colour
);
698 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
700 state
.SetBgColour(m_Colour
);
701 if (state
.GetSelectionState() != wxHTML_SEL_IN
)
702 dc
.SetTextBackground(m_Colour
);
703 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
710 // ---------------------------------------------------------------------------
712 // ---------------------------------------------------------------------------
714 void wxHtmlFontCell::Draw(wxDC
& dc
,
715 int WXUNUSED(x
), int WXUNUSED(y
),
716 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
717 wxHtmlRenderingState
& WXUNUSED(state
))
722 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
),
723 wxHtmlRenderingState
& WXUNUSED(state
))
735 // ---------------------------------------------------------------------------
737 // ---------------------------------------------------------------------------
739 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
743 m_Wnd
->GetSize(&sx
, &sy
);
744 m_Width
= sx
, m_Height
= sy
;
749 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
),
750 int WXUNUSED(x
), int WXUNUSED(y
),
751 int WXUNUSED(view_y1
), int WXUNUSED(view_y2
),
752 wxHtmlRenderingState
& WXUNUSED(state
))
754 int absx
= 0, absy
= 0, stx
, sty
;
755 wxHtmlCell
*c
= this;
759 absx
+= c
->GetPosX();
760 absy
+= c
->GetPosY();
764 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
765 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
770 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
),
771 int WXUNUSED(x
), int WXUNUSED(y
),
772 wxHtmlRenderingState
& WXUNUSED(state
))
774 int absx
= 0, absy
= 0, stx
, sty
;
775 wxHtmlCell
*c
= this;
779 absx
+= c
->GetPosX();
780 absy
+= c
->GetPosY();
784 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
785 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
790 void wxHtmlWidgetCell::Layout(int w
)
792 if (m_WidthFloat
!= 0)
794 m_Width
= (w
* m_WidthFloat
) / 100;
795 m_Wnd
->SetSize(m_Width
, m_Height
);
798 wxHtmlCell::Layout(w
);