]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
af01bcbfe67a7c146fc3846c4153c40ce4440b8f
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"
27 #include "wx/html/htmlcell.h"
28 #include "wx/html/htmlwin.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 wxHtmlCell::wxHtmlCell() : wxObject()
40 m_Width
= m_Height
= m_Descent
= 0;
41 m_CanLiveOnPagebreak
= TRUE
;
45 wxHtmlCell::~wxHtmlCell()
47 if (m_Link
) delete m_Link
;
48 if (m_Next
) delete m_Next
;
52 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
53 const wxMouseEvent
& event
)
55 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
58 wxHtmlLinkInfo
lnk2(*lnk
);
59 lnk2
.SetEvent(&event
);
60 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk2
);
61 // note : this overcasting is legal because parent is *always* wxHtmlWindow
67 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
)
69 if ((!m_CanLiveOnPagebreak
) &&
70 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
) {
72 if (m_Next
!= NULL
) m_Next
-> AdjustPagebreak(pagebreak
);
77 if (m_Next
!= NULL
) return m_Next
-> AdjustPagebreak(pagebreak
);
84 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
86 if (m_Link
) delete m_Link
;
87 m_Link
= new wxHtmlLinkInfo(link
);
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
96 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
100 if (m_Word
.Find(wxT('&')) != -1)
102 static wxChar
* substitutions
[][3] =
104 { wxT(" "), wxT("  "), wxT(" ") },
105 { wxT("©"), wxT("© "), wxT("(c)") },
106 { wxT("""), wxT("" "), wxT("\"") },
107 { wxT("<"), wxT("< "), wxT("<") },
108 { wxT(">"), wxT("> "), wxT(">") },
109 { wxT("&"), wxT("& "), wxT("&") /*this one should be last one*/ },
113 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
115 m_Word
.Replace(substitutions
[i
][0], substitutions
[i
][2], TRUE
);
116 m_Word
.Replace(substitutions
[i
][1], substitutions
[i
][2], TRUE
);
120 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
121 SetCanLiveOnPagebreak(FALSE
);
126 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
128 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
129 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
134 //-----------------------------------------------------------------------------
135 // wxHtmlContainerCell
136 //-----------------------------------------------------------------------------
139 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
141 m_Cells
= m_LastCell
= NULL
;
143 if (m_Parent
) m_Parent
-> InsertCell(this);
144 m_AlignHor
= wxHTML_ALIGN_LEFT
;
145 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
146 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
147 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
148 m_UseBkColour
= FALSE
;
150 m_MinHeight
= m_MaxLineWidth
= 0;
151 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
156 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
158 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
159 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
160 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
161 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
162 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
167 int wxHtmlContainerCell::GetIndent(int ind
) const
169 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
170 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
171 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
172 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
173 else return -1; /* BUG! Should not be called... */
179 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
182 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
183 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
184 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
185 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
186 if (p
) return wxHTML_UNITS_PERCENT
;
187 else return wxHTML_UNITS_PIXELS
;
192 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
194 if (!m_CanLiveOnPagebreak
)
195 return wxHtmlCell::AdjustPagebreak(pagebreak
);
198 wxHtmlCell
*c
= GetFirstCell();
200 int pbrk
= *pagebreak
- m_PosY
;
203 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
206 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
213 void wxHtmlContainerCell::Layout(int w
)
215 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
216 long xpos
= 0, ypos
= m_IndentTop
;
217 int xdelta
= 0, ybasicpos
= 0, ydiff
;
218 int s_width
, s_indent
;
219 int ysizeup
= 0, ysizedown
= 0;
227 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
228 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
229 else m_Width
= m_WidthFloat
* w
/ 100;
232 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
233 else m_Width
= m_WidthFloat
;
237 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
238 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
239 m_Cells
-> Layout(m_Width
- (l
+ r
));
248 // adjust indentation:
249 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
250 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
255 while (cell
!= NULL
) {
256 switch (m_AlignVer
) {
257 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
258 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
259 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
261 ydiff
= cell
-> GetHeight() + ybasicpos
;
263 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
264 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
266 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
267 xpos
+= cell
-> GetWidth();
268 cell
= cell
-> GetNext();
270 // force new line if occured:
271 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
272 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
273 if (ysizeup
< 0) ysizeup
= 0;
274 if (ysizedown
< 0) ysizedown
= 0;
275 switch (m_AlignHor
) {
276 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
277 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
278 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
280 if (xdelta
< 0) xdelta
= 0;
284 while (line
!= cell
) {
285 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
286 line
= line
-> GetNext();
291 ysizeup
= ysizedown
= 0;
296 // setup height & width, depending on container layout:
297 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
299 if (m_Height
< m_MinHeight
) {
300 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
301 int diff
= m_MinHeight
- m_Height
;
302 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
305 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
306 cell
= cell
-> GetNext();
309 m_Height
= m_MinHeight
;
312 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
313 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
315 wxHtmlCell::Layout(w
);
319 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
320 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
322 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
324 // container visible, draw it:
325 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
328 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
330 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
331 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
334 dc
.SetPen(*wxTRANSPARENT_PEN
);
335 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
339 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
340 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
343 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
344 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
346 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
347 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
350 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
352 // container invisible, just proceed font+color changing:
354 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
357 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
362 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
364 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
365 wxHtmlCell::DrawInvisible(dc
, x
, y
);
370 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
372 wxHtmlCell
*c
= m_Cells
;
376 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
377 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
378 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
379 return c
-> GetLink(x
- cx
, y
- cy
);
387 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
389 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
391 m_LastCell
-> SetNext(f
);
393 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
395 f
-> SetParent(this);
400 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
402 if (tag
.HasParam(wxT("ALIGN"))) {
403 wxString alg
= tag
.GetParam(wxT("ALIGN"));
405 if (alg
== wxT("CENTER"))
406 SetAlignHor(wxHTML_ALIGN_CENTER
);
407 else if (alg
== wxT("LEFT"))
408 SetAlignHor(wxHTML_ALIGN_LEFT
);
409 else if (alg
== wxT("RIGHT"))
410 SetAlignHor(wxHTML_ALIGN_RIGHT
);
416 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
418 if (tag
.HasParam(wxT("WIDTH"))) {
420 wxString wd
= tag
.GetParam(wxT("WIDTH"));
422 if (wd
[wd
.Length()-1] == wxT('%')) {
423 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
424 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
427 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
428 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
435 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
437 const wxHtmlCell
*r
= NULL
;
440 r
= m_Cells
-> Find(condition
, param
);
444 return wxHtmlCell::Find(condition
, param
);
449 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
452 wxHtmlCell
*c
= m_Cells
;
454 if ( (c
-> GetPosX() <= x
) &&
455 (c
-> GetPosY() <= y
) &&
456 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
457 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
458 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), event
);
470 //--------------------------------------------------------------------------------
472 //--------------------------------------------------------------------------------
474 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
476 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
477 dc
.SetTextForeground(m_Colour
);
478 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
479 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
480 dc
.SetTextBackground(m_Colour
);
482 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
485 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
487 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
488 dc
.SetTextForeground(m_Colour
);
489 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
490 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
491 dc
.SetTextBackground(m_Colour
);
493 wxHtmlCell::DrawInvisible(dc
, x
, y
);
499 //--------------------------------------------------------------------------------
501 //--------------------------------------------------------------------------------
503 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
506 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
509 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
512 wxHtmlCell::DrawInvisible(dc
, x
, y
);
522 //--------------------------------------------------------------------------------
524 //--------------------------------------------------------------------------------
526 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
530 m_Wnd
-> GetSize(&sx
, &sy
);
531 m_Width
= sx
, m_Height
= sy
;
536 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
538 int absx
= 0, absy
= 0, stx
, sty
;
539 wxHtmlCell
*c
= this;
542 absx
+= c
-> GetPosX();
543 absy
+= c
-> GetPosY();
544 c
= c
-> GetParent();
547 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
548 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
550 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
555 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
557 int absx
= 0, absy
= 0, stx
, sty
;
558 wxHtmlCell
*c
= this;
561 absx
+= c
-> GetPosX();
562 absy
+= c
-> GetPosY();
563 c
= c
-> GetParent();
566 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
567 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
569 wxHtmlCell::DrawInvisible(dc
, x
, y
);
574 void wxHtmlWidgetCell::Layout(int w
)
576 if (m_WidthFloat
!= 0) {
577 m_Width
= (w
* m_WidthFloat
) / 100;
578 m_Wnd
-> SetSize(m_Width
, m_Height
);
581 wxHtmlCell::Layout(w
);