]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
ac40ae3c7f4389f0eb401ed8b9d0cce5e96aea76
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"
26 #include "wx/html/htmlcell.h"
27 #include "wx/html/htmlwin.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
36 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
38 bool WXUNUSED(middle
),
41 wxString lnk
= GetLink(x
, y
);
42 if (lnk
!= wxEmptyString
)
43 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk
);
44 // note : this overcasting is legal because parent is *always* wxHtmlWindow
49 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
)
52 if ((!m_CanLiveOnPagebreak
) &&
53 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
>= *pagebreak
) {
55 if (m_Next
!= NULL
) m_Next
-> AdjustPagebreak(pagebreak
);
60 if (m_Next
!= NULL
) return m_Next
-> AdjustPagebreak(pagebreak
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
75 m_Word
.Replace(" ", " ", TRUE
);
76 m_Word
.Replace(""", "\"", TRUE
);
77 m_Word
.Replace("<", "<", TRUE
);
78 m_Word
.Replace(">", ">", TRUE
);
79 m_Word
.Replace("&", "&", TRUE
);
80 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
81 SetCanLiveOnPagebreak(FALSE
);
86 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
88 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
89 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
94 //-----------------------------------------------------------------------------
95 // wxHtmlContainerCell
96 //-----------------------------------------------------------------------------
99 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
101 m_Cells
= m_LastCell
= NULL
;
103 if (m_Parent
) m_Parent
-> InsertCell(this);
104 m_AlignHor
= HTML_ALIGN_LEFT
;
105 m_AlignVer
= HTML_ALIGN_BOTTOM
;
106 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
107 m_WidthFloat
= 100; m_WidthFloatUnits
= HTML_UNITS_PERCENT
;
108 m_UseBkColour
= FALSE
;
110 m_MinHeight
= m_MaxLineWidth
= 0;
111 m_MinHeightAlign
= HTML_ALIGN_TOP
;
116 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
118 int val
= (units
== HTML_UNITS_PIXELS
) ? i
: -i
;
119 if (what
& HTML_INDENT_LEFT
) m_IndentLeft
= val
;
120 if (what
& HTML_INDENT_RIGHT
) m_IndentRight
= val
;
121 if (what
& HTML_INDENT_TOP
) m_IndentTop
= val
;
122 if (what
& HTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
127 int wxHtmlContainerCell::GetIndent(int ind
) const
129 if (ind
& HTML_INDENT_LEFT
) return m_IndentLeft
;
130 else if (ind
& HTML_INDENT_RIGHT
) return m_IndentRight
;
131 else if (ind
& HTML_INDENT_TOP
) return m_IndentTop
;
132 else if (ind
& HTML_INDENT_BOTTOM
) return m_IndentBottom
;
133 else return -1; /* BUG! Should not be called... */
139 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
142 if (ind
& HTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
143 else if (ind
& HTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
144 else if (ind
& HTML_INDENT_TOP
) p
= m_IndentTop
< 0;
145 else if (ind
& HTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
146 if (p
) return HTML_UNITS_PERCENT
;
147 else return HTML_UNITS_PIXELS
;
152 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
154 if (!m_CanLiveOnPagebreak
)
155 return wxHtmlCell::AdjustPagebreak(pagebreak
);
157 wxHtmlCell
*c
= GetFirstCell();
161 if (c
-> AdjustPagebreak(pagebreak
)) rt
= TRUE
;
170 void wxHtmlContainerCell::Layout(int w
)
172 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
173 long xpos
= 0, ypos
= m_IndentTop
;
174 int xdelta
= 0, ybasicpos
= 0, ydiff
;
175 int s_width
, s_indent
;
176 int ysizeup
= 0, ysizedown
= 0;
184 if (m_WidthFloatUnits
== HTML_UNITS_PERCENT
) {
185 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
186 else m_Width
= m_WidthFloat
* w
/ 100;
189 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
190 else m_Width
= m_WidthFloat
;
194 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
195 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
196 m_Cells
-> Layout(m_Width
- (l
+ r
));
205 // adjust indentation:
206 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
207 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
212 while (cell
!= NULL
) {
213 switch (m_AlignVer
) {
214 case HTML_ALIGN_TOP
: ybasicpos
= 0; break;
215 case HTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
216 case HTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
218 ydiff
= cell
-> GetHeight() + ybasicpos
;
220 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
221 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
223 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
224 xpos
+= cell
-> GetWidth();
225 cell
= cell
-> GetNext();
227 // force new line if occured:
228 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
229 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
230 if (ysizeup
< 0) ysizeup
= 0;
231 if (ysizedown
< 0) ysizedown
= 0;
232 switch (m_AlignHor
) {
233 case HTML_ALIGN_LEFT
: xdelta
= 0; break;
234 case HTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
235 case HTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
237 if (xdelta
< 0) xdelta
= 0;
241 while (line
!= cell
) {
242 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
243 line
= line
-> GetNext();
248 ysizeup
= ysizedown
= 0;
253 // setup height & width, depending on container layout:
254 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
256 if (m_Height
< m_MinHeight
) {
257 if (m_MinHeightAlign
!= HTML_ALIGN_TOP
) {
258 int diff
= m_MinHeight
- m_Height
;
259 if (m_MinHeightAlign
== HTML_ALIGN_CENTER
) diff
/= 2;
262 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
263 cell
= cell
-> GetNext();
266 m_Height
= m_MinHeight
;
269 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
270 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
272 wxHtmlCell::Layout(w
);
276 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
277 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
279 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
281 // container visible, draw it:
282 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
285 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
287 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
288 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
291 dc
.SetPen(*wxTRANSPARENT_PEN
);
292 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
296 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
297 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
300 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
301 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
303 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
304 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
307 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
309 // container invisible, just proceed font+color changing:
311 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
314 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
319 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
321 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
322 wxHtmlCell::DrawInvisible(dc
, x
, y
);
327 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
329 wxHtmlCell
*c
= m_Cells
;
333 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
334 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
335 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
336 return c
-> GetLink(x
- cx
, y
- cy
);
339 return wxEmptyString
;
344 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
346 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
348 m_LastCell
-> SetNext(f
);
350 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
352 f
-> SetParent(this);
357 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
359 if (tag
.HasParam("ALIGN")) {
360 wxString alg
= tag
.GetParam("ALIGN");
363 SetAlignHor(HTML_ALIGN_CENTER
);
364 else if (alg
== "LEFT")
365 SetAlignHor(HTML_ALIGN_LEFT
);
366 else if (alg
== "RIGHT")
367 SetAlignHor(HTML_ALIGN_RIGHT
);
373 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
375 if (tag
.HasParam("WIDTH")) {
377 wxString wd
= tag
.GetParam("WIDTH");
379 if (wd
[wd
.Length()-1] == '%') {
380 sscanf(wd
.c_str(), "%i%%", &wdi
);
381 SetWidthFloat(wdi
, HTML_UNITS_PERCENT
);
384 sscanf(wd
.c_str(), "%i", &wdi
);
385 SetWidthFloat(wdi
, HTML_UNITS_PIXELS
);
392 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
394 const wxHtmlCell
*r
= NULL
;
397 r
= m_Cells
-> Find(condition
, param
);
401 return wxHtmlCell::Find(condition
, param
);
406 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
409 wxHtmlCell
*c
= m_Cells
;
411 if ( (c
-> GetPosX() <= x
) &&
412 (c
-> GetPosY() <= y
) &&
413 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
414 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
415 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
427 //--------------------------------------------------------------------------------
429 //--------------------------------------------------------------------------------
431 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
433 if (m_Flags
& HTML_CLR_FOREGROUND
)
434 dc
.SetTextForeground(m_Colour
);
435 if (m_Flags
& HTML_CLR_BACKGROUND
) {
436 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
437 dc
.SetTextBackground(m_Colour
);
439 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
442 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
444 if (m_Flags
& HTML_CLR_FOREGROUND
)
445 dc
.SetTextForeground(m_Colour
);
446 if (m_Flags
& HTML_CLR_BACKGROUND
) {
447 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
448 dc
.SetTextBackground(m_Colour
);
450 wxHtmlCell::DrawInvisible(dc
, x
, y
);
456 //--------------------------------------------------------------------------------
458 //--------------------------------------------------------------------------------
460 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
463 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
466 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
469 wxHtmlCell::DrawInvisible(dc
, x
, y
);
479 //--------------------------------------------------------------------------------
481 //--------------------------------------------------------------------------------
483 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
487 m_Wnd
-> GetSize(&sx
, &sy
);
488 m_Width
= sx
, m_Height
= sy
;
493 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
495 int absx
= 0, absy
= 0, stx
, sty
;
496 wxHtmlCell
*c
= this;
499 absx
+= c
-> GetPosX();
500 absy
+= c
-> GetPosY();
501 c
= c
-> GetParent();
504 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
505 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
507 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
512 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
514 int absx
= 0, absy
= 0, stx
, sty
;
515 wxHtmlCell
*c
= this;
518 absx
+= c
-> GetPosX();
519 absy
+= c
-> GetPosY();
520 c
= c
-> GetParent();
523 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
524 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
526 wxHtmlCell::DrawInvisible(dc
, x
, y
);
531 void wxHtmlWidgetCell::Layout(int w
)
533 if (m_WidthFloat
!= 0) {
534 m_Width
= (w
* m_WidthFloat
) / 100;
535 m_Wnd
-> SetSize(m_Width
, m_Height
);
538 wxHtmlCell::Layout(w
);