]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmlcell.cpp
6979805f45fd302dc65e6dc5dad703716a108a75
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
= wxHTML_ALIGN_LEFT
;
105 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
106 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
107 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
108 m_UseBkColour
= FALSE
;
110 m_MinHeight
= m_MaxLineWidth
= 0;
111 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
116 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
118 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
119 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
120 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
121 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
122 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
127 int wxHtmlContainerCell::GetIndent(int ind
) const
129 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
130 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
131 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
132 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
133 else return -1; /* BUG! Should not be called... */
139 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
142 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
143 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
144 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
145 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
146 if (p
) return wxHTML_UNITS_PERCENT
;
147 else return wxHTML_UNITS_PIXELS
;
152 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
154 if (!m_CanLiveOnPagebreak
)
155 return wxHtmlCell::AdjustPagebreak(pagebreak
);
158 wxHtmlCell
*c
= GetFirstCell();
160 int pbrk
= *pagebreak
- m_PosY
;
163 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
166 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
173 void wxHtmlContainerCell::Layout(int w
)
175 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
176 long xpos
= 0, ypos
= m_IndentTop
;
177 int xdelta
= 0, ybasicpos
= 0, ydiff
;
178 int s_width
, s_indent
;
179 int ysizeup
= 0, ysizedown
= 0;
187 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
188 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
189 else m_Width
= m_WidthFloat
* w
/ 100;
192 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
193 else m_Width
= m_WidthFloat
;
197 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
198 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
199 m_Cells
-> Layout(m_Width
- (l
+ r
));
208 // adjust indentation:
209 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
210 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
215 while (cell
!= NULL
) {
216 switch (m_AlignVer
) {
217 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
218 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
219 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
221 ydiff
= cell
-> GetHeight() + ybasicpos
;
223 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
224 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
226 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
227 xpos
+= cell
-> GetWidth();
228 cell
= cell
-> GetNext();
230 // force new line if occured:
231 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
232 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
233 if (ysizeup
< 0) ysizeup
= 0;
234 if (ysizedown
< 0) ysizedown
= 0;
235 switch (m_AlignHor
) {
236 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
237 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
238 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
240 if (xdelta
< 0) xdelta
= 0;
244 while (line
!= cell
) {
245 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
246 line
= line
-> GetNext();
251 ysizeup
= ysizedown
= 0;
256 // setup height & width, depending on container layout:
257 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
259 if (m_Height
< m_MinHeight
) {
260 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
261 int diff
= m_MinHeight
- m_Height
;
262 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
265 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
266 cell
= cell
-> GetNext();
269 m_Height
= m_MinHeight
;
272 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
273 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
275 wxHtmlCell::Layout(w
);
279 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
280 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
282 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
284 // container visible, draw it:
285 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
288 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
290 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
291 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
294 dc
.SetPen(*wxTRANSPARENT_PEN
);
295 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
299 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
300 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
303 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
304 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
306 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
307 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
310 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
312 // container invisible, just proceed font+color changing:
314 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
317 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
322 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
324 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
325 wxHtmlCell::DrawInvisible(dc
, x
, y
);
330 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
332 wxHtmlCell
*c
= m_Cells
;
336 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
337 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
338 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
339 return c
-> GetLink(x
- cx
, y
- cy
);
342 return wxEmptyString
;
347 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
349 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
351 m_LastCell
-> SetNext(f
);
353 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
355 f
-> SetParent(this);
360 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
362 if (tag
.HasParam("ALIGN")) {
363 wxString alg
= tag
.GetParam("ALIGN");
366 SetAlignHor(wxHTML_ALIGN_CENTER
);
367 else if (alg
== "LEFT")
368 SetAlignHor(wxHTML_ALIGN_LEFT
);
369 else if (alg
== "RIGHT")
370 SetAlignHor(wxHTML_ALIGN_RIGHT
);
376 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
378 if (tag
.HasParam("WIDTH")) {
380 wxString wd
= tag
.GetParam("WIDTH");
382 if (wd
[wd
.Length()-1] == '%') {
383 sscanf(wd
.c_str(), "%i%%", &wdi
);
384 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
387 sscanf(wd
.c_str(), "%i", &wdi
);
388 SetWidthFloat(wdi
, wxHTML_UNITS_PIXELS
);
395 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
397 const wxHtmlCell
*r
= NULL
;
400 r
= m_Cells
-> Find(condition
, param
);
404 return wxHtmlCell::Find(condition
, param
);
409 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
412 wxHtmlCell
*c
= m_Cells
;
414 if ( (c
-> GetPosX() <= x
) &&
415 (c
-> GetPosY() <= y
) &&
416 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
417 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
418 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
430 //--------------------------------------------------------------------------------
432 //--------------------------------------------------------------------------------
434 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
436 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
437 dc
.SetTextForeground(m_Colour
);
438 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
439 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
440 dc
.SetTextBackground(m_Colour
);
442 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
445 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
447 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
448 dc
.SetTextForeground(m_Colour
);
449 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
450 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
451 dc
.SetTextBackground(m_Colour
);
453 wxHtmlCell::DrawInvisible(dc
, x
, y
);
459 //--------------------------------------------------------------------------------
461 //--------------------------------------------------------------------------------
463 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
466 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
469 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
472 wxHtmlCell::DrawInvisible(dc
, x
, y
);
482 //--------------------------------------------------------------------------------
484 //--------------------------------------------------------------------------------
486 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
490 m_Wnd
-> GetSize(&sx
, &sy
);
491 m_Width
= sx
, m_Height
= sy
;
496 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
498 int absx
= 0, absy
= 0, stx
, sty
;
499 wxHtmlCell
*c
= this;
502 absx
+= c
-> GetPosX();
503 absy
+= c
-> GetPosY();
504 c
= c
-> GetParent();
507 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
508 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
510 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
515 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
517 int absx
= 0, absy
= 0, stx
, sty
;
518 wxHtmlCell
*c
= this;
521 absx
+= c
-> GetPosX();
522 absy
+= c
-> GetPosY();
523 c
= c
-> GetParent();
526 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
527 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
529 wxHtmlCell::DrawInvisible(dc
, x
, y
);
534 void wxHtmlWidgetCell::Layout(int w
)
536 if (m_WidthFloat
!= 0) {
537 m_Width
= (w
* m_WidthFloat
) / 100;
538 m_Wnd
-> SetSize(m_Width
, m_Height
);
541 wxHtmlCell::Layout(w
);