]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
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
14 #include "wx/wxprec.h"
18 #if wxUSE_HTML && wxUSE_STREAMS
28 #include "wx/html/htmlcell.h"
29 #include "wx/html/htmlwin.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 wxHtmlCell::wxHtmlCell() : wxObject()
41 m_Width
= m_Height
= m_Descent
= 0;
42 m_CanLiveOnPagebreak
= TRUE
;
46 wxHtmlCell::~wxHtmlCell()
48 if (m_Link
) delete m_Link
;
49 if (m_Next
) delete m_Next
;
53 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
54 const wxMouseEvent
& event
)
56 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
59 wxHtmlLinkInfo
lnk2(*lnk
);
60 lnk2
.SetEvent(&event
);
61 lnk2
.SetHtmlCell(this);
62 ((wxHtmlWindow
*)parent
)->OnLinkClicked(lnk2
);
63 // note : this overcasting is legal because parent is *always* wxHtmlWindow
69 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
) const
71 if ((!m_CanLiveOnPagebreak
) &&
72 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
75 if (m_Next
!= NULL
) m_Next
->AdjustPagebreak(pagebreak
);
81 if (m_Next
!= NULL
) return m_Next
->AdjustPagebreak(pagebreak
);
88 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
90 if (m_Link
) delete m_Link
;
92 if (link
.GetHref() != wxEmptyString
)
93 m_Link
= new wxHtmlLinkInfo(link
);
98 void wxHtmlCell::Layout(int w
)
101 if (m_Next
) m_Next
->Layout(w
);
105 void wxHtmlCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
107 if (m_Next
) m_Next
->Draw(dc
, x
, y
, view_y1
, view_y2
);
112 void wxHtmlCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
114 if (m_Next
) m_Next
->DrawInvisible(dc
, x
, y
);
119 const wxHtmlCell
* wxHtmlCell::Find(int condition
, const void* param
) const
121 if (m_Next
) return m_Next
->Find(condition
, param
);
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 view_y1
, int view_y2
)
142 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
143 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
148 //-----------------------------------------------------------------------------
149 // wxHtmlContainerCell
150 //-----------------------------------------------------------------------------
153 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
155 m_Cells
= m_LastCell
= NULL
;
157 if (m_Parent
) m_Parent
->InsertCell(this);
158 m_AlignHor
= wxHTML_ALIGN_LEFT
;
159 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
160 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
161 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
162 m_UseBkColour
= FALSE
;
165 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
169 wxHtmlContainerCell::~wxHtmlContainerCell()
171 if (m_Cells
) delete m_Cells
;
176 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
178 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
179 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
180 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
181 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
182 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
188 int wxHtmlContainerCell::GetIndent(int ind
) const
190 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
191 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
192 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
193 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
194 else return -1; /* BUG! Should not be called... */
200 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
203 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
204 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
205 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
206 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
207 if (p
) return wxHTML_UNITS_PERCENT
;
208 else return wxHTML_UNITS_PIXELS
;
213 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
215 if (!m_CanLiveOnPagebreak
)
216 return wxHtmlCell::AdjustPagebreak(pagebreak
);
220 wxHtmlCell
*c
= GetFirstCell();
222 int pbrk
= *pagebreak
- m_PosY
;
226 if (c
->AdjustPagebreak(&pbrk
)) rt
= TRUE
;
229 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
236 void wxHtmlContainerCell::Layout(int w
)
238 if (m_LastLayout
== w
)
240 wxHtmlCell::Layout(w
);
244 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
245 long xpos
= 0, ypos
= m_IndentTop
;
246 int xdelta
= 0, ybasicpos
= 0, ydiff
;
247 int s_width
, s_indent
;
248 int ysizeup
= 0, ysizedown
= 0;
249 int MaxLineWidth
= 0;
259 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
261 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
262 else m_Width
= m_WidthFloat
* w
/ 100;
266 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
267 else m_Width
= m_WidthFloat
;
272 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
273 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
274 m_Cells
->Layout(m_Width
- (l
+ r
));
283 // adjust indentation:
284 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
285 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
292 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
293 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
294 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
296 ydiff
= cell
->GetHeight() + ybasicpos
;
298 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
299 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
301 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
302 xpos
+= cell
->GetWidth();
303 cell
= cell
->GetNext();
306 // force new line if occured:
307 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
309 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
310 if (ysizeup
< 0) ysizeup
= 0;
311 if (ysizedown
< 0) ysizedown
= 0;
312 switch (m_AlignHor
) {
313 case wxHTML_ALIGN_LEFT
:
314 case wxHTML_ALIGN_JUSTIFY
:
317 case wxHTML_ALIGN_RIGHT
:
318 xdelta
= 0 + (s_width
- xpos
);
320 case wxHTML_ALIGN_CENTER
:
321 xdelta
= 0 + (s_width
- xpos
) / 2;
324 if (xdelta
< 0) xdelta
= 0;
329 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
332 line
->SetPos(line
->GetPosX() + xdelta
,
333 ypos
+ line
->GetPosY());
334 line
= line
->GetNext();
339 int step
= (s_width
- xpos
);
340 if (step
< 0) step
= 0;
342 if (xcnt
> 0) while (line
!= cell
)
344 line
->SetPos(line
->GetPosX() + s_indent
+
345 (counter
++ * step
/ xcnt
),
346 ypos
+ line
->GetPosY());
347 line
= line
->GetNext();
354 ysizeup
= ysizedown
= 0;
359 // setup height & width, depending on container layout:
360 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
362 if (m_Height
< m_MinHeight
)
364 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
366 int diff
= m_MinHeight
- m_Height
;
367 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
371 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
372 cell
= cell
->GetNext();
375 m_Height
= m_MinHeight
;
378 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
379 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
383 wxHtmlCell::Layout(w
);
387 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
388 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
390 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
392 // container visible, draw it:
393 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
398 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
400 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
401 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
404 dc
.SetPen(*wxTRANSPARENT_PEN
);
405 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
410 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
411 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
414 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
415 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
417 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
418 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
421 if (m_Cells
) m_Cells
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
423 // container invisible, just proceed font+color changing:
426 if (m_Cells
) m_Cells
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
429 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
434 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
436 if (m_Cells
) m_Cells
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
437 wxHtmlCell::DrawInvisible(dc
, x
, y
);
442 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
444 wxHtmlCell
*c
= m_Cells
;
449 cx
= c
->GetPosX(), cy
= c
->GetPosY();
450 cw
= c
->GetWidth(), ch
= c
->GetHeight();
451 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
452 return c
->GetLink(x
- cx
, y
- cy
);
460 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
462 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
465 m_LastCell
->SetNext(f
);
467 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
475 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
477 if (tag
.HasParam(wxT("ALIGN")))
479 wxString alg
= tag
.GetParam(wxT("ALIGN"));
481 if (alg
== wxT("CENTER"))
482 SetAlignHor(wxHTML_ALIGN_CENTER
);
483 else if (alg
== wxT("LEFT"))
484 SetAlignHor(wxHTML_ALIGN_LEFT
);
485 else if (alg
== wxT("JUSTIFY"))
486 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
487 else if (alg
== wxT("RIGHT"))
488 SetAlignHor(wxHTML_ALIGN_RIGHT
);
495 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
497 if (tag
.HasParam(wxT("WIDTH")))
500 wxString wd
= tag
.GetParam(wxT("WIDTH"));
502 if (wd
[wd
.Length()-1] == wxT('%'))
504 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
505 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
509 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
510 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
518 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
520 const wxHtmlCell
*r
= NULL
;
524 r
= m_Cells
->Find(condition
, param
);
528 return wxHtmlCell::Find(condition
, param
);
533 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
537 wxHtmlCell
*c
= m_Cells
;
540 if ( (c
->GetPosX() <= x
) &&
541 (c
->GetPosY() <= y
) &&
542 (c
->GetPosX() + c
->GetWidth() > x
) &&
543 (c
->GetPosY() + c
->GetHeight() > y
))
545 c
->OnMouseClick(parent
, x
- c
->GetPosX(), y
- c
->GetPosY(), event
);
557 //--------------------------------------------------------------------------------
559 //--------------------------------------------------------------------------------
561 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
563 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
564 dc
.SetTextForeground(m_Colour
);
565 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
567 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
568 dc
.SetTextBackground(m_Colour
);
570 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
573 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
575 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
576 dc
.SetTextForeground(m_Colour
);
577 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
579 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
580 dc
.SetTextBackground(m_Colour
);
582 wxHtmlCell::DrawInvisible(dc
, x
, y
);
588 //--------------------------------------------------------------------------------
590 //--------------------------------------------------------------------------------
592 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
595 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
598 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
601 wxHtmlCell::DrawInvisible(dc
, x
, y
);
611 //--------------------------------------------------------------------------------
613 //--------------------------------------------------------------------------------
615 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
619 m_Wnd
->GetSize(&sx
, &sy
);
620 m_Width
= sx
, m_Height
= sy
;
625 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
627 int absx
= 0, absy
= 0, stx
, sty
;
628 wxHtmlCell
*c
= this;
632 absx
+= c
->GetPosX();
633 absy
+= c
->GetPosY();
637 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
638 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
640 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
645 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
647 int absx
= 0, absy
= 0, stx
, sty
;
648 wxHtmlCell
*c
= this;
652 absx
+= c
->GetPosX();
653 absy
+= c
->GetPosY();
657 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
658 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
660 wxHtmlCell::DrawInvisible(dc
, x
, y
);
665 void wxHtmlWidgetCell::Layout(int w
)
667 if (m_WidthFloat
!= 0)
669 m_Width
= (w
* m_WidthFloat
) / 100;
670 m_Wnd
->SetSize(m_Width
, m_Height
);
673 wxHtmlCell::Layout(w
);