use iterators in HtmlizeLinebreaks()
[wxWidgets.git] / src / html / m_pre.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/html/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 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_HTML && wxUSE_STREAMS
17
18 #ifndef WX_PRECOMP
19 #endif
20
21 #include "wx/html/forcelnk.h"
22 #include "wx/html/m_templ.h"
23
24 #include "wx/html/htmlcell.h"
25 #include "wx/tokenzr.h"
26 #include "wx/encconv.h"
27
28 FORCE_LINK_ME(m_pre)
29
30 // replaces '\t', ' ' and '\n' with HTML markup:
31 static wxString LINKAGEMODE HtmlizeLinebreaks(const wxString& str)
32 {
33 wxString out;
34 out.reserve(str.length()); // we'll certainly need at least that
35
36 for ( wxString::const_iterator i = str.begin(); i != str.end(); ++i )
37 {
38 switch ( (*i).GetValue() )
39 {
40 case '<':
41 while ( i != str.end() && *i != '>')
42 {
43 out << *i++;
44 }
45 out << '>';
46 break;
47 case '\n':
48 out << "<br>";
49 break;
50 default:
51 out << *i;
52 break;
53 }
54 }
55 return out;
56 }
57
58
59 //-----------------------------------------------------------------------------
60 // The list handler:
61 //-----------------------------------------------------------------------------
62
63
64 TAG_HANDLER_BEGIN(PRE, "PRE")
65 TAG_HANDLER_CONSTR(PRE) { }
66
67 TAG_HANDLER_PROC(tag)
68 {
69 wxHtmlContainerCell *c;
70
71 const int fixed = m_WParser->GetFontFixed();
72 const int italic = m_WParser->GetFontItalic();
73 const int underlined = m_WParser->GetFontUnderlined();
74 const int bold = m_WParser->GetFontBold();
75 const int fsize = m_WParser->GetFontSize();
76 const wxHtmlWinParser::WhitespaceMode whitespace =
77 m_WParser->GetWhitespaceMode();
78
79 c = m_WParser->GetContainer();
80 m_WParser->SetWhitespaceMode(wxHtmlWinParser::Whitespace_Pre);
81 m_WParser->SetFontUnderlined(false);
82 m_WParser->SetFontBold(false);
83 m_WParser->SetFontItalic(false);
84 m_WParser->SetFontFixed(true);
85 m_WParser->SetFontSize(3);
86 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
87
88 m_WParser->CloseContainer();
89 c = m_WParser->OpenContainer();
90 c->SetWidthFloat(tag);
91 c = m_WParser->OpenContainer();
92 c->SetAlignHor(wxHTML_ALIGN_LEFT);
93 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
94
95 wxString srcMid = m_WParser->GetInnerSource(tag);
96
97 // setting Whitespace_Pre mode takes care of spaces and TABs, but
98 // not linebreaks, so we have to translate them into <br> by
99 // calling HtmlizeLinebreaks() here
100 ParseInnerSource(HtmlizeLinebreaks(srcMid));
101
102 m_WParser->CloseContainer();
103 m_WParser->CloseContainer();
104 c = m_WParser->OpenContainer();
105
106 m_WParser->SetWhitespaceMode(whitespace);
107 m_WParser->SetFontUnderlined(underlined);
108 m_WParser->SetFontBold(bold);
109 m_WParser->SetFontItalic(italic);
110 m_WParser->SetFontFixed(fixed);
111 m_WParser->SetFontSize(fsize);
112 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
113
114 return true;
115 }
116
117 TAG_HANDLER_END(PRE)
118
119
120
121
122
123 TAGS_MODULE_BEGIN(Pre)
124
125 TAGS_MODULE_ADD(PRE)
126
127 TAGS_MODULE_END(Pre)
128
129 #endif