]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_pre.cpp
Bug fix in GetLabelFromText
[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
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
33 FORCE_LINK_ME(m_pre)
34
35
36 //-----------------------------------------------------------------------------
37 // wxHtmlCodeCell
38 //-----------------------------------------------------------------------------
39
40 class wxHtmlPRECell : public wxHtmlCell
41 {
42 private:
43 wxString** m_Text;
44 // list of wxString objects.
45 int m_LinesCnt;
46 // number of lines
47 int m_LineHeight;
48 // height of single line of text
49
50 public:
51 wxHtmlPRECell(const wxString& s, wxDC& dc);
52 ~wxHtmlPRECell();
53 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
54 };
55
56
57 wxHtmlPRECell::wxHtmlPRECell(const wxString& s, wxDC& dc) : wxHtmlCell()
58 {
59 wxStringTokenizer tokenizer(s, "\n");
60 wxString tmp;
61 long int x, z;
62 int i;
63
64 m_LineHeight = dc.GetCharHeight();
65 m_LinesCnt = 0;
66 m_Text = NULL;
67 m_Width = m_Height = 0;
68
69 i = 0;
70 while (tokenizer.HasMoreTokens()) {
71 if (i % 10 == 0) m_Text = (wxString**) realloc(m_Text, sizeof(wxString*) * (i + 10));
72 tmp = tokenizer.NextToken();
73 tmp.Replace(wxT("&copy;"), wxT("(c)"), TRUE);
74 tmp.Replace(wxT("&nbsp;"), wxT(" "), TRUE);
75 tmp.Replace(wxT("&quot;"), wxT("\""), TRUE);
76 tmp.Replace(wxT("&lt;"), wxT("<"), TRUE);
77 tmp.Replace(wxT("&gt;"), wxT(">"), TRUE);
78 tmp.Replace(wxT("&amp;"), wxT("&"), TRUE);
79 tmp.Replace(wxT("\t"), wxT(" "), TRUE);
80 tmp.Replace(wxT("\r"), wxT(""), TRUE);
81 m_Text[i++] = new wxString(tmp);
82
83 dc.GetTextExtent(tmp, &x, &z, &z);
84 if (x > m_Width) m_Width = x;
85 m_Height += m_LineHeight;
86 m_LinesCnt++;
87 }
88 }
89
90
91
92 wxHtmlPRECell::~wxHtmlPRECell()
93 {
94 for (int i = 0; i < m_LinesCnt; i++) delete m_Text[i];
95 free(m_Text);
96 }
97
98
99 void wxHtmlPRECell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
100 {
101 for (int i = 0; i < m_LinesCnt; i++)
102 dc.DrawText(*(m_Text[i]), x + m_PosX, y + m_PosY + m_LineHeight * i);
103
104 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
105 }
106
107
108
109
110 //-----------------------------------------------------------------------------
111 // The list handler:
112 //-----------------------------------------------------------------------------
113
114
115 TAG_HANDLER_BEGIN(PRE, "PRE")
116
117 TAG_HANDLER_PROC(tag)
118 {
119 wxHtmlContainerCell *c;
120
121 int fixed = m_WParser -> GetFontFixed(),
122 italic = m_WParser -> GetFontItalic(),
123 underlined = m_WParser -> GetFontUnderlined(),
124 bold = m_WParser -> GetFontBold(),
125 fsize = m_WParser -> GetFontSize();
126
127 m_WParser -> CloseContainer();
128 c = m_WParser -> OpenContainer();
129 c -> SetAlignHor(wxHTML_ALIGN_LEFT);
130 c -> SetIndent(m_WParser -> GetCharHeight(), wxHTML_INDENT_VERTICAL);
131
132 m_WParser -> SetFontUnderlined(FALSE);
133 m_WParser -> SetFontBold(FALSE);
134 m_WParser -> SetFontItalic(FALSE);
135 m_WParser -> SetFontFixed(TRUE);
136 m_WParser -> SetFontSize(3);
137 c -> InsertCell(new wxHtmlFontCell(m_WParser -> CreateCurrentFont()));
138
139 {
140 wxString cit;
141 cit = m_WParser -> GetSource() -> Mid(tag.GetBeginPos(), tag.GetEndPos1() - tag.GetBeginPos());
142 c -> InsertCell(new wxHtmlPRECell(cit, *(m_WParser -> GetDC())));
143 }
144
145 m_WParser -> SetFontUnderlined(underlined);
146 m_WParser -> SetFontBold(bold);
147 m_WParser -> SetFontItalic(italic);
148 m_WParser -> SetFontFixed(fixed);
149 m_WParser -> SetFontSize(fsize);
150 c -> InsertCell(new wxHtmlFontCell(m_WParser -> CreateCurrentFont()));
151
152 m_WParser -> CloseContainer();
153 m_WParser -> OpenContainer();
154 return TRUE;
155 }
156
157 TAG_HANDLER_END(PRE)
158
159
160
161
162
163 TAGS_MODULE_BEGIN(Pre)
164
165 TAGS_MODULE_ADD(PRE)
166
167 TAGS_MODULE_END(Pre)
168
169 #endif