]>
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()
76 m_Word
.Replace(wxT(" "), wxT(" "), TRUE
);
77 m_Word
.Replace(wxT("""), wxT("\""), TRUE
);
78 m_Word
.Replace(wxT("<"), wxT("<"), TRUE
);
79 m_Word
.Replace(wxT(">"), wxT(">"), TRUE
);
80 m_Word
.Replace(wxT("&"), wxT("&"), TRUE
);
81 dc
.GetTextExtent(m_Word
, &m_Width
, &m_Height
, &m_Descent
);
82 SetCanLiveOnPagebreak(FALSE
);
87 void wxHtmlWordCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
89 dc
.DrawText(m_Word
, x
+ m_PosX
, y
+ m_PosY
);
90 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
95 //-----------------------------------------------------------------------------
96 // wxHtmlContainerCell
97 //-----------------------------------------------------------------------------
100 wxHtmlContainerCell::wxHtmlContainerCell(wxHtmlContainerCell
*parent
) : wxHtmlCell()
102 m_Cells
= m_LastCell
= NULL
;
104 if (m_Parent
) m_Parent
-> InsertCell(this);
105 m_AlignHor
= wxHTML_ALIGN_LEFT
;
106 m_AlignVer
= wxHTML_ALIGN_BOTTOM
;
107 m_IndentLeft
= m_IndentRight
= m_IndentTop
= m_IndentBottom
= 0;
108 m_WidthFloat
= 100; m_WidthFloatUnits
= wxHTML_UNITS_PERCENT
;
109 m_UseBkColour
= FALSE
;
111 m_MinHeight
= m_MaxLineWidth
= 0;
112 m_MinHeightAlign
= wxHTML_ALIGN_TOP
;
117 void wxHtmlContainerCell::SetIndent(int i
, int what
, int units
)
119 int val
= (units
== wxHTML_UNITS_PIXELS
) ? i
: -i
;
120 if (what
& wxHTML_INDENT_LEFT
) m_IndentLeft
= val
;
121 if (what
& wxHTML_INDENT_RIGHT
) m_IndentRight
= val
;
122 if (what
& wxHTML_INDENT_TOP
) m_IndentTop
= val
;
123 if (what
& wxHTML_INDENT_BOTTOM
) m_IndentBottom
= val
;
128 int wxHtmlContainerCell::GetIndent(int ind
) const
130 if (ind
& wxHTML_INDENT_LEFT
) return m_IndentLeft
;
131 else if (ind
& wxHTML_INDENT_RIGHT
) return m_IndentRight
;
132 else if (ind
& wxHTML_INDENT_TOP
) return m_IndentTop
;
133 else if (ind
& wxHTML_INDENT_BOTTOM
) return m_IndentBottom
;
134 else return -1; /* BUG! Should not be called... */
140 int wxHtmlContainerCell::GetIndentUnits(int ind
) const
143 if (ind
& wxHTML_INDENT_LEFT
) p
= m_IndentLeft
< 0;
144 else if (ind
& wxHTML_INDENT_RIGHT
) p
= m_IndentRight
< 0;
145 else if (ind
& wxHTML_INDENT_TOP
) p
= m_IndentTop
< 0;
146 else if (ind
& wxHTML_INDENT_BOTTOM
) p
= m_IndentBottom
< 0;
147 if (p
) return wxHTML_UNITS_PERCENT
;
148 else return wxHTML_UNITS_PIXELS
;
153 bool wxHtmlContainerCell::AdjustPagebreak(int *pagebreak
)
155 if (!m_CanLiveOnPagebreak
)
156 return wxHtmlCell::AdjustPagebreak(pagebreak
);
159 wxHtmlCell
*c
= GetFirstCell();
161 int pbrk
= *pagebreak
- m_PosY
;
164 if (c
-> AdjustPagebreak(&pbrk
)) rt
= TRUE
;
167 if (rt
) *pagebreak
= pbrk
+ m_PosY
;
174 void wxHtmlContainerCell::Layout(int w
)
176 wxHtmlCell
*cell
= m_Cells
, *line
= m_Cells
;
177 long xpos
= 0, ypos
= m_IndentTop
;
178 int xdelta
= 0, ybasicpos
= 0, ydiff
;
179 int s_width
, s_indent
;
180 int ysizeup
= 0, ysizedown
= 0;
188 if (m_WidthFloatUnits
== wxHTML_UNITS_PERCENT
) {
189 if (m_WidthFloat
< 0) m_Width
= (100 + m_WidthFloat
) * w
/ 100;
190 else m_Width
= m_WidthFloat
* w
/ 100;
193 if (m_WidthFloat
< 0) m_Width
= w
+ m_WidthFloat
;
194 else m_Width
= m_WidthFloat
;
198 int l
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
199 int r
= (m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
;
200 m_Cells
-> Layout(m_Width
- (l
+ r
));
209 // adjust indentation:
210 s_indent
= (m_IndentLeft
< 0) ? (-m_IndentLeft
* m_Width
/ 100) : m_IndentLeft
;
211 s_width
= m_Width
- s_indent
- ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
216 while (cell
!= NULL
) {
217 switch (m_AlignVer
) {
218 case wxHTML_ALIGN_TOP
: ybasicpos
= 0; break;
219 case wxHTML_ALIGN_BOTTOM
: ybasicpos
= - cell
-> GetHeight(); break;
220 case wxHTML_ALIGN_CENTER
: ybasicpos
= - cell
-> GetHeight() / 2; break;
222 ydiff
= cell
-> GetHeight() + ybasicpos
;
224 if (cell
-> GetDescent() + ydiff
> ysizedown
) ysizedown
= cell
-> GetDescent() + ydiff
;
225 if (ybasicpos
+ cell
-> GetDescent() < -ysizeup
) ysizeup
= - (ybasicpos
+ cell
-> GetDescent());
227 cell
-> SetPos(xpos
, ybasicpos
+ cell
-> GetDescent());
228 xpos
+= cell
-> GetWidth();
229 cell
= cell
-> GetNext();
231 // force new line if occured:
232 if ((cell
== NULL
) || (xpos
+ cell
-> GetWidth() > s_width
)) {
233 if (xpos
> m_MaxLineWidth
) m_MaxLineWidth
= xpos
;
234 if (ysizeup
< 0) ysizeup
= 0;
235 if (ysizedown
< 0) ysizedown
= 0;
236 switch (m_AlignHor
) {
237 case wxHTML_ALIGN_LEFT
: xdelta
= 0; break;
238 case wxHTML_ALIGN_RIGHT
: xdelta
= 0 + (s_width
- xpos
); break;
239 case wxHTML_ALIGN_CENTER
: xdelta
= 0 + (s_width
- xpos
) / 2; break;
241 if (xdelta
< 0) xdelta
= 0;
245 while (line
!= cell
) {
246 line
-> SetPos(line
-> GetPosX() + xdelta
, ypos
+ line
-> GetPosY());
247 line
= line
-> GetNext();
252 ysizeup
= ysizedown
= 0;
257 // setup height & width, depending on container layout:
258 m_Height
= ypos
+ (ysizedown
+ ysizeup
) + m_IndentBottom
;
260 if (m_Height
< m_MinHeight
) {
261 if (m_MinHeightAlign
!= wxHTML_ALIGN_TOP
) {
262 int diff
= m_MinHeight
- m_Height
;
263 if (m_MinHeightAlign
== wxHTML_ALIGN_CENTER
) diff
/= 2;
266 cell
-> SetPos(cell
-> GetPosX(), cell
-> GetPosY() + diff
);
267 cell
= cell
-> GetNext();
270 m_Height
= m_MinHeight
;
273 m_MaxLineWidth
+= s_indent
+ ((m_IndentRight
< 0) ? (-m_IndentRight
* m_Width
/ 100) : m_IndentRight
);
274 if (m_Width
< m_MaxLineWidth
) m_Width
= m_MaxLineWidth
;
276 wxHtmlCell::Layout(w
);
280 #define mMin(a, b) (((a) < (b)) ? (a) : (b))
281 #define mMax(a, b) (((a) < (b)) ? (b) : (a))
283 void wxHtmlContainerCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
285 // container visible, draw it:
286 if ((y
+ m_PosY
< view_y2
) && (y
+ m_PosY
+ m_Height
> view_y1
)) {
289 wxBrush myb
= wxBrush(m_BkColour
, wxSOLID
);
291 int real_y1
= mMax(y
+ m_PosY
, view_y1
);
292 int real_y2
= mMin(y
+ m_PosY
+ m_Height
- 1, view_y2
);
295 dc
.SetPen(*wxTRANSPARENT_PEN
);
296 dc
.DrawRectangle(x
+ m_PosX
, real_y1
, m_Width
, real_y2
- real_y1
+ 1);
300 wxPen
mypen1(m_BorderColour1
, 1, wxSOLID
);
301 wxPen
mypen2(m_BorderColour2
, 1, wxSOLID
);
304 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1);
305 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
);
307 dc
.DrawLine(x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
308 dc
.DrawLine(x
+ m_PosX
, y
+ m_PosY
+ m_Height
- 1, x
+ m_PosX
+ m_Width
- 1, y
+ m_PosY
+ m_Height
- 1);
311 if (m_Cells
) m_Cells
-> Draw(dc
, x
+ m_PosX
, y
+ m_PosY
, view_y1
, view_y2
);
313 // container invisible, just proceed font+color changing:
315 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
318 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
323 void wxHtmlContainerCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
325 if (m_Cells
) m_Cells
-> DrawInvisible(dc
, x
+ m_PosX
, y
+ m_PosY
);
326 wxHtmlCell::DrawInvisible(dc
, x
, y
);
331 wxString
wxHtmlContainerCell::GetLink(int x
, int y
) const
333 wxHtmlCell
*c
= m_Cells
;
337 cx
= c
-> GetPosX(), cy
= c
-> GetPosY();
338 cw
= c
-> GetWidth(), ch
= c
-> GetHeight();
339 if ((x
>= cx
) && (x
< cx
+ cw
) && (y
>= cy
) && (y
< cy
+ ch
))
340 return c
-> GetLink(x
- cx
, y
- cy
);
343 return wxEmptyString
;
348 void wxHtmlContainerCell::InsertCell(wxHtmlCell
*f
)
350 if (!m_Cells
) m_Cells
= m_LastCell
= f
;
352 m_LastCell
-> SetNext(f
);
354 if (m_LastCell
) while (m_LastCell
-> GetNext()) m_LastCell
= m_LastCell
-> GetNext();
356 f
-> SetParent(this);
361 void wxHtmlContainerCell::SetAlign(const wxHtmlTag
& tag
)
363 if (tag
.HasParam("ALIGN")) {
364 wxString alg
= tag
.GetParam("ALIGN");
367 SetAlignHor(wxHTML_ALIGN_CENTER
);
368 else if (alg
== "LEFT")
369 SetAlignHor(wxHTML_ALIGN_LEFT
);
370 else if (alg
== "RIGHT")
371 SetAlignHor(wxHTML_ALIGN_RIGHT
);
377 void wxHtmlContainerCell::SetWidthFloat(const wxHtmlTag
& tag
)
379 if (tag
.HasParam("WIDTH")) {
381 wxString wd
= tag
.GetParam("WIDTH");
383 if (wd
[wd
.Length()-1] == '%') {
384 wxSscanf(wd
.c_str(), wxT("%i%%"), &wdi
);
385 SetWidthFloat(wdi
, wxHTML_UNITS_PERCENT
);
388 wxSscanf(wd
.c_str(), wxT("%i"), &wdi
);
389 SetWidthFloat(wdi
, wxHTML_UNITS_PIXELS
);
396 const wxHtmlCell
* wxHtmlContainerCell::Find(int condition
, const void* param
) const
398 const wxHtmlCell
*r
= NULL
;
401 r
= m_Cells
-> Find(condition
, param
);
405 return wxHtmlCell::Find(condition
, param
);
410 void wxHtmlContainerCell::OnMouseClick(wxWindow
*parent
, int x
, int y
, bool left
, bool middle
, bool right
)
413 wxHtmlCell
*c
= m_Cells
;
415 if ( (c
-> GetPosX() <= x
) &&
416 (c
-> GetPosY() <= y
) &&
417 (c
-> GetPosX() + c
-> GetWidth() > x
) &&
418 (c
-> GetPosY() + c
-> GetHeight() > y
)) {
419 c
-> OnMouseClick(parent
, x
- c
-> GetPosX(), y
- c
-> GetPosY(), left
, middle
, right
);
431 //--------------------------------------------------------------------------------
433 //--------------------------------------------------------------------------------
435 void wxHtmlColourCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
437 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
438 dc
.SetTextForeground(m_Colour
);
439 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
440 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
441 dc
.SetTextBackground(m_Colour
);
443 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
446 void wxHtmlColourCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
448 if (m_Flags
& wxHTML_CLR_FOREGROUND
)
449 dc
.SetTextForeground(m_Colour
);
450 if (m_Flags
& wxHTML_CLR_BACKGROUND
) {
451 dc
.SetBackground(wxBrush(m_Colour
, wxSOLID
));
452 dc
.SetTextBackground(m_Colour
);
454 wxHtmlCell::DrawInvisible(dc
, x
, y
);
460 //--------------------------------------------------------------------------------
462 //--------------------------------------------------------------------------------
464 void wxHtmlFontCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
467 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
470 void wxHtmlFontCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
473 wxHtmlCell::DrawInvisible(dc
, x
, y
);
483 //--------------------------------------------------------------------------------
485 //--------------------------------------------------------------------------------
487 wxHtmlWidgetCell::wxHtmlWidgetCell(wxWindow
*wnd
, int w
)
491 m_Wnd
-> GetSize(&sx
, &sy
);
492 m_Width
= sx
, m_Height
= sy
;
497 void wxHtmlWidgetCell::Draw(wxDC
& dc
, int x
, int y
, int view_y1
, int view_y2
)
499 int absx
= 0, absy
= 0, stx
, sty
;
500 wxHtmlCell
*c
= this;
503 absx
+= c
-> GetPosX();
504 absy
+= c
-> GetPosY();
505 c
= c
-> GetParent();
508 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
509 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
511 wxHtmlCell::Draw(dc
, x
, y
, view_y1
, view_y2
);
516 void wxHtmlWidgetCell::DrawInvisible(wxDC
& dc
, int x
, int y
)
518 int absx
= 0, absy
= 0, stx
, sty
;
519 wxHtmlCell
*c
= this;
522 absx
+= c
-> GetPosX();
523 absy
+= c
-> GetPosY();
524 c
= c
-> GetParent();
527 ((wxScrolledWindow
*)(m_Wnd
-> GetParent())) -> ViewStart(&stx
, &sty
);
528 m_Wnd
-> SetSize(absx
- wxHTML_SCROLL_STEP
* stx
, absy
- wxHTML_SCROLL_STEP
* sty
, m_Width
, m_Height
);
530 wxHtmlCell::DrawInvisible(dc
, x
, y
);
535 void wxHtmlWidgetCell::Layout(int w
)
537 if (m_WidthFloat
!= 0) {
538 m_Width
= (w
* m_WidthFloat
) / 100;
539 m_Wnd
-> SetSize(m_Width
, m_Height
);
542 wxHtmlCell::Layout(w
);