]>
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 //-----------------------------------------------------------------------------
36 wxHtmlCell::wxHtmlCell() : wxObject()
40 m_Width
= m_Height
= m_Descent
= 0;
41 m_CanLiveOnPagebreak
= TRUE
;
45 wxHtmlCell::~wxHtmlCell()
47 if (m_Link
) delete m_Link
;
48 if (m_Next
) delete m_Next
;
52 void wxHtmlCell::OnMouseClick(wxWindow
*parent
, int x
, int y
,
54 bool WXUNUSED(middle
),
57 wxHtmlLinkInfo
*lnk
= GetLink(x
, y
);
59 ((wxHtmlWindow
*)parent
) -> OnLinkClicked(lnk
);
60 // note : this overcasting is legal because parent is *always* wxHtmlWindow
65 bool wxHtmlCell::AdjustPagebreak(int *pagebreak
)
67 if ((!m_CanLiveOnPagebreak
) &&
68 m_PosY
< *pagebreak
&& m_PosY
+ m_Height
> *pagebreak
) {
70 if (m_Next
!= NULL
) m_Next
-> AdjustPagebreak(pagebreak
);
75 if (m_Next
!= NULL
) return m_Next
-> AdjustPagebreak(pagebreak
);
82 void wxHtmlCell::SetLink(const wxHtmlLinkInfo
& link
)
84 if (m_Link
) delete m_Link
;
85 m_Link
= new wxHtmlLinkInfo(link
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 wxHtmlWordCell::wxHtmlWordCell(const wxString
& word
, wxDC
& dc
) : wxHtmlCell()
98 if (m_Word
.Find(wxT('&')) != -1)
100 static wxChar
* substitutions
[][3] =
102 { wxT(" "), wxT("  "), wxT(" ") },
103 { wxT("©"), wxT("© "), wxT("(c)") },
104 { wxT("""), wxT("" "), wxT("\"") },
105 { wxT("<"), wxT("< "), wxT("<") },
106 { wxT(">"), wxT("> "), wxT(">") },
107 { wxT("&"), wxT("& "), wxT("&") /*this one should be last one*/ },
111 for (int i
= 0; substitutions
[i
][0] != NULL
; i
++)
113 m_Word
.Replace(substitutions
[i
][0], substitutions
[i
][2], TRUE
);
114 m_Word
.Replace(substitutions
[i
][1], substitutions
[i
][2], TRUE
);
118 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
119 SetCanLiveOnPagebreak(FALSE
);
124 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
126 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
127 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
132 //-----------------------------------------------------------------------------
133 // wxHtmlContainerCell
134 //-----------------------------------------------------------------------------
137 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
139 m_Cells
= m_LastCell
= NULL
;
141 if (m_Parent
) m_Parent
-> InsertCell(this);
142 m_AlignHor
= wxHTML_ALIGN_LEFT
;
143 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
144 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
145 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
146 m_UseBkColour
= FALSE
;
148 m_MinHeight
= m_MaxLineWidth
= 0;
149 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
154 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
156 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
157 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
158 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
159 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
160 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
165 int wxHtmlContainerCell::GetIndent(int ind
) const
167 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
168 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
169 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
170 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
171 else return -1; /* BUG! Should not be called... */
177 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
180 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
181 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
182 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
183 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
184 if (p
) return wxHTML_UNITS_PERCENT
;
185 else return wxHTML_UNITS_PIXELS
;
190 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
192 if (!m_CanLiveOnPagebreak
)
193 return wxHtmlCell::AdjustPagebreak(pagebreak
);
196 wxHtmlCell
*c
= GetFirstCell();
198 int pbrk
= *pagebreak
- m_PosY
;
201 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
204 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
211 void wxHtmlContainerCell::Layout(int w
)
213 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
214 long xpos
= 0, ypos
= m_IndentTop
;
215 int xdelta
= 0, ybasicpos
= 0, ydiff
;
216 int s_width
, s_indent
;
217 int ysizeup
= 0, ysizedown
= 0;
225 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
226 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
227 else m_Width
= m_WidthFloat
* w
/ 100;
230 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
231 else m_Width
= m_WidthFloat
;
235 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
236 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
237 m_Cells
-> Layout(m_Width
- (l
+ r
));
246 // adjust indentation:
247 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
248 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
253 while (cell
!= NULL
) {
254 switch (m_AlignVer
) {
255 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
256 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
257 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
259 ydiff
= cell
-> GetHeight() + ybasicpos
;
261 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
262 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
264 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
265 xpos
+= cell
-> GetWidth();
266 cell
= cell
-> GetNext();
268 // force new line if occured:
269 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
270 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
271 if (ysizeup
< 0) ysizeup
= 0;
272 if (ysizedown
< 0) ysizedown
= 0;
273 switch (m_AlignHor
) {
274 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
275 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
276 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
278 if (xdelta
< 0) xdelta
= 0;
282 while (line
!= cell
) {
283 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
284 line
= line
-> GetNext();
289 ysizeup
= ysizedown
= 0;
294 // setup height & width, depending on container layout:
295 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
297 if (m_Height
< m_MinHeight
) {
298 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
299 int diff
= m_MinHeight
- m_Height
;
300 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
303 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
304 cell
= cell
-> GetNext();
307 m_Height
= m_MinHeight
;
310 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
311 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
313 wxHtmlCell::Layout(w
);
317 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
318 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
320 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
322 // container visible, draw it:
323 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
326 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
328 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
329 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
332 dc
.SetPen(*wxTRANSPARENT_PEN
);
333 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
337 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
338 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
341 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
342 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
344 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
345 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
348 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
350 // container invisible, just proceed font+color changing:
352 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
355 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
360 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
362 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
363 wxHtmlCell::DrawInvisible(dc
, x
, y
);
368 wxHtmlLinkInfo
*wxHtmlContainerCell::GetLink(int x
, int y
) const
370 wxHtmlCell
*c
= m_Cells
;
374 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
375 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
376 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
377 return c
-> GetLink(x
- cx
, y
- cy
);
385 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
387 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
389 m_LastCell
-> SetNext(f
);
391 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
393 f
-> SetParent(this);
398 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
400 if (tag
.HasParam(wxT("ALIGN"))) {
401 wxString alg
= tag
.GetParam(wxT("ALIGN"));
403 if (alg
== wxT("CENTER"))
404 SetAlignHor(wxHTML_ALIGN_CENTER
);
405 else if (alg
== wxT("LEFT"))
406 SetAlignHor(wxHTML_ALIGN_LEFT
);
407 else if (alg
== wxT("RIGHT"))
408 SetAlignHor(wxHTML_ALIGN_RIGHT
);
414 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
, double pixel_scale
)
416 if (tag
.HasParam(wxT("WIDTH"))) {
418 wxString wd
= tag
.GetParam(wxT("WIDTH"));
420 if (wd
[wd
.Length()-1] == wxT('%')) {
421 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
422 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
425 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
426 SetWidthFloat((int)(pixel_scale
* (double)wdi
), wxHTML_UNITS_PIXELS
);
433 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
435 const wxHtmlCell
*r
= NULL
;
438 r
= m_Cells
-> Find(condition
, param
);
442 return wxHtmlCell::Find(condition
, param
);
447 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
450 wxHtmlCell
*c
= m_Cells
;
452 if ( (c
-> GetPosX() <= x
) &&
453 (c
-> GetPosY() <= y
) &&
454 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
455 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
456 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
468 //--------------------------------------------------------------------------------
470 //--------------------------------------------------------------------------------
472 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
474 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
475 dc
.SetTextForeground(m_Colour
);
476 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
477 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
478 dc
.SetTextBackground(m_Colour
);
480 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
483 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
485 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
486 dc
.SetTextForeground(m_Colour
);
487 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
488 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
489 dc
.SetTextBackground(m_Colour
);
491 wxHtmlCell::DrawInvisible(dc
, x
, y
);
497 //--------------------------------------------------------------------------------
499 //--------------------------------------------------------------------------------
501 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
504 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
507 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
510 wxHtmlCell::DrawInvisible(dc
, x
, y
);
520 //--------------------------------------------------------------------------------
522 //--------------------------------------------------------------------------------
524 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
528 m_Wnd
-> GetSize(&sx
, &sy
);
529 m_Width
= sx
, m_Height
= sy
;
534 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
536 int absx
= 0, absy
= 0, stx
, sty
;
537 wxHtmlCell
*c
= this;
540 absx
+= c
-> GetPosX();
541 absy
+= c
-> GetPosY();
542 c
= c
-> GetParent();
545 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
546 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
548 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
553 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
555 int absx
= 0, absy
= 0, stx
, sty
;
556 wxHtmlCell
*c
= this;
559 absx
+= c
-> GetPosX();
560 absy
+= c
-> GetPosY();
561 c
= c
-> GetParent();
564 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
565 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
567 wxHtmlCell::DrawInvisible(dc
, x
, y
);
572 void wxHtmlWidgetCell::Layout(int w
)
574 if (m_WidthFloat
!= 0) {
575 m_Width
= (w
* m_WidthFloat
) / 100;
576 m_Wnd
-> SetSize(m_Width
, m_Height
);
579 wxHtmlCell::Layout(w
);