]>
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 /////////////////////////////////////////////////////////////////////////////
10 #pragma implementation "htmlcell.h"
13 #include "wx/wxprec.h"
25 #include "wx/html/htmlcell.h"
26 #include "wx/html/htmlwin.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
35 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
37 wxString lnk
= GetLink(x
, y
);
38 if (lnk
!= wxEmptyString
)
39 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk
);
40 // note : this overcasting is legal because parent is *always* wxHtmlWindow
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
52 m_Word
.Replace(" ", " ", TRUE
);
53 m_Word
.Replace(""", "\"", TRUE
);
54 m_Word
.Replace("<", "<", TRUE
);
55 m_Word
.Replace(">", ">", TRUE
);
56 m_Word
.Replace("&", "&", TRUE
);
57 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
62 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
64 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
65 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
70 //-----------------------------------------------------------------------------
71 // wxHtmlContainerCell
72 //-----------------------------------------------------------------------------
75 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
77 m_Cells
= m_LastCell
= NULL
;
79 if (m_Parent
) m_Parent
-> InsertCell(this);
80 m_AlignHor
= HTML_ALIGN_LEFT
;
81 m_AlignVer
= HTML_ALIGN_BOTTOM
;
82 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
83 m_WidthFloat
= 100; m_WidthFloatUnits
= HTML_UNITS_PERCENT
;
84 m_UseBkColour
= FALSE
;
86 m_MinHeight
= m_MaxLineWidth
= 0;
87 m_MinHeightAlign
= HTML_ALIGN_TOP
;
92 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
94 int val
= (units
== HTML_UNITS_PIXELS
) ? i
: -i
;
95 if (what
& HTML_INDENT_LEFT
) m_IndentLeft
= val
;
96 if (what
& HTML_INDENT_RIGHT
) m_IndentRight
= val
;
97 if (what
& HTML_INDENT_TOP
) m_IndentTop
= val
;
98 if (what
& HTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
103 int wxHtmlContainerCell::GetIndent(int ind
) const
105 if (ind
& HTML_INDENT_LEFT
) return m_IndentLeft
;
106 else if (ind
& HTML_INDENT_RIGHT
) return m_IndentRight
;
107 else if (ind
& HTML_INDENT_TOP
) return m_IndentTop
;
108 else if (ind
& HTML_INDENT_BOTTOM
) return m_IndentBottom
;
109 else return -1; /* BUG! Should not be called... */
115 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
118 if (ind
& HTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
119 else if (ind
& HTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
120 else if (ind
& HTML_INDENT_TOP
) p
= m_IndentTop
< 0;
121 else if (ind
& HTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
122 if (p
) return HTML_UNITS_PERCENT
;
123 else return HTML_UNITS_PIXELS
;
128 void wxHtmlContainerCell::Layout(int w
)
130 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
131 long xpos
= 0, ypos
= m_IndentTop
;
132 int xdelta
= 0, ybasicpos
= 0, ydiff
;
133 int s_width
, s_indent
;
134 int ysizeup
= 0, ysizedown
= 0;
142 if (m_WidthFloatUnits
== HTML_UNITS_PERCENT
) {
143 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
144 else m_Width
= m_WidthFloat
* w
/ 100;
147 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
148 else m_Width
= m_WidthFloat
;
152 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
153 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
154 m_Cells
-> Layout(m_Width
- (l
+ r
));
163 // adjust indentation:
164 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
165 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
170 while (cell
!= NULL
) {
171 switch (m_AlignVer
) {
172 case HTML_ALIGN_TOP
: ybasicpos
= 0; break;
173 case HTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
174 case HTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
176 ydiff
= cell
-> GetHeight() + ybasicpos
;
178 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
179 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
181 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
182 xpos
+= cell
-> GetWidth();
183 cell
= cell
-> GetNext();
185 // force new line if occured:
186 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
187 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
188 if (ysizeup
< 0) ysizeup
= 0;
189 if (ysizedown
< 0) ysizedown
= 0;
190 switch (m_AlignHor
) {
191 case HTML_ALIGN_LEFT
: xdelta
= 0; break;
192 case HTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
193 case HTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
195 if (xdelta
< 0) xdelta
= 0;
199 while (line
!= cell
) {
200 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
201 line
= line
-> GetNext();
206 ysizeup
= ysizedown
= 0;
211 // setup height & width, depending on container layout:
212 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
214 if (m_Height
< m_MinHeight
) {
215 if (m_MinHeightAlign
!= HTML_ALIGN_TOP
) {
216 int diff
= m_MinHeight
- m_Height
;
217 if (m_MinHeightAlign
== HTML_ALIGN_CENTER
) diff
/= 2;
220 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
221 cell
= cell
-> GetNext();
224 m_Height
= m_MinHeight
;
227 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
228 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
230 wxHtmlCell::Layout(w
);
234 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
235 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
237 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
239 // container visible, draw it:
240 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
243 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
245 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
246 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
249 dc
.SetPen(*wxTRANSPARENT_PEN
);
250 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
254 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
255 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
258 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
259 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
261 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
262 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
265 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
267 // container invisible, just proceed font+color changing:
269 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
272 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
277 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
279 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
280 wxHtmlCell::DrawInvisible(dc
, x
, y
);
285 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
287 wxHtmlCell
*c
= m_Cells
;
291 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
292 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
293 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
294 return c
-> GetLink(x
- cx
, y
- cy
);
297 return wxEmptyString
;
302 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
304 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
306 m_LastCell
-> SetNext(f
);
308 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
310 f
-> SetParent(this);
315 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
317 if (tag
.HasParam("ALIGN")) {
318 wxString alg
= tag
.GetParam("ALIGN");
321 SetAlignHor(HTML_ALIGN_CENTER
);
322 else if (alg
== "LEFT")
323 SetAlignHor(HTML_ALIGN_LEFT
);
324 else if (alg
== "RIGHT")
325 SetAlignHor(HTML_ALIGN_RIGHT
);
331 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
333 if (tag
.HasParam("WIDTH")) {
335 wxString wd
= tag
.GetParam("WIDTH");
337 if (wd
[wd
.Length()-1] == '%') {
338 sscanf(wd
.c_str(), "%i%%", &wdi
);
339 SetWidthFloat(wdi
, HTML_UNITS_PERCENT
);
342 sscanf(wd
.c_str(), "%i", &wdi
);
343 SetWidthFloat(wdi
, HTML_UNITS_PIXELS
);
350 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
352 const wxHtmlCell
*r
= NULL
;
355 r
= m_Cells
-> Find(condition
, param
);
359 return wxHtmlCell::Find(condition
, param
);
364 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
367 wxHtmlCell
*c
= m_Cells
;
369 if ( (c
-> GetPosX() <= x
) &&
370 (c
-> GetPosY() <= y
) &&
371 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
372 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
373 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
385 //--------------------------------------------------------------------------------
387 //--------------------------------------------------------------------------------
389 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
391 if (m_Flags
& HTML_CLR_FOREGROUND
)
392 dc
.SetTextForeground(m_Colour
);
393 if (m_Flags
& HTML_CLR_BACKGROUND
) {
394 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
395 dc
.SetTextBackground(m_Colour
);
397 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
400 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
402 if (m_Flags
& HTML_CLR_FOREGROUND
)
403 dc
.SetTextForeground(m_Colour
);
404 if (m_Flags
& HTML_CLR_BACKGROUND
) {
405 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
406 dc
.SetTextBackground(m_Colour
);
408 wxHtmlCell::DrawInvisible(dc
, x
, y
);
414 //--------------------------------------------------------------------------------
416 //--------------------------------------------------------------------------------
418 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
421 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
424 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
427 wxHtmlCell::DrawInvisible(dc
, x
, y
);
437 //--------------------------------------------------------------------------------
439 //--------------------------------------------------------------------------------
441 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
445 m_Wnd
-> GetSize(&sx
, &sy
);
446 m_Width
= sx
, m_Height
= sy
;
451 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
453 int absx
= 0, absy
= 0, stx
, sty
;
454 wxHtmlCell
*c
= this;
457 absx
+= c
-> GetPosX();
458 absy
+= c
-> GetPosY();
459 c
= c
-> GetParent();
462 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
464 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
465 // m_Wnd -> Refresh();
467 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
472 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
474 int absx
= 0, absy
= 0, stx
, sty
;
475 wxHtmlCell
*c
= this;
478 absx
+= c
-> GetPosX();
479 absy
+= c
-> GetPosY();
480 c
= c
-> GetParent();
482 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
484 m_Wnd
-> SetSize(absx
- HTML_SCROLL_STEP
* stx
, absy
- HTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
485 wxHtmlCell::DrawInvisible(dc
, x
, y
);
490 void wxHtmlWidgetCell::Layout(int w
)
492 if (m_WidthFloat
!= 0) {
493 m_Width
= (w
* m_WidthFloat
) / 100;
494 m_Wnd
-> SetSize(m_Width
, m_Height
);
497 wxHtmlCell::Layout(w
);