]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlCell - basic element of HTML output
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include <wx/wxprec.h>
27 #include <wx/html/htmlcell.h>
28 #include <wx/html/htmlwin.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
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 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
56 m_Word
.Replace(" ", " ", TRUE
);
57 m_Word
.Replace(""", "\"", TRUE
);
58 m_Word
.Replace("<", "<", TRUE
);
59 m_Word
.Replace(">", ">", TRUE
);
60 m_Word
.Replace("&", "&", TRUE
);
61 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
66 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
68 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
69 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
78 //-----------------------------------------------------------------------------
79 // wxHtmlContainerCell
80 //-----------------------------------------------------------------------------
83 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
85 m_Cells
= m_LastCell
= NULL
;
87 if (m_Parent
) m_Parent
-> InsertCell(this);
88 m_AlignHor
= HTML_ALIGN_LEFT
;
89 m_AlignVer
= HTML_ALIGN_BOTTOM
;
90 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
91 m_WidthFloat
= 100; m_WidthFloatUnits
= HTML_UNITS_PERCENT
;
92 m_UseBkColour
= FALSE
;
94 m_MinHeight
= m_MaxLineWidth
= 0;
95 m_MinHeightAlign
= HTML_ALIGN_TOP
;
100 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
102 int val
= (units
== HTML_UNITS_PIXELS
) ? i
: -i
;
103 if (what
& HTML_INDENT_LEFT
) m_IndentLeft
= val
;
104 if (what
& HTML_INDENT_RIGHT
) m_IndentRight
= val
;
105 if (what
& HTML_INDENT_TOP
) m_IndentTop
= val
;
106 if (what
& HTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
111 int wxHtmlContainerCell::GetIndent(int ind
) const
113 if (ind
& HTML_INDENT_LEFT
) return m_IndentLeft
;
114 else if (ind
& HTML_INDENT_RIGHT
) return m_IndentRight
;
115 else if (ind
& HTML_INDENT_TOP
) return m_IndentTop
;
116 else if (ind
& HTML_INDENT_BOTTOM
) return m_IndentBottom
;
117 else return -1; /* BUG! Should not be called... */
123 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
126 if (ind
& HTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
127 else if (ind
& HTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
128 else if (ind
& HTML_INDENT_TOP
) p
= m_IndentTop
< 0;
129 else if (ind
& HTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
130 if (p
) return HTML_UNITS_PERCENT
;
131 else return HTML_UNITS_PIXELS
;
136 void wxHtmlContainerCell::Layout(int w
)
138 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
139 long xpos
= 0, ypos
= m_IndentTop
;
140 int xdelta
= 0, ybasicpos
= 0, ydiff
;
141 int s_width
, s_indent
;
142 int ysizeup
= 0, ysizedown
= 0;
150 if (m_WidthFloatUnits
== HTML_UNITS_PERCENT
) {
151 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
152 else m_Width
= m_WidthFloat
* w
/ 100;
155 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
156 else m_Width
= m_WidthFloat
;
160 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
161 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
162 m_Cells
-> Layout(m_Width
- (l
+ r
));
171 // adjust indentation:
172 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
173 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
178 while (cell
!= NULL
) {
179 switch (m_AlignVer
) {
180 case HTML_ALIGN_TOP
: ybasicpos
= 0; break;
181 case HTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
182 case HTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
184 ydiff
= cell
-> GetHeight() + ybasicpos
;
186 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
187 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
189 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
190 xpos
+= cell
-> GetWidth();
191 cell
= cell
-> GetNext();
193 // force new line if occured:
194 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
195 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
196 if (ysizeup
< 0) ysizeup
= 0;
197 if (ysizedown
< 0) ysizedown
= 0;
198 switch (m_AlignHor
) {
199 case HTML_ALIGN_LEFT
: xdelta
= 0; break;
200 case HTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
201 case HTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
203 if (xdelta
< 0) xdelta
= 0;
207 while (line
!= cell
) {
208 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
209 line
= line
-> GetNext();
214 ysizeup
= ysizedown
= 0;
219 // setup height & width, depending on container layout:
220 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
222 if (m_Height
< m_MinHeight
) {
223 if (m_MinHeightAlign
!= HTML_ALIGN_TOP
) {
224 int diff
= m_MinHeight
- m_Height
;
225 if (m_MinHeightAlign
== HTML_ALIGN_CENTER
) diff
/= 2;
228 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
229 cell
= cell
-> GetNext();
232 m_Height
= m_MinHeight
;
235 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
236 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
238 wxHtmlCell::Layout(w
);
242 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
243 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
245 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
247 // container visible, draw it:
248 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
251 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
253 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
254 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
257 dc
.SetPen(*wxTRANSPARENT_PEN
);
258 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
262 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
263 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
266 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
267 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
269 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
270 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
273 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
275 // container invisible, just proceed font+color changing:
277 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
280 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
285 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
287 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
288 wxHtmlCell::DrawInvisible(dc
, x
, y
);
293 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
295 wxHtmlCell
*c
= m_Cells
;
299 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
300 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
301 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
302 return c
-> GetLink(x
- cx
, y
- cy
);
305 return wxEmptyString
;
310 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
312 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
314 m_LastCell
-> SetNext(f
);
316 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
318 f
-> SetParent(this);
323 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
325 if (tag
.HasParam("ALIGN")) {
326 wxString alg
= tag
.GetParam("ALIGN");
329 SetAlignHor(HTML_ALIGN_CENTER
);
330 else if (alg
== "LEFT")
331 SetAlignHor(HTML_ALIGN_LEFT
);
332 else if (alg
== "RIGHT")
333 SetAlignHor(HTML_ALIGN_RIGHT
);
339 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
341 if (tag
.HasParam("WIDTH")) {
343 wxString wd
= tag
.GetParam("WIDTH");
345 if (wd
[wd
.Length()-1] == '%') {
346 sscanf(wd
.c_str(), "%i%%", &wdi
);
347 SetWidthFloat(wdi
, HTML_UNITS_PERCENT
);
350 sscanf(wd
.c_str(), "%i", &wdi
);
351 SetWidthFloat(wdi
, HTML_UNITS_PIXELS
);
358 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
360 const wxHtmlCell
*r
= NULL
;
363 r
= m_Cells
-> Find(condition
, param
);
367 return wxHtmlCell::Find(condition
, param
);
372 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
375 wxHtmlCell
*c
= m_Cells
;
377 if ( (c
-> GetPosX() <= x
) &&
378 (c
-> GetPosY() <= y
) &&
379 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
380 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
381 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
393 //--------------------------------------------------------------------------------
395 //--------------------------------------------------------------------------------
397 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
399 if (m_Flags
& HTML_CLR_FOREGROUND
)
400 dc
.SetTextForeground(m_Colour
);
401 if (m_Flags
& HTML_CLR_BACKGROUND
) {
402 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
403 dc
.SetTextBackground(m_Colour
);
405 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
408 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
410 if (m_Flags
& HTML_CLR_FOREGROUND
)
411 dc
.SetTextForeground(m_Colour
);
412 if (m_Flags
& HTML_CLR_BACKGROUND
) {
413 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
414 dc
.SetTextBackground(m_Colour
);
416 wxHtmlCell::DrawInvisible(dc
, x
, y
);
422 //--------------------------------------------------------------------------------
424 //--------------------------------------------------------------------------------
426 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
429 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
432 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
435 wxHtmlCell::DrawInvisible(dc
, x
, y
);
445 //--------------------------------------------------------------------------------
447 //--------------------------------------------------------------------------------
449 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
453 m_Wnd
-> GetSize(&sx
, &sy
);
454 m_Width
= sx
, m_Height
= sy
;
459 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
461 int absx
= 0, absy
= 0, stx
, sty
;
462 wxHtmlCell
*c
= this;
465 absx
+= c
-> GetPosX();
466 absy
+= c
-> GetPosY();
467 c
= c
-> GetParent();
470 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
472 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
473 // m_Wnd -> Refresh();
475 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
480 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
482 int absx
= 0, absy
= 0, stx
, sty
;
483 wxHtmlCell
*c
= this;
486 absx
+= c
-> GetPosX();
487 absy
+= c
-> GetPosY();
488 c
= c
-> GetParent();
490 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
492 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
493 wxHtmlCell::DrawInvisible(dc
, x
, y
);
498 void wxHtmlWidgetCell::Layout(int w
)
500 if (m_WidthFloat
!= 0) {
501 m_Width
= (w
* m_WidthFloat
) / 100;
502 m_Wnd
-> SetSize(m_Width
, m_Height
);
505 wxHtmlCell::Layout(w
);