]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
135e85544d8f498fc08a44ecf7826702448d6e4e
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
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()
50 if (m_Link
) delete m_Link
;
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);
63 ((wxHtmlWindow
*)parent
)->OnLinkClicked(lnk2
);
64 // note : this overcasting is legal because parent is *always* wxHtmlWindow
70 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
) const
72 if ((!m_CanLiveOnPagebreak
) &&
73 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
84 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
86 if (m_Link
) delete m_Link
;
88 if (link
.GetHref() != wxEmptyString
)
89 m_Link
= new wxHtmlLinkInfo(link
);
94 void wxHtmlCell::Layout(int WXUNUSED(w
))
101 void wxHtmlCell::GetHorizontalConstraints(int *left
, int *right
) const
106 *right
= m_PosX
+ m_Width
- 1;
111 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
125 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
126 SetCanLiveOnPagebreak(FALSE
);
131 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
133 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
138 //-----------------------------------------------------------------------------
139 // wxHtmlContainerCell
140 //-----------------------------------------------------------------------------
143 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
145 m_Cells
= m_LastCell
= NULL
;
147 if (m_Parent
) m_Parent
->InsertCell(this);
148 m_AlignHor
= wxHTML_ALIGN_LEFT
;
149 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
150 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
151 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
152 m_UseBkColour
= FALSE
;
155 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
159 wxHtmlContainerCell::~wxHtmlContainerCell()
161 wxHtmlCell
*cell
= m_Cells
;
164 wxHtmlCell
*cellNext
= cell
->GetNext();
172 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
174 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
175 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
176 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
177 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
178 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
184 int wxHtmlContainerCell::GetIndent(int ind
) const
186 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
187 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
188 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
189 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
190 else return -1; /* BUG! Should not be called... */
196 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
199 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
200 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
201 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
202 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
203 if (p
) return wxHTML_UNITS_PERCENT
;
204 else return wxHTML_UNITS_PIXELS
;
209 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
211 if (!m_CanLiveOnPagebreak
)
212 return wxHtmlCell::AdjustPagebreak(pagebreak
);
216 wxHtmlCell
*c
= GetFirstCell();
218 int pbrk
= *pagebreak
- m_PosY
;
222 if (c
->AdjustPagebreak(&pbrk
))
227 *pagebreak
= pbrk
+ m_PosY
;
234 void wxHtmlContainerCell::Layout(int w
)
236 if (m_LastLayout
== w
)
238 wxHtmlCell::Layout(w
);
242 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
243 long xpos
= 0, ypos
= m_IndentTop
;
244 int xdelta
= 0, ybasicpos
= 0, ydiff
;
245 int s_width
, s_indent
;
246 int ysizeup
= 0, ysizedown
= 0;
247 int MaxLineWidth
= 0;
257 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
259 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
260 else m_Width
= m_WidthFloat
* w
/ 100;
264 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
265 else m_Width
= m_WidthFloat
;
270 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
271 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
272 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
273 cell
->Layout(m_Width
- (l
+ r
));
282 // adjust indentation:
283 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
284 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
291 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
292 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
293 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
295 ydiff
= cell
->GetHeight() + ybasicpos
;
297 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
298 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
300 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
301 xpos
+= cell
->GetWidth();
302 cell
= cell
->GetNext();
305 // force new line if occured:
306 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
308 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
309 if (ysizeup
< 0) ysizeup
= 0;
310 if (ysizedown
< 0) ysizedown
= 0;
311 switch (m_AlignHor
) {
312 case wxHTML_ALIGN_LEFT
:
313 case wxHTML_ALIGN_JUSTIFY
:
316 case wxHTML_ALIGN_RIGHT
:
317 xdelta
= 0 + (s_width
- xpos
);
319 case wxHTML_ALIGN_CENTER
:
320 xdelta
= 0 + (s_width
- xpos
) / 2;
323 if (xdelta
< 0) xdelta
= 0;
328 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
331 line
->SetPos(line
->GetPosX() + xdelta
,
332 ypos
+ line
->GetPosY());
333 line
= line
->GetNext();
338 int step
= (s_width
- xpos
);
339 if (step
< 0) step
= 0;
341 if (xcnt
> 0) while (line
!= cell
)
343 line
->SetPos(line
->GetPosX() + s_indent
+
344 (counter
++ * step
/ xcnt
),
345 ypos
+ line
->GetPosY());
346 line
= line
->GetNext();
353 ysizeup
= ysizedown
= 0;
358 // setup height & width, depending on container layout:
359 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
361 if (m_Height
< m_MinHeight
)
363 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
365 int diff
= m_MinHeight
- m_Height
;
366 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
370 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
371 cell
= cell
->GetNext();
374 m_Height
= m_MinHeight
;
377 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
378 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
382 wxHtmlCell::Layout(w
);
386 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
387 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
389 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
391 // container visible, draw it:
392 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
397 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
399 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
400 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
403 dc
.SetPen(*wxTRANSPARENT_PEN
);
404 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
409 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
410 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
413 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
414 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
416 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
417 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
422 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
423 cell
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
426 // container invisible, just proceed font+color changing:
431 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
432 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
439 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
443 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
444 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
450 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
452 wxHtmlCell
*c
= m_Cells
;
457 cx
= c
->GetPosX(), cy
= c
->GetPosY();
458 cw
= c
->GetWidth(), ch
= c
->GetHeight();
459 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
460 return c
->GetLink(x
- cx
, y
- cy
);
468 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
470 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
473 m_LastCell
->SetNext(f
);
475 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
483 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
485 if (tag
.HasParam(wxT("ALIGN")))
487 wxString alg
= tag
.GetParam(wxT("ALIGN"));
489 if (alg
== wxT("CENTER"))
490 SetAlignHor(wxHTML_ALIGN_CENTER
);
491 else if (alg
== wxT("LEFT"))
492 SetAlignHor(wxHTML_ALIGN_LEFT
);
493 else if (alg
== wxT("JUSTIFY"))
494 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
495 else if (alg
== wxT("RIGHT"))
496 SetAlignHor(wxHTML_ALIGN_RIGHT
);
503 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
505 if (tag
.HasParam(wxT("WIDTH")))
508 wxString wd
= tag
.GetParam(wxT("WIDTH"));
510 if (wd
[wd
.Length()-1] == wxT('%'))
512 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
513 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
517 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
518 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
526 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
530 const wxHtmlCell
*r
= NULL
;
532 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
534 r
= cell
->Find(condition
, param
);
543 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
547 wxHtmlCell
*c
= m_Cells
;
550 if ( (c
->GetPosX() <= x
) &&
551 (c
->GetPosY() <= y
) &&
552 (c
->GetPosX() + c
->GetWidth() > x
) &&
553 (c
->GetPosY() + c
->GetHeight() > y
))
555 c
->OnMouseClick(parent
, x
- c
->GetPosX(), y
- c
->GetPosY(), event
);
565 void wxHtmlContainerCell::GetHorizontalConstraints(int *left
, int *right
) const
567 int cleft
= m_PosX
+ m_Width
, cright
= m_PosX
; // worst case
570 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
572 cell
->GetHorizontalConstraints(&l
, &r
);
589 //--------------------------------------------------------------------------------
591 //--------------------------------------------------------------------------------
593 void wxHtmlColourCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
595 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
596 dc
.SetTextForeground(m_Colour
);
597 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
599 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
600 dc
.SetTextBackground(m_Colour
);
604 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
606 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
607 dc
.SetTextForeground(m_Colour
);
608 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
610 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
611 dc
.SetTextBackground(m_Colour
);
618 //--------------------------------------------------------------------------------
620 //--------------------------------------------------------------------------------
622 void wxHtmlFontCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
627 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
639 //--------------------------------------------------------------------------------
641 //--------------------------------------------------------------------------------
643 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
647 m_Wnd
->GetSize(&sx
, &sy
);
648 m_Width
= sx
, m_Height
= sy
;
653 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
655 int absx
= 0, absy
= 0, stx
, sty
;
656 wxHtmlCell
*c
= this;
660 absx
+= c
->GetPosX();
661 absy
+= c
->GetPosY();
665 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
666 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
671 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
))
673 int absx
= 0, absy
= 0, stx
, sty
;
674 wxHtmlCell
*c
= this;
678 absx
+= c
->GetPosX();
679 absy
+= c
->GetPosY();
683 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
684 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
689 void wxHtmlWidgetCell::Layout(int w
)
691 if (m_WidthFloat
!= 0)
693 m_Width
= (w
* m_WidthFloat
) / 100;
694 m_Wnd
->SetSize(m_Width
, m_Height
);
697 wxHtmlCell::Layout(w
);