]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
ffee96c8c02f9742ddbef2a813481132d8beb6ce
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"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 wxHtmlCell::wxHtmlCell() : wxObject()
43 m_Width
= m_Height
= m_Descent
= 0;
44 m_CanLiveOnPagebreak
= TRUE
;
48 wxHtmlCell::~wxHtmlCell()
54 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
55 const wxMouseEvent
& event
)
57 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
60 wxHtmlLinkInfo
lnk2(*lnk
);
61 lnk2
.SetEvent(&event
);
62 lnk2
.SetHtmlCell(this);
64 // note : this cast is legal because parent is *always* wxHtmlWindow
65 wxStaticCast(parent
, wxHtmlWindow
)->OnLinkClicked(lnk2
);
71 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
) const
73 if ((!m_CanLiveOnPagebreak
) &&
74 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
85 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
87 if (m_Link
) delete m_Link
;
89 if (link
.GetHref() != wxEmptyString
)
90 m_Link
= new wxHtmlLinkInfo(link
);
95 void wxHtmlCell::Layout(int WXUNUSED(w
))
102 void wxHtmlCell::GetHorizontalConstraints(int *left
, int *right
) const
107 *right
= m_PosX
+ m_Width
;
112 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
118 wxHtmlCell
*wxHtmlCell::FindCellByPos(wxCoord x
, wxCoord y
) const
120 if ( x
>= 0 && x
< m_Width
&& y
>= 0 && y
< m_Height
)
121 return wxConstCast(this, wxHtmlCell
);
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
134 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
135 SetCanLiveOnPagebreak(FALSE
);
140 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
142 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
147 //-----------------------------------------------------------------------------
148 // wxHtmlContainerCell
149 //-----------------------------------------------------------------------------
152 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
154 m_Cells
= m_LastCell
= NULL
;
156 if (m_Parent
) m_Parent
->InsertCell(this);
157 m_AlignHor
= wxHTML_ALIGN_LEFT
;
158 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
159 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
160 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
161 m_UseBkColour
= FALSE
;
164 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
168 wxHtmlContainerCell::~wxHtmlContainerCell()
170 wxHtmlCell
*cell
= m_Cells
;
173 wxHtmlCell
*cellNext
= cell
->GetNext();
181 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
183 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
184 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
185 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
186 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
187 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
193 int wxHtmlContainerCell::GetIndent(int ind
) const
195 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
196 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
197 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
198 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
199 else return -1; /* BUG! Should not be called... */
205 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
208 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
209 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
210 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
211 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
212 if (p
) return wxHTML_UNITS_PERCENT
;
213 else return wxHTML_UNITS_PIXELS
;
218 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
220 if (!m_CanLiveOnPagebreak
)
221 return wxHtmlCell::AdjustPagebreak(pagebreak
);
225 wxHtmlCell
*c
= GetFirstCell();
227 int pbrk
= *pagebreak
- m_PosY
;
231 if (c
->AdjustPagebreak(&pbrk
))
236 *pagebreak
= pbrk
+ m_PosY
;
243 void wxHtmlContainerCell::Layout(int w
)
245 wxHtmlCell::Layout(w
);
247 if (m_LastLayout
== w
) return;
249 // VS: Any attempt to layout with negative or zero width leads to hell,
250 // but we can't ignore such attempts completely, since it sometimes
251 // happen (e.g. when trying how small a table can be). The best thing we
252 // can do is to set the width of child cells to zero
256 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
258 // this does two things: it recursively calls this code on all
259 // child contrainers and resets children's position to (0,0)
263 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
264 long xpos
= 0, ypos
= m_IndentTop
;
265 int xdelta
= 0, ybasicpos
= 0, ydiff
;
266 int s_width
, s_indent
;
267 int ysizeup
= 0, ysizedown
= 0;
268 int MaxLineWidth
= 0;
278 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
280 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
281 else m_Width
= m_WidthFloat
* w
/ 100;
285 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
286 else m_Width
= m_WidthFloat
;
291 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
292 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
293 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
294 cell
->Layout(m_Width
- (l
+ r
));
303 // adjust indentation:
304 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
305 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
312 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
313 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
314 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
316 ydiff
= cell
->GetHeight() + ybasicpos
;
318 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
319 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
321 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
322 xpos
+= cell
->GetWidth();
323 cell
= cell
->GetNext();
326 // force new line if occured:
327 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
329 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
330 if (ysizeup
< 0) ysizeup
= 0;
331 if (ysizedown
< 0) ysizedown
= 0;
332 switch (m_AlignHor
) {
333 case wxHTML_ALIGN_LEFT
:
334 case wxHTML_ALIGN_JUSTIFY
:
337 case wxHTML_ALIGN_RIGHT
:
338 xdelta
= 0 + (s_width
- xpos
);
340 case wxHTML_ALIGN_CENTER
:
341 xdelta
= 0 + (s_width
- xpos
) / 2;
344 if (xdelta
< 0) xdelta
= 0;
349 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
352 line
->SetPos(line
->GetPosX() + xdelta
,
353 ypos
+ line
->GetPosY());
354 line
= line
->GetNext();
359 int step
= (s_width
- xpos
);
360 if (step
< 0) step
= 0;
362 if (xcnt
> 0) while (line
!= cell
)
364 line
->SetPos(line
->GetPosX() + s_indent
+
365 (counter
++ * step
/ xcnt
),
366 ypos
+ line
->GetPosY());
367 line
= line
->GetNext();
374 ysizeup
= ysizedown
= 0;
379 // setup height & width, depending on container layout:
380 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
382 if (m_Height
< m_MinHeight
)
384 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
386 int diff
= m_MinHeight
- m_Height
;
387 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
391 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
392 cell
= cell
->GetNext();
395 m_Height
= m_MinHeight
;
398 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
399 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
405 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
406 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
408 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
410 // container visible, draw it:
411 if ((y
+ m_PosY
<= view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
415 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
417 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
418 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
421 dc
.SetPen(*wxTRANSPARENT_PEN
);
422 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
427 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
428 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
431 // Check on other platforms whether we should be subtracting
432 // 1. On wxMSW, we get a notch if we do.
437 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
438 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- subtractMe
, y
+ m_PosY
);
440 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
441 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- subtractMe
, y
+ m_PosY
+ m_Height
- 1);
446 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
447 cell
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
450 // container invisible, just proceed font+color changing:
455 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
456 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
463 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
467 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
468 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
473 wxColour
wxHtmlContainerCell::GetBackgroundColour()
483 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
485 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
487 // VZ: I don't know if we should pass absolute or relative coords to
488 // wxHtmlCell::GetLink()? As the base class version just ignores them
489 // anyhow, it hardly matters right now but should still be clarified
490 return cell
? cell
->GetLink(x
, y
) : NULL
;
495 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
497 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
500 m_LastCell
->SetNext(f
);
502 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
510 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
512 if (tag
.HasParam(wxT("ALIGN")))
514 wxString alg
= tag
.GetParam(wxT("ALIGN"));
516 if (alg
== wxT("CENTER"))
517 SetAlignHor(wxHTML_ALIGN_CENTER
);
518 else if (alg
== wxT("LEFT"))
519 SetAlignHor(wxHTML_ALIGN_LEFT
);
520 else if (alg
== wxT("JUSTIFY"))
521 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
522 else if (alg
== wxT("RIGHT"))
523 SetAlignHor(wxHTML_ALIGN_RIGHT
);
530 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
532 if (tag
.HasParam(wxT("WIDTH")))
535 wxString wd
= tag
.GetParam(wxT("WIDTH"));
537 if (wd
[wd
.Length()-1] == wxT('%'))
539 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
540 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
544 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
545 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
553 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
557 const wxHtmlCell
*r
= NULL
;
559 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
561 r
= cell
->Find(condition
, param
);
569 wxHtmlCell
*wxHtmlContainerCell::FindCellByPos(wxCoord x
, wxCoord y
) const
571 for ( const wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext() )
573 int cx
= cell
->GetPosX(),
574 cy
= cell
->GetPosY();
576 if ( (cx
<= x
) && (cx
+ cell
->GetWidth() > x
) &&
577 (cy
<= y
) && (cy
+ cell
->GetHeight() > y
) )
579 return cell
->FindCellByPos(x
- cx
, y
- cy
);
587 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
589 wxHtmlCell
*cell
= FindCellByPos(x
, y
);
591 cell
->OnMouseClick(parent
, x
, y
, event
);
596 void wxHtmlContainerCell::GetHorizontalConstraints(int *left
, int *right
) const
598 int cleft
= m_PosX
+ m_Width
, cright
= m_PosX
; // worst case
601 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
603 cell
->GetHorizontalConstraints(&l
, &r
);
610 cleft
-= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
611 cright
+= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
623 //--------------------------------------------------------------------------------
625 //--------------------------------------------------------------------------------
627 void wxHtmlColourCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
629 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
630 dc
.SetTextForeground(m_Colour
);
631 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
633 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
634 dc
.SetTextBackground(m_Colour
);
638 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
640 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
641 dc
.SetTextForeground(m_Colour
);
642 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
644 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
645 dc
.SetTextBackground(m_Colour
);
652 //--------------------------------------------------------------------------------
654 //--------------------------------------------------------------------------------
656 void wxHtmlFontCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
661 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
673 //--------------------------------------------------------------------------------
675 //--------------------------------------------------------------------------------
677 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
681 m_Wnd
->GetSize(&sx
, &sy
);
682 m_Width
= sx
, m_Height
= sy
;
687 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
689 int absx
= 0, absy
= 0, stx
, sty
;
690 wxHtmlCell
*c
= this;
694 absx
+= c
->GetPosX();
695 absy
+= c
->GetPosY();
699 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
700 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
705 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
))
707 int absx
= 0, absy
= 0, stx
, sty
;
708 wxHtmlCell
*c
= this;
712 absx
+= c
->GetPosX();
713 absy
+= c
->GetPosY();
717 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
718 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
723 void wxHtmlWidgetCell::Layout(int w
)
725 if (m_WidthFloat
!= 0)
727 m_Width
= (w
* m_WidthFloat
) / 100;
728 m_Wnd
->SetSize(m_Width
, m_Height
);
731 wxHtmlCell::Layout(w
);