]>
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
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
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
126 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
127 SetCanLiveOnPagebreak(FALSE
);
132 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
134 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
139 //-----------------------------------------------------------------------------
140 // wxHtmlContainerCell
141 //-----------------------------------------------------------------------------
144 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
146 m_Cells
= m_LastCell
= NULL
;
148 if (m_Parent
) m_Parent
->InsertCell(this);
149 m_AlignHor
= wxHTML_ALIGN_LEFT
;
150 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
151 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
152 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
153 m_UseBkColour
= FALSE
;
156 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
160 wxHtmlContainerCell::~wxHtmlContainerCell()
162 wxHtmlCell
*cell
= m_Cells
;
165 wxHtmlCell
*cellNext
= cell
->GetNext();
173 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
175 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
176 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
177 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
178 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
179 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
185 int wxHtmlContainerCell::GetIndent(int ind
) const
187 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
188 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
189 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
190 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
191 else return -1; /* BUG! Should not be called... */
197 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
200 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
201 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
202 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
203 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
204 if (p
) return wxHTML_UNITS_PERCENT
;
205 else return wxHTML_UNITS_PIXELS
;
210 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
212 if (!m_CanLiveOnPagebreak
)
213 return wxHtmlCell::AdjustPagebreak(pagebreak
);
217 wxHtmlCell
*c
= GetFirstCell();
219 int pbrk
= *pagebreak
- m_PosY
;
223 if (c
->AdjustPagebreak(&pbrk
))
228 *pagebreak
= pbrk
+ m_PosY
;
235 void wxHtmlContainerCell::Layout(int w
)
237 wxHtmlCell::Layout(w
);
239 if (m_LastLayout
== w
) return;
241 // VS: Any attempt to layout with negative or zero width leads to hell,
242 // but we can't ignore such attempts completely, since it sometimes
243 // happen (e.g. when trying how small a table can be). The best thing we
244 // can do is to set the width of child cells to zero
248 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
250 // this does two things: it recursively calls this code on all child
251 // contrainers and resets children's position to (0,0)
255 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
256 long xpos
= 0, ypos
= m_IndentTop
;
257 int xdelta
= 0, ybasicpos
= 0, ydiff
;
258 int s_width
, s_indent
;
259 int ysizeup
= 0, ysizedown
= 0;
260 int MaxLineWidth
= 0;
270 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
272 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
273 else m_Width
= m_WidthFloat
* w
/ 100;
277 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
278 else m_Width
= m_WidthFloat
;
283 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
284 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
285 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
286 cell
->Layout(m_Width
- (l
+ r
));
295 // adjust indentation:
296 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
297 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
304 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
305 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
306 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
308 ydiff
= cell
->GetHeight() + ybasicpos
;
310 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
311 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
313 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
314 xpos
+= cell
->GetWidth();
315 cell
= cell
->GetNext();
318 // force new line if occured:
319 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
321 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
322 if (ysizeup
< 0) ysizeup
= 0;
323 if (ysizedown
< 0) ysizedown
= 0;
324 switch (m_AlignHor
) {
325 case wxHTML_ALIGN_LEFT
:
326 case wxHTML_ALIGN_JUSTIFY
:
329 case wxHTML_ALIGN_RIGHT
:
330 xdelta
= 0 + (s_width
- xpos
);
332 case wxHTML_ALIGN_CENTER
:
333 xdelta
= 0 + (s_width
- xpos
) / 2;
336 if (xdelta
< 0) xdelta
= 0;
341 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
344 line
->SetPos(line
->GetPosX() + xdelta
,
345 ypos
+ line
->GetPosY());
346 line
= line
->GetNext();
351 int step
= (s_width
- xpos
);
352 if (step
< 0) step
= 0;
354 if (xcnt
> 0) while (line
!= cell
)
356 line
->SetPos(line
->GetPosX() + s_indent
+
357 (counter
++ * step
/ xcnt
),
358 ypos
+ line
->GetPosY());
359 line
= line
->GetNext();
366 ysizeup
= ysizedown
= 0;
371 // setup height & width, depending on container layout:
372 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
374 if (m_Height
< m_MinHeight
)
376 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
378 int diff
= m_MinHeight
- m_Height
;
379 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
383 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
384 cell
= cell
->GetNext();
387 m_Height
= m_MinHeight
;
390 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
391 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
397 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
398 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
400 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
402 // container visible, draw it:
403 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
408 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
410 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
411 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
414 dc
.SetPen(*wxTRANSPARENT_PEN
);
415 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
420 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
421 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
424 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
425 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
427 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
428 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
433 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
434 cell
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
437 // container invisible, just proceed font+color changing:
442 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
443 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
450 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
454 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
455 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
461 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
463 wxHtmlCell
*c
= m_Cells
;
468 cx
= c
->GetPosX(), cy
= c
->GetPosY();
469 cw
= c
->GetWidth(), ch
= c
->GetHeight();
470 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
471 return c
->GetLink(x
- cx
, y
- cy
);
479 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
481 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
484 m_LastCell
->SetNext(f
);
486 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
494 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
496 if (tag
.HasParam(wxT("ALIGN")))
498 wxString alg
= tag
.GetParam(wxT("ALIGN"));
500 if (alg
== wxT("CENTER"))
501 SetAlignHor(wxHTML_ALIGN_CENTER
);
502 else if (alg
== wxT("LEFT"))
503 SetAlignHor(wxHTML_ALIGN_LEFT
);
504 else if (alg
== wxT("JUSTIFY"))
505 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
506 else if (alg
== wxT("RIGHT"))
507 SetAlignHor(wxHTML_ALIGN_RIGHT
);
514 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
516 if (tag
.HasParam(wxT("WIDTH")))
519 wxString wd
= tag
.GetParam(wxT("WIDTH"));
521 if (wd
[wd
.Length()-1] == wxT('%'))
523 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
524 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
528 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
529 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
537 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
541 const wxHtmlCell
*r
= NULL
;
543 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
545 r
= cell
->Find(condition
, param
);
554 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
558 wxHtmlCell
*c
= m_Cells
;
561 if ( (c
->GetPosX() <= x
) &&
562 (c
->GetPosY() <= y
) &&
563 (c
->GetPosX() + c
->GetWidth() > x
) &&
564 (c
->GetPosY() + c
->GetHeight() > y
))
566 c
->OnMouseClick(parent
, x
- c
->GetPosX(), y
- c
->GetPosY(), event
);
576 void wxHtmlContainerCell::GetHorizontalConstraints(int *left
, int *right
) const
578 int cleft
= m_PosX
+ m_Width
, cright
= m_PosX
; // worst case
581 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
583 cell
->GetHorizontalConstraints(&l
, &r
);
590 cleft
-= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
591 cright
+= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
603 //--------------------------------------------------------------------------------
605 //--------------------------------------------------------------------------------
607 void wxHtmlColourCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
609 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
610 dc
.SetTextForeground(m_Colour
);
611 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
613 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
614 dc
.SetTextBackground(m_Colour
);
618 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
620 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
621 dc
.SetTextForeground(m_Colour
);
622 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
624 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
625 dc
.SetTextBackground(m_Colour
);
632 //--------------------------------------------------------------------------------
634 //--------------------------------------------------------------------------------
636 void wxHtmlFontCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
641 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
653 //--------------------------------------------------------------------------------
655 //--------------------------------------------------------------------------------
657 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
661 m_Wnd
->GetSize(&sx
, &sy
);
662 m_Width
= sx
, m_Height
= sy
;
667 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
669 int absx
= 0, absy
= 0, stx
, sty
;
670 wxHtmlCell
*c
= this;
674 absx
+= c
->GetPosX();
675 absy
+= c
->GetPosY();
679 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
680 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
685 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
))
687 int absx
= 0, absy
= 0, stx
, sty
;
688 wxHtmlCell
*c
= this;
692 absx
+= c
->GetPosX();
693 absy
+= c
->GetPosY();
697 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
698 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
703 void wxHtmlWidgetCell::Layout(int w
)
705 if (m_WidthFloat
!= 0)
707 m_Width
= (w
* m_WidthFloat
) / 100;
708 m_Wnd
->SetSize(m_Width
, m_Height
);
711 wxHtmlCell::Layout(w
);