]>
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("(c)"), TRUE
);
79 m_Word
.Replace(wxT("""), wxT("\""), TRUE
);
80 m_Word
.Replace(wxT("<"), wxT("<"), TRUE
);
81 m_Word
.Replace(wxT(">"), wxT(">"), TRUE
);
82 m_Word
.Replace(wxT("&"), wxT("&"), TRUE
);
84 m_Word
.Replace(wxT("  "), wxT(" "), TRUE
);
85 m_Word
.Replace(wxT("© "), wxT("(c)"), TRUE
);
86 m_Word
.Replace(wxT("" "), wxT("\""), TRUE
);
87 m_Word
.Replace(wxT("< "), wxT("<"), TRUE
);
88 m_Word
.Replace(wxT("> "), wxT(">"), TRUE
);
89 m_Word
.Replace(wxT("& "), wxT("&"), TRUE
);
91 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
92 SetCanLiveOnPagebreak(FALSE
);
97 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
99 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
100 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
105 //-----------------------------------------------------------------------------
106 // wxHtmlContainerCell
107 //-----------------------------------------------------------------------------
110 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
112 m_Cells
= m_LastCell
= NULL
;
114 if (m_Parent
) m_Parent
-> InsertCell(this);
115 m_AlignHor
= wxHTML_ALIGN_LEFT
;
116 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
117 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
118 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
119 m_UseBkColour
= FALSE
;
121 m_MinHeight
= m_MaxLineWidth
= 0;
122 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
127 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
129 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
130 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
131 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
132 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
133 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
138 int wxHtmlContainerCell::GetIndent(int ind
) const
140 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
141 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
142 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
143 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
144 else return -1; /* BUG! Should not be called... */
150 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
153 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
154 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
155 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
156 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
157 if (p
) return wxHTML_UNITS_PERCENT
;
158 else return wxHTML_UNITS_PIXELS
;
163 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
165 if (!m_CanLiveOnPagebreak
)
166 return wxHtmlCell::AdjustPagebreak(pagebreak
);
169 wxHtmlCell
*c
= GetFirstCell();
171 int pbrk
= *pagebreak
- m_PosY
;
174 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
177 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
184 void wxHtmlContainerCell::Layout(int w
)
186 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
187 long xpos
= 0, ypos
= m_IndentTop
;
188 int xdelta
= 0, ybasicpos
= 0, ydiff
;
189 int s_width
, s_indent
;
190 int ysizeup
= 0, ysizedown
= 0;
198 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
199 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
200 else m_Width
= m_WidthFloat
* w
/ 100;
203 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
204 else m_Width
= m_WidthFloat
;
208 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
209 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
210 m_Cells
-> Layout(m_Width
- (l
+ r
));
219 // adjust indentation:
220 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
221 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
226 while (cell
!= NULL
) {
227 switch (m_AlignVer
) {
228 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
229 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
230 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
232 ydiff
= cell
-> GetHeight() + ybasicpos
;
234 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
235 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
237 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
238 xpos
+= cell
-> GetWidth();
239 cell
= cell
-> GetNext();
241 // force new line if occured:
242 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
243 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
244 if (ysizeup
< 0) ysizeup
= 0;
245 if (ysizedown
< 0) ysizedown
= 0;
246 switch (m_AlignHor
) {
247 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
248 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
249 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
251 if (xdelta
< 0) xdelta
= 0;
255 while (line
!= cell
) {
256 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
257 line
= line
-> GetNext();
262 ysizeup
= ysizedown
= 0;
267 // setup height & width, depending on container layout:
268 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
270 if (m_Height
< m_MinHeight
) {
271 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
272 int diff
= m_MinHeight
- m_Height
;
273 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
276 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
277 cell
= cell
-> GetNext();
280 m_Height
= m_MinHeight
;
283 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
284 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
286 wxHtmlCell::Layout(w
);
290 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
291 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
293 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
295 // container visible, draw it:
296 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
299 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
301 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
302 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
305 dc
.SetPen(*wxTRANSPARENT_PEN
);
306 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
310 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
311 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
314 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
315 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
317 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
318 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
321 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
323 // container invisible, just proceed font+color changing:
325 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
328 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
333 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
335 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
336 wxHtmlCell::DrawInvisible(dc
, x
, y
);
341 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
343 wxHtmlCell
*c
= m_Cells
;
347 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
348 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
349 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
350 return c
-> GetLink(x
- cx
, y
- cy
);
353 return wxEmptyString
;
358 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
360 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
362 m_LastCell
-> SetNext(f
);
364 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
366 f
-> SetParent(this);
371 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
373 if (tag
.HasParam("ALIGN")) {
374 wxString alg
= tag
.GetParam("ALIGN");
377 SetAlignHor(wxHTML_ALIGN_CENTER
);
378 else if (alg
== "LEFT")
379 SetAlignHor(wxHTML_ALIGN_LEFT
);
380 else if (alg
== "RIGHT")
381 SetAlignHor(wxHTML_ALIGN_RIGHT
);
387 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
389 if (tag
.HasParam("WIDTH")) {
391 wxString wd
= tag
.GetParam("WIDTH");
393 if (wd
[wd
.Length()-1] == '%') {
394 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
395 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
398 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
399 SetWidthFloat(wdi
, wxHTML_UNITS_PIXELS
);
406 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
408 const wxHtmlCell
*r
= NULL
;
411 r
= m_Cells
-> Find(condition
, param
);
415 return wxHtmlCell::Find(condition
, param
);
420 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
423 wxHtmlCell
*c
= m_Cells
;
425 if ( (c
-> GetPosX() <= x
) &&
426 (c
-> GetPosY() <= y
) &&
427 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
428 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
429 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
441 //--------------------------------------------------------------------------------
443 //--------------------------------------------------------------------------------
445 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
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::Draw(dc
, x
, y
, view_y1
, view_y2
);
456 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
458 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
459 dc
.SetTextForeground(m_Colour
);
460 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
461 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
462 dc
.SetTextBackground(m_Colour
);
464 wxHtmlCell::DrawInvisible(dc
, x
, y
);
470 //--------------------------------------------------------------------------------
472 //--------------------------------------------------------------------------------
474 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
477 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
480 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
483 wxHtmlCell::DrawInvisible(dc
, x
, y
);
493 //--------------------------------------------------------------------------------
495 //--------------------------------------------------------------------------------
497 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
501 m_Wnd
-> GetSize(&sx
, &sy
);
502 m_Width
= sx
, m_Height
= sy
;
507 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
509 int absx
= 0, absy
= 0, stx
, sty
;
510 wxHtmlCell
*c
= this;
513 absx
+= c
-> GetPosX();
514 absy
+= c
-> GetPosY();
515 c
= c
-> GetParent();
518 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
519 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
521 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
526 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
528 int absx
= 0, absy
= 0, stx
, sty
;
529 wxHtmlCell
*c
= this;
532 absx
+= c
-> GetPosX();
533 absy
+= c
-> GetPosY();
534 c
= c
-> GetParent();
537 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
538 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
540 wxHtmlCell::DrawInvisible(dc
, x
, y
);
545 void wxHtmlWidgetCell::Layout(int w
)
547 if (m_WidthFloat
!= 0) {
548 m_Width
= (w
* m_WidthFloat
) / 100;
549 m_Wnd
-> SetSize(m_Width
, m_Height
);
552 wxHtmlCell::Layout(w
);