]>
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
;
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
))
100 const wxHtmlCell
* wxHtmlCell::Find(int WXUNUSED(condition
), const void* WXUNUSED(param
)) const
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
114 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
115 SetCanLiveOnPagebreak(FALSE
);
120 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
122 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
127 //-----------------------------------------------------------------------------
128 // wxHtmlContainerCell
129 //-----------------------------------------------------------------------------
132 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
134 m_Cells
= m_LastCell
= NULL
;
136 if (m_Parent
) m_Parent
->InsertCell(this);
137 m_AlignHor
= wxHTML_ALIGN_LEFT
;
138 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
139 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
140 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
141 m_UseBkColour
= FALSE
;
144 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
148 wxHtmlContainerCell::~wxHtmlContainerCell()
150 wxHtmlCell
*cell
= m_Cells
;
153 wxHtmlCell
*cellNext
= cell
->GetNext();
161 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
163 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
164 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
165 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
166 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
167 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
173 int wxHtmlContainerCell::GetIndent(int ind
) const
175 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
176 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
177 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
178 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
179 else return -1; /* BUG! Should not be called... */
185 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
188 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
189 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
190 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
191 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
192 if (p
) return wxHTML_UNITS_PERCENT
;
193 else return wxHTML_UNITS_PIXELS
;
198 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
) const
200 if (!m_CanLiveOnPagebreak
)
201 return wxHtmlCell::AdjustPagebreak(pagebreak
);
205 wxHtmlCell
*c
= GetFirstCell();
207 int pbrk
= *pagebreak
- m_PosY
;
211 if (c
->AdjustPagebreak(&pbrk
))
216 *pagebreak
= pbrk
+ m_PosY
;
223 void wxHtmlContainerCell::Layout(int w
)
225 if (m_LastLayout
== w
)
227 wxHtmlCell::Layout(w
);
231 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
232 long xpos
= 0, ypos
= m_IndentTop
;
233 int xdelta
= 0, ybasicpos
= 0, ydiff
;
234 int s_width
, s_indent
;
235 int ysizeup
= 0, ysizedown
= 0;
236 int MaxLineWidth
= 0;
246 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
)
248 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
249 else m_Width
= m_WidthFloat
* w
/ 100;
253 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
254 else m_Width
= m_WidthFloat
;
259 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
260 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
261 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
262 cell
->Layout(m_Width
- (l
+ r
));
271 // adjust indentation:
272 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
273 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
280 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
281 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
->GetHeight(); break;
282 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
->GetHeight() / 2; break;
284 ydiff
= cell
->GetHeight() + ybasicpos
;
286 if (cell
->GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
->GetDescent() + ydiff
;
287 if (ybasicpos
+ cell
->GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
->GetDescent());
289 cell
->SetPos(xpos
, ybasicpos
+ cell
->GetDescent());
290 xpos
+= cell
->GetWidth();
291 cell
= cell
->GetNext();
294 // force new line if occured:
295 if ((cell
== NULL
) || (xpos
+ cell
->GetWidth() > s_width
))
297 if (xpos
> MaxLineWidth
) MaxLineWidth
= xpos
;
298 if (ysizeup
< 0) ysizeup
= 0;
299 if (ysizedown
< 0) ysizedown
= 0;
300 switch (m_AlignHor
) {
301 case wxHTML_ALIGN_LEFT
:
302 case wxHTML_ALIGN_JUSTIFY
:
305 case wxHTML_ALIGN_RIGHT
:
306 xdelta
= 0 + (s_width
- xpos
);
308 case wxHTML_ALIGN_CENTER
:
309 xdelta
= 0 + (s_width
- xpos
) / 2;
312 if (xdelta
< 0) xdelta
= 0;
317 if (m_AlignHor
!= wxHTML_ALIGN_JUSTIFY
|| cell
== NULL
)
320 line
->SetPos(line
->GetPosX() + xdelta
,
321 ypos
+ line
->GetPosY());
322 line
= line
->GetNext();
327 int step
= (s_width
- xpos
);
328 if (step
< 0) step
= 0;
330 if (xcnt
> 0) while (line
!= cell
)
332 line
->SetPos(line
->GetPosX() + s_indent
+
333 (counter
++ * step
/ xcnt
),
334 ypos
+ line
->GetPosY());
335 line
= line
->GetNext();
342 ysizeup
= ysizedown
= 0;
347 // setup height & width, depending on container layout:
348 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
350 if (m_Height
< m_MinHeight
)
352 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
)
354 int diff
= m_MinHeight
- m_Height
;
355 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
359 cell
->SetPos(cell
->GetPosX(), cell
->GetPosY() + diff
);
360 cell
= cell
->GetNext();
363 m_Height
= m_MinHeight
;
366 MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
367 if (m_Width
< MaxLineWidth
) m_Width
= MaxLineWidth
;
371 wxHtmlCell::Layout(w
);
375 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
376 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
378 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
380 // container visible, draw it:
381 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
))
386 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
388 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
389 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
392 dc
.SetPen(*wxTRANSPARENT_PEN
);
393 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
398 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
399 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
402 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
403 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
405 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
406 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
411 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
412 cell
->Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
415 // container invisible, just proceed font+color changing:
420 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
421 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
428 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
432 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
433 cell
->DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
439 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
441 wxHtmlCell
*c
= m_Cells
;
446 cx
= c
->GetPosX(), cy
= c
->GetPosY();
447 cw
= c
->GetWidth(), ch
= c
->GetHeight();
448 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
449 return c
->GetLink(x
- cx
, y
- cy
);
457 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
459 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
462 m_LastCell
->SetNext(f
);
464 if (m_LastCell
) while (m_LastCell
->GetNext()) m_LastCell
= m_LastCell
->GetNext();
472 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
474 if (tag
.HasParam(wxT("ALIGN")))
476 wxString alg
= tag
.GetParam(wxT("ALIGN"));
478 if (alg
== wxT("CENTER"))
479 SetAlignHor(wxHTML_ALIGN_CENTER
);
480 else if (alg
== wxT("LEFT"))
481 SetAlignHor(wxHTML_ALIGN_LEFT
);
482 else if (alg
== wxT("JUSTIFY"))
483 SetAlignHor(wxHTML_ALIGN_JUSTIFY
);
484 else if (alg
== wxT("RIGHT"))
485 SetAlignHor(wxHTML_ALIGN_RIGHT
);
492 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
494 if (tag
.HasParam(wxT("WIDTH")))
497 wxString wd
= tag
.GetParam(wxT("WIDTH"));
499 if (wd
[wd
.Length()-1] == wxT('%'))
501 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
502 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
506 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
507 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
515 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
519 const wxHtmlCell
*r
= NULL
;
521 for (wxHtmlCell
*cell
= m_Cells
; cell
; cell
= cell
->GetNext())
523 r
= cell
->Find(condition
, param
);
532 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
536 wxHtmlCell
*c
= m_Cells
;
539 if ( (c
->GetPosX() <= x
) &&
540 (c
->GetPosY() <= y
) &&
541 (c
->GetPosX() + c
->GetWidth() > x
) &&
542 (c
->GetPosY() + c
->GetHeight() > y
))
544 c
->OnMouseClick(parent
, x
- c
->GetPosX(), y
- c
->GetPosY(), event
);
556 //--------------------------------------------------------------------------------
558 //--------------------------------------------------------------------------------
560 void wxHtmlColourCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
562 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
563 dc
.SetTextForeground(m_Colour
);
564 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
566 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
567 dc
.SetTextBackground(m_Colour
);
571 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
573 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
574 dc
.SetTextForeground(m_Colour
);
575 if (m_Flags
& wxHTML_CLR_BACKGROUND
)
577 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
578 dc
.SetTextBackground(m_Colour
);
585 //--------------------------------------------------------------------------------
587 //--------------------------------------------------------------------------------
589 void wxHtmlFontCell::Draw(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
594 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int WXUNUSED(x
), int WXUNUSED(y
))
606 //--------------------------------------------------------------------------------
608 //--------------------------------------------------------------------------------
610 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
614 m_Wnd
->GetSize(&sx
, &sy
);
615 m_Width
= sx
, m_Height
= sy
;
620 void wxHtmlWidgetCell::Draw(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
), int WXUNUSED(view_y1
), int WXUNUSED(view_y2
))
622 int absx
= 0, absy
= 0, stx
, sty
;
623 wxHtmlCell
*c
= this;
627 absx
+= c
->GetPosX();
628 absy
+= c
->GetPosY();
632 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
633 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
638 void wxHtmlWidgetCell::DrawInvisible(wxDC
& WXUNUSED(dc
), int WXUNUSED(x
), int WXUNUSED(y
))
640 int absx
= 0, absy
= 0, stx
, sty
;
641 wxHtmlCell
*c
= this;
645 absx
+= c
->GetPosX();
646 absy
+= c
->GetPosY();
650 ((wxScrolledWindow
*)(m_Wnd
->GetParent()))->GetViewStart(&stx
, &sty
);
651 m_Wnd
->SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
656 void wxHtmlWidgetCell::Layout(int w
)
658 if (m_WidthFloat
!= 0)
660 m_Width
= (w
* m_WidthFloat
) / 100;
661 m_Wnd
->SetSize(m_Width
, m_Height
);
664 wxHtmlCell::Layout(w
);