]>
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"
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 lnk2
.SetHtmlCell(this);
61 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk2
);
62 // note : this overcasting is legal because parent is *always* wxHtmlWindow
68 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
)
70 if ((!m_CanLiveOnPagebreak
) &&
71 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
) {
73 if (m_Next
!= NULL
) m_Next
-> AdjustPagebreak(pagebreak
);
78 if (m_Next
!= NULL
) return m_Next
-> AdjustPagebreak(pagebreak
);
85 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
87 if (m_Link
) delete m_Link
;
88 m_Link
= new wxHtmlLinkInfo(link
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
101 if (m_Word
.Find(wxT('&')) != -1)
103 static wxChar
* substitutions
[][3] =
105 { wxT(" "), wxT("  "), wxT(" ") },
106 { wxT("©"), wxT("© "), wxT("(c)") },
107 { wxT("""), wxT("" "), wxT("\"") },
108 { wxT("<"), wxT("< "), wxT("<") },
109 { wxT(">"), wxT("> "), wxT(">") },
110 { wxT("&"), wxT("& "), wxT("&") /*this one should be last one*/ },
114 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
116 m_Word
.Replace(substitutions
[i
][0], substitutions
[i
][2], TRUE
);
117 m_Word
.Replace(substitutions
[i
][1], substitutions
[i
][2], TRUE
);
121 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
122 SetCanLiveOnPagebreak(FALSE
);
127 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
129 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
130 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
135 //-----------------------------------------------------------------------------
136 // wxHtmlContainerCell
137 //-----------------------------------------------------------------------------
140 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
142 m_Cells
= m_LastCell
= NULL
;
144 if (m_Parent
) m_Parent
-> InsertCell(this);
145 m_AlignHor
= wxHTML_ALIGN_LEFT
;
146 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
147 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
148 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
149 m_UseBkColour
= FALSE
;
151 m_MinHeight
= m_MaxLineWidth
= 0;
152 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
157 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
159 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
160 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
161 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
162 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
163 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
168 int wxHtmlContainerCell::GetIndent(int ind
) const
170 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
171 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
172 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
173 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
174 else return -1; /* BUG! Should not be called... */
180 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
183 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
184 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
185 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
186 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
187 if (p
) return wxHTML_UNITS_PERCENT
;
188 else return wxHTML_UNITS_PIXELS
;
193 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
195 if (!m_CanLiveOnPagebreak
)
196 return wxHtmlCell::AdjustPagebreak(pagebreak
);
199 wxHtmlCell
*c
= GetFirstCell();
201 int pbrk
= *pagebreak
- m_PosY
;
204 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
207 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
214 void wxHtmlContainerCell::Layout(int w
)
216 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
217 long xpos
= 0, ypos
= m_IndentTop
;
218 int xdelta
= 0, ybasicpos
= 0, ydiff
;
219 int s_width
, s_indent
;
220 int ysizeup
= 0, ysizedown
= 0;
228 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
229 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
230 else m_Width
= m_WidthFloat
* w
/ 100;
233 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
234 else m_Width
= m_WidthFloat
;
238 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
239 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
240 m_Cells
-> Layout(m_Width
- (l
+ r
));
249 // adjust indentation:
250 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
251 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
256 while (cell
!= NULL
) {
257 switch (m_AlignVer
) {
258 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
259 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
260 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
262 ydiff
= cell
-> GetHeight() + ybasicpos
;
264 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
265 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
267 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
268 xpos
+= cell
-> GetWidth();
269 cell
= cell
-> GetNext();
271 // force new line if occured:
272 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
273 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
274 if (ysizeup
< 0) ysizeup
= 0;
275 if (ysizedown
< 0) ysizedown
= 0;
276 switch (m_AlignHor
) {
277 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
278 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
279 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
281 if (xdelta
< 0) xdelta
= 0;
285 while (line
!= cell
) {
286 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
287 line
= line
-> GetNext();
292 ysizeup
= ysizedown
= 0;
297 // setup height & width, depending on container layout:
298 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
300 if (m_Height
< m_MinHeight
) {
301 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
302 int diff
= m_MinHeight
- m_Height
;
303 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
306 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
307 cell
= cell
-> GetNext();
310 m_Height
= m_MinHeight
;
313 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
314 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
316 wxHtmlCell::Layout(w
);
320 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
321 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
323 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
325 // container visible, draw it:
326 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
329 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
331 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
332 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
335 dc
.SetPen(*wxTRANSPARENT_PEN
);
336 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
340 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
341 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
344 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
345 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
347 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
348 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
351 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
353 // container invisible, just proceed font+color changing:
355 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
358 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
363 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
365 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
366 wxHtmlCell::DrawInvisible(dc
, x
, y
);
371 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
373 wxHtmlCell
*c
= m_Cells
;
377 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
378 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
379 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
380 return c
-> GetLink(x
- cx
, y
- cy
);
388 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
390 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
392 m_LastCell
-> SetNext(f
);
394 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
396 f
-> SetParent(this);
401 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
403 if (tag
.HasParam(wxT("ALIGN"))) {
404 wxString alg
= tag
.GetParam(wxT("ALIGN"));
406 if (alg
== wxT("CENTER"))
407 SetAlignHor(wxHTML_ALIGN_CENTER
);
408 else if (alg
== wxT("LEFT"))
409 SetAlignHor(wxHTML_ALIGN_LEFT
);
410 else if (alg
== wxT("RIGHT"))
411 SetAlignHor(wxHTML_ALIGN_RIGHT
);
417 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
419 if (tag
.HasParam(wxT("WIDTH"))) {
421 wxString wd
= tag
.GetParam(wxT("WIDTH"));
423 if (wd
[wd
.Length()-1] == wxT('%')) {
424 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
425 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
428 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
429 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
436 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
438 const wxHtmlCell
*r
= NULL
;
441 r
= m_Cells
-> Find(condition
, param
);
445 return wxHtmlCell::Find(condition
, param
);
450 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, const wxMouseEvent
& event
)
453 wxHtmlCell
*c
= m_Cells
;
455 if ( (c
-> GetPosX() <= x
) &&
456 (c
-> GetPosY() <= y
) &&
457 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
458 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
459 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), event
);
471 //--------------------------------------------------------------------------------
473 //--------------------------------------------------------------------------------
475 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
477 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
478 dc
.SetTextForeground(m_Colour
);
479 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
480 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
481 dc
.SetTextBackground(m_Colour
);
483 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
486 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
488 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
489 dc
.SetTextForeground(m_Colour
);
490 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
491 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
492 dc
.SetTextBackground(m_Colour
);
494 wxHtmlCell::DrawInvisible(dc
, x
, y
);
500 //--------------------------------------------------------------------------------
502 //--------------------------------------------------------------------------------
504 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
507 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
510 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
513 wxHtmlCell::DrawInvisible(dc
, x
, y
);
523 //--------------------------------------------------------------------------------
525 //--------------------------------------------------------------------------------
527 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
531 m_Wnd
-> GetSize(&sx
, &sy
);
532 m_Width
= sx
, m_Height
= sy
;
537 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
539 int absx
= 0, absy
= 0, stx
, sty
;
540 wxHtmlCell
*c
= this;
543 absx
+= c
-> GetPosX();
544 absy
+= c
-> GetPosY();
545 c
= c
-> GetParent();
548 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
549 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
551 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
556 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
558 int absx
= 0, absy
= 0, stx
, sty
;
559 wxHtmlCell
*c
= this;
562 absx
+= c
-> GetPosX();
563 absy
+= c
-> GetPosY();
564 c
= c
-> GetParent();
567 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
568 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
570 wxHtmlCell::DrawInvisible(dc
, x
, y
);
575 void wxHtmlWidgetCell::Layout(int w
)
577 if (m_WidthFloat
!= 0) {
578 m_Width
= (w
* m_WidthFloat
) / 100;
579 m_Wnd
-> SetSize(m_Width
, m_Height
);
582 wxHtmlCell::Layout(w
);