]>
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 //-----------------------------------------------------------------------------
37 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
39 bool WXUNUSED(middle
),
42 wxString lnk
= GetLink(x
, y
);
43 if (lnk
!= wxEmptyString
)
44 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk
);
45 // note : this overcasting is legal because parent is *always* wxHtmlWindow
50 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
)
53 if ((!m_CanLiveOnPagebreak
) &&
54 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
) {
56 if (m_Next
!= NULL
) m_Next
-> AdjustPagebreak(pagebreak
);
61 if (m_Next
!= NULL
) return m_Next
-> AdjustPagebreak(pagebreak
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
77 m_Word
.Replace(wxT(" "), wxT(" "), TRUE
);
78 m_Word
.Replace(wxT("""), wxT("\""), TRUE
);
79 m_Word
.Replace(wxT("<"), wxT("<"), TRUE
);
80 m_Word
.Replace(wxT(">"), wxT(">"), TRUE
);
81 m_Word
.Replace(wxT("&"), wxT("&"), TRUE
);
83 m_Word
.Replace(wxT("  "), wxT(" "), TRUE
);
84 m_Word
.Replace(wxT("" "), wxT("\""), TRUE
);
85 m_Word
.Replace(wxT("< "), wxT("<"), TRUE
);
86 m_Word
.Replace(wxT("> "), wxT(">"), TRUE
);
87 m_Word
.Replace(wxT("& "), wxT("&"), TRUE
);
89 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
90 SetCanLiveOnPagebreak(FALSE
);
95 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
97 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
98 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
103 //-----------------------------------------------------------------------------
104 // wxHtmlContainerCell
105 //-----------------------------------------------------------------------------
108 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
110 m_Cells
= m_LastCell
= NULL
;
112 if (m_Parent
) m_Parent
-> InsertCell(this);
113 m_AlignHor
= wxHTML_ALIGN_LEFT
;
114 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
115 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
116 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
117 m_UseBkColour
= FALSE
;
119 m_MinHeight
= m_MaxLineWidth
= 0;
120 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
125 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
127 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
128 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
129 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
130 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
131 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
136 int wxHtmlContainerCell::GetIndent(int ind
) const
138 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
139 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
140 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
141 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
142 else return -1; /* BUG! Should not be called... */
148 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
151 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
152 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
153 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
154 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
155 if (p
) return wxHTML_UNITS_PERCENT
;
156 else return wxHTML_UNITS_PIXELS
;
161 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
163 if (!m_CanLiveOnPagebreak
)
164 return wxHtmlCell::AdjustPagebreak(pagebreak
);
167 wxHtmlCell
*c
= GetFirstCell();
169 int pbrk
= *pagebreak
- m_PosY
;
172 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
175 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
182 void wxHtmlContainerCell::Layout(int w
)
184 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
185 long xpos
= 0, ypos
= m_IndentTop
;
186 int xdelta
= 0, ybasicpos
= 0, ydiff
;
187 int s_width
, s_indent
;
188 int ysizeup
= 0, ysizedown
= 0;
196 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
197 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
198 else m_Width
= m_WidthFloat
* w
/ 100;
201 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
202 else m_Width
= m_WidthFloat
;
206 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
207 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
208 m_Cells
-> Layout(m_Width
- (l
+ r
));
217 // adjust indentation:
218 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
219 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
224 while (cell
!= NULL
) {
225 switch (m_AlignVer
) {
226 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
227 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
228 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
230 ydiff
= cell
-> GetHeight() + ybasicpos
;
232 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
233 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
235 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
236 xpos
+= cell
-> GetWidth();
237 cell
= cell
-> GetNext();
239 // force new line if occured:
240 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
241 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
242 if (ysizeup
< 0) ysizeup
= 0;
243 if (ysizedown
< 0) ysizedown
= 0;
244 switch (m_AlignHor
) {
245 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
246 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
247 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
249 if (xdelta
< 0) xdelta
= 0;
253 while (line
!= cell
) {
254 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
255 line
= line
-> GetNext();
260 ysizeup
= ysizedown
= 0;
265 // setup height & width, depending on container layout:
266 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
268 if (m_Height
< m_MinHeight
) {
269 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
270 int diff
= m_MinHeight
- m_Height
;
271 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
274 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
275 cell
= cell
-> GetNext();
278 m_Height
= m_MinHeight
;
281 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
282 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
284 wxHtmlCell::Layout(w
);
288 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
289 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
291 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
293 // container visible, draw it:
294 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
297 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
299 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
300 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
303 dc
.SetPen(*wxTRANSPARENT_PEN
);
304 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
308 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
309 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
312 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
313 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
315 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
316 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
319 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
321 // container invisible, just proceed font+color changing:
323 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
326 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
331 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
333 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
334 wxHtmlCell::DrawInvisible(dc
, x
, y
);
339 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
341 wxHtmlCell
*c
= m_Cells
;
345 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
346 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
347 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
348 return c
-> GetLink(x
- cx
, y
- cy
);
351 return wxEmptyString
;
356 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
358 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
360 m_LastCell
-> SetNext(f
);
362 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
364 f
-> SetParent(this);
369 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
371 if (tag
.HasParam("ALIGN")) {
372 wxString alg
= tag
.GetParam("ALIGN");
375 SetAlignHor(wxHTML_ALIGN_CENTER
);
376 else if (alg
== "LEFT")
377 SetAlignHor(wxHTML_ALIGN_LEFT
);
378 else if (alg
== "RIGHT")
379 SetAlignHor(wxHTML_ALIGN_RIGHT
);
385 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
387 if (tag
.HasParam("WIDTH")) {
389 wxString wd
= tag
.GetParam("WIDTH");
391 if (wd
[wd
.Length()-1] == '%') {
392 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
393 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
396 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
397 SetWidthFloat(wdi
, wxHTML_UNITS_PIXELS
);
404 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
406 const wxHtmlCell
*r
= NULL
;
409 r
= m_Cells
-> Find(condition
, param
);
413 return wxHtmlCell::Find(condition
, param
);
418 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
421 wxHtmlCell
*c
= m_Cells
;
423 if ( (c
-> GetPosX() <= x
) &&
424 (c
-> GetPosY() <= y
) &&
425 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
426 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
427 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
439 //--------------------------------------------------------------------------------
441 //--------------------------------------------------------------------------------
443 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
445 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
446 dc
.SetTextForeground(m_Colour
);
447 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
448 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
449 dc
.SetTextBackground(m_Colour
);
451 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
454 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
456 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
457 dc
.SetTextForeground(m_Colour
);
458 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
459 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
460 dc
.SetTextBackground(m_Colour
);
462 wxHtmlCell::DrawInvisible(dc
, x
, y
);
468 //--------------------------------------------------------------------------------
470 //--------------------------------------------------------------------------------
472 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
475 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
478 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
481 wxHtmlCell::DrawInvisible(dc
, x
, y
);
491 //--------------------------------------------------------------------------------
493 //--------------------------------------------------------------------------------
495 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
499 m_Wnd
-> GetSize(&sx
, &sy
);
500 m_Width
= sx
, m_Height
= sy
;
505 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
507 int absx
= 0, absy
= 0, stx
, sty
;
508 wxHtmlCell
*c
= this;
511 absx
+= c
-> GetPosX();
512 absy
+= c
-> GetPosY();
513 c
= c
-> GetParent();
516 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
517 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
519 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
524 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
526 int absx
= 0, absy
= 0, stx
, sty
;
527 wxHtmlCell
*c
= this;
530 absx
+= c
-> GetPosX();
531 absy
+= c
-> GetPosY();
532 c
= c
-> GetParent();
535 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
536 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
538 wxHtmlCell::DrawInvisible(dc
, x
, y
);
543 void wxHtmlWidgetCell::Layout(int w
)
545 if (m_WidthFloat
!= 0) {
546 m_Width
= (w
* m_WidthFloat
) / 100;
547 m_Wnd
-> SetSize(m_Width
, m_Height
);
550 wxHtmlCell::Layout(w
);