]>
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()
50 if (m_Link
) delete m_Link
;
51 if (m_Next
) delete m_Next
;
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);
64 ((wxHtmlWindow
*)parent
)->OnLinkClicked(lnk2
);
65 // note : this overcasting is legal because parent is *always* wxHtmlWindow
71 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
) const
73 if ((!m_CanLiveOnPagebreak
) &&
74 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
)
77 if (m_Next
!= NULL
) m_Next
->AdjustPagebreak(pagebreak
);
83 if (m_Next
!= NULL
) return m_Next
->AdjustPagebreak(pagebreak
);
90 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
92 if (m_Link
) delete m_Link
;
94 if (link
.GetHref() != wxEmptyString
)
95 m_Link
= new wxHtmlLinkInfo(link
);
100 void wxHtmlCell::Layout(int w
)
103 if (m_Next
) m_Next
->Layout(w
);
107 void wxHtmlCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
109 if (m_Next
) m_Next
->Draw(dc
, x
, y
, view_y1
, view_y2
);
114 void wxHtmlCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
116 if (m_Next
) m_Next
->DrawInvisible(dc
, x
, y
);
121 const wxHtmlCell
* wxHtmlCell::Find(int condition
, const void* param
) const
123 if (m_Next
) return m_Next
->Find(condition
, param
);
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
136 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
137 SetCanLiveOnPagebreak(FALSE
);
142 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
144 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
145 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
150 //-----------------------------------------------------------------------------
151 // wxHtmlContainerCell
152 //-----------------------------------------------------------------------------
155 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
157 m_Cells
= m_LastCell
= NULL
;
159 if (m_Parent
) m_Parent
->InsertCell(this);
160 m_AlignHor
= wxHTML_ALIGN_LEFT
;
161 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
162 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
163 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
164 m_UseBkColour
= FALSE
;
167 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
171 wxHtmlContainerCell::~wxHtmlContainerCell()
173 if (m_Cells
) delete m_Cells
;
178 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
180 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
181 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
182 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
183 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
184 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
190 int wxHtmlContainerCell::GetIndent(int ind
) const
192 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
193 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
194 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
195 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
196 else return -1; /* BUG! Should not be called... */
202 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
205 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
206 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
207 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
208 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
209 if (p
) return wxHTML_UNITS_PERCENT
;
210 else return wxHTML_UNITS_PIXELS
;
215 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
217 if (!m_CanLiveOnPagebreak
)
218 return wxHtmlCell::AdjustPagebreak(pagebreak
);
222 wxHtmlCell
*c
= GetFirstCell();
224 int pbrk
= *pagebreak
- m_PosY
;
228 if (c
->AdjustPagebreak(&pbrk
)) rt
= TRUE
;
231 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
238 void wxHtmlContainerCell::Layout(int w
)
240 if (m_LastLayout
== w
)
242 wxHtmlCell::Layout(w
);
246 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
247 long xpos
= 0, ypos
= m_IndentTop
;
248 int xdelta
= 0, ybasicpos
= 0, ydiff
;
249 int s_width
, s_indent
;
250 int ysizeup
= 0, ysizedown
= 0;
251 int MaxLineWidth
= 0;
261 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
263 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
264 else m_Width
= m_WidthFloat
* w
/ 100;
268 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
269 else m_Width
= m_WidthFloat
;
274 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
275 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
276 m_Cells
->Layout(m_Width
- (l
+ r
));
285 // adjust indentation:
286 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
287 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
294 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
295 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
296 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
298 ydiff
= cell
->GetHeight() + ybasicpos
;
300 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
301 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
303 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
304 xpos
+= cell
->GetWidth();
305 cell
= cell
->GetNext();
308 // force new line if occured:
309 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
311 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
312 if (ysizeup
< 0) ysizeup
= 0;
313 if (ysizedown
< 0) ysizedown
= 0;
314 switch (m_AlignHor
) {
315 case wxHTML_ALIGN_LEFT
:
316 case wxHTML_ALIGN_JUSTIFY
:
319 case wxHTML_ALIGN_RIGHT
:
320 xdelta
= 0 + (s_width
- xpos
);
322 case wxHTML_ALIGN_CENTER
:
323 xdelta
= 0 + (s_width
- xpos
) / 2;
326 if (xdelta
< 0) xdelta
= 0;
331 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
334 line
->SetPos(line
->GetPosX() + xdelta
,
335 ypos
+ line
->GetPosY());
336 line
= line
->GetNext();
341 int step
= (s_width
- xpos
);
342 if (step
< 0) step
= 0;
344 if (xcnt
> 0) while (line
!= cell
)
346 line
->SetPos(line
->GetPosX() + s_indent
+
347 (counter
++ * step
/ xcnt
),
348 ypos
+ line
->GetPosY());
349 line
= line
->GetNext();
356 ysizeup
= ysizedown
= 0;
361 // setup height & width, depending on container layout:
362 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
364 if (m_Height
< m_MinHeight
)
366 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
368 int diff
= m_MinHeight
- m_Height
;
369 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
373 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
374 cell
= cell
->GetNext();
377 m_Height
= m_MinHeight
;
380 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
381 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
385 wxHtmlCell::Layout(w
);
389 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
390 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
392 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
394 // container visible, draw it:
395 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
400 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
402 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
403 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
406 dc
.SetPen(*wxTRANSPARENT_PEN
);
407 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
412 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
413 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
416 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
417 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
419 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
420 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
423 if (m_Cells
) m_Cells
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
425 // container invisible, just proceed font+color changing:
428 if (m_Cells
) m_Cells
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
431 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
436 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
438 if (m_Cells
) m_Cells
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
439 wxHtmlCell::DrawInvisible(dc
, x
, y
);
444 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
446 wxHtmlCell
*c
= m_Cells
;
451 cx
= c
->GetPosX(), cy
= c
->GetPosY();
452 cw
= c
->GetWidth(), ch
= c
->GetHeight();
453 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
454 return c
->GetLink(x
- cx
, y
- cy
);
462 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
464 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
467 m_LastCell
->SetNext(f
);
469 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
477 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
479 if (tag
.HasParam(wxT("ALIGN")))
481 wxString alg
= tag
.GetParam(wxT("ALIGN"));
483 if (alg
== wxT("CENTER"))
484 SetAlignHor(wxHTML_ALIGN_CENTER
);
485 else if (alg
== wxT("LEFT"))
486 SetAlignHor(wxHTML_ALIGN_LEFT
);
487 else if (alg
== wxT("JUSTIFY"))
488 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
489 else if (alg
== wxT("RIGHT"))
490 SetAlignHor(wxHTML_ALIGN_RIGHT
);
497 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
499 if (tag
.HasParam(wxT("WIDTH")))
502 wxString wd
= tag
.GetParam(wxT("WIDTH"));
504 if (wd
[wd
.Length()-1] == wxT('%'))
506 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
507 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
511 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
512 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
520 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
522 const wxHtmlCell
*r
= NULL
;
526 r
= m_Cells
->Find(condition
, param
);
530 return wxHtmlCell::Find(condition
, param
);
535 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
539 wxHtmlCell
*c
= m_Cells
;
542 if ( (c
->GetPosX() <= x
) &&
543 (c
->GetPosY() <= y
) &&
544 (c
->GetPosX() + c
->GetWidth() > x
) &&
545 (c
->GetPosY() + c
->GetHeight() > y
))
547 c
->OnMouseClick(parent
, x
- c
->GetPosX(), y
- c
->GetPosY(), event
);
559 //--------------------------------------------------------------------------------
561 //--------------------------------------------------------------------------------
563 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
565 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
566 dc
.SetTextForeground(m_Colour
);
567 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
569 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
570 dc
.SetTextBackground(m_Colour
);
572 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
575 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
577 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
578 dc
.SetTextForeground(m_Colour
);
579 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
581 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
582 dc
.SetTextBackground(m_Colour
);
584 wxHtmlCell::DrawInvisible(dc
, x
, y
);
590 //--------------------------------------------------------------------------------
592 //--------------------------------------------------------------------------------
594 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
597 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
600 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
603 wxHtmlCell::DrawInvisible(dc
, x
, y
);
613 //--------------------------------------------------------------------------------
615 //--------------------------------------------------------------------------------
617 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
621 m_Wnd
->GetSize(&sx
, &sy
);
622 m_Width
= sx
, m_Height
= sy
;
627 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
629 int absx
= 0, absy
= 0, stx
, sty
;
630 wxHtmlCell
*c
= this;
634 absx
+= c
->GetPosX();
635 absy
+= c
->GetPosY();
639 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
640 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
642 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
647 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
649 int absx
= 0, absy
= 0, stx
, sty
;
650 wxHtmlCell
*c
= this;
654 absx
+= c
->GetPosX();
655 absy
+= c
->GetPosY();
659 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
660 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
662 wxHtmlCell::DrawInvisible(dc
, x
, y
);
667 void wxHtmlWidgetCell::Layout(int w
)
669 if (m_WidthFloat
!= 0)
671 m_Width
= (w
* m_WidthFloat
) / 100;
672 m_Wnd
->SetSize(m_Width
, m_Height
);
675 wxHtmlCell::Layout(w
);