merged 2.2 branch
[wxWidgets.git] / src / html / m_pre.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: m_pre.cpp
3 // Purpose: wxHtml module for <PRE> ... </PRE> tag (code citation)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #include "wx/defs.h"
17 #if wxUSE_HTML && wxUSE_STREAMS
18 #ifdef __BORDLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #include "wx/wx.h"
24 #endif
25
26
27 #include "wx/html/forcelnk.h"
28 #include "wx/html/m_templ.h"
29
30 #include "wx/html/htmlcell.h"
31 #include "wx/tokenzr.h"
32 #include "wx/encconv.h"
33
34 FORCE_LINK_ME(m_pre)
35
36
37 //-----------------------------------------------------------------------------
38 // wxHtmlCodeCell
39 //-----------------------------------------------------------------------------
40
41 class wxHtmlPRECell : public wxHtmlCell
42 {
43 private:
44 wxString** m_Text;
45 // list of wxString objects.
46 int m_LinesCnt;
47 // number of lines
48 int m_LineHeight;
49 // height of single line of text
50
51 public:
52 wxHtmlPRECell(const wxString& s, wxDC& dc);
53 ~wxHtmlPRECell();
54 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
55 };
56
57
58 wxHtmlPRECell::wxHtmlPRECell(const wxString& s, wxDC& dc) : wxHtmlCell()
59 {
60 wxStringTokenizer tokenizer(s, "\n");
61 wxString tmp;
62 long int x, z;
63 int i;
64
65 m_LineHeight = dc.GetCharHeight();
66 m_LinesCnt = 0;
67 m_Text = NULL;
68 m_Width = m_Height = 0;
69
70 i = 0;
71 while (tokenizer.HasMoreTokens()) {
72 if (i % 10 == 0) m_Text = (wxString**) realloc(m_Text, sizeof(wxString*) * (i + 10));
73 tmp = tokenizer.NextToken();
74 tmp.Replace(wxT("&copy;"), wxT("(c)"), TRUE);
75 tmp.Replace(wxT("&nbsp;"), wxT(" "), TRUE);
76 tmp.Replace(wxT("&quot;"), wxT("\""), TRUE);
77 tmp.Replace(wxT("&lt;"), wxT("<"), TRUE);
78 tmp.Replace(wxT("&gt;"), wxT(">"), TRUE);
79 tmp.Replace(wxT("&amp;"), wxT("&"), TRUE);
80 tmp.Replace(wxT("\t"), wxT(" "), TRUE);
81 tmp.Replace(wxT("\r"), wxT(""), TRUE);
82 m_Text[i++] = new wxString(tmp);
83
84 dc.GetTextExtent(tmp, &x, &z, &z);
85 if (x > m_Width) m_Width = x;
86 m_Height += m_LineHeight;
87 m_LinesCnt++;
88 }
89 }
90
91
92
93 wxHtmlPRECell::~wxHtmlPRECell()
94 {
95 for (int i = 0; i < m_LinesCnt; i++) delete m_Text[i];
96 free(m_Text);
97 }
98
99
100 void wxHtmlPRECell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
101 {
102 for (int i = 0; i < m_LinesCnt; i++)
103 dc.DrawText(*(m_Text[i]), x + m_PosX, y + m_PosY + m_LineHeight * i);
104
105 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
106 }
107
108
109
110
111 //-----------------------------------------------------------------------------
112 // The list handler:
113 //-----------------------------------------------------------------------------
114
115
116 TAG_HANDLER_BEGIN(PRE, "PRE")
117
118 TAG_HANDLER_PROC(tag)
119 {
120 wxHtmlContainerCell *c;
121
122 int fixed = m_WParser -> GetFontFixed(),
123 italic = m_WParser -> GetFontItalic(),
124 underlined = m_WParser -> GetFontUnderlined(),
125 bold = m_WParser -> GetFontBold(),
126 fsize = m_WParser -> GetFontSize();
127
128 m_WParser -> CloseContainer();
129 c = m_WParser -> OpenContainer();
130 c -> SetAlignHor(wxHTML_ALIGN_LEFT);
131 c -> SetIndent(m_WParser -> GetCharHeight(), wxHTML_INDENT_VERTICAL);
132
133 m_WParser -> SetFontUnderlined(FALSE);
134 m_WParser -> SetFontBold(FALSE);
135 m_WParser -> SetFontItalic(FALSE);
136 m_WParser -> SetFontFixed(TRUE);
137 m_WParser -> SetFontSize(3);
138 c -> InsertCell(new wxHtmlFontCell(m_WParser -> CreateCurrentFont()));
139
140 {
141 wxString cit;
142 wxEncodingConverter *encconv = m_WParser -> GetEncodingConverter();
143 cit = m_WParser -> GetSource() -> Mid(tag.GetBeginPos(),
144 tag.GetEndPos1() - tag.GetBeginPos());
145 if (encconv)
146 c -> InsertCell(new wxHtmlPRECell(encconv -> Convert(cit),
147 *(m_WParser -> GetDC())));
148 else
149 c -> InsertCell(new wxHtmlPRECell(cit,
150 *(m_WParser -> GetDC())));
151 }
152
153 m_WParser -> SetFontUnderlined(underlined);
154 m_WParser -> SetFontBold(bold);
155 m_WParser -> SetFontItalic(italic);
156 m_WParser -> SetFontFixed(fixed);
157 m_WParser -> SetFontSize(fsize);
158 c -> InsertCell(new wxHtmlFontCell(m_WParser -> CreateCurrentFont()));
159
160 m_WParser -> CloseContainer();
161 m_WParser -> OpenContainer();
162 return TRUE;
163 }
164
165 TAG_HANDLER_END(PRE)
166
167
168
169
170
171 TAGS_MODULE_BEGIN(Pre)
172
173 TAGS_MODULE_ADD(PRE)
174
175 TAGS_MODULE_END(Pre)
176
177 #endif