]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/html/m_pre.cpp
Merge in from trunk r64802 - r68625
[wxWidgets.git] / src / html / m_pre.cpp
... / ...
CommitLineData
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
28FORCE_LINK_ME(m_pre)
29
30// replaces '\t', ' ' and '\n' with HTML markup:
31static wxString LINKAGEMODE HtmlizeLinebreaks(const wxString& str)
32{
33 wxString out;
34 out.reserve(str.length()); // we'll certainly need at least that
35
36 const wxString::const_iterator end = str.end();
37 for ( wxString::const_iterator i = str.begin(); i != end; ++i )
38 {
39 switch ( (*i).GetValue() )
40 {
41 case '<':
42 while ( i != end && *i != '>' )
43 {
44 out << *i++;
45 }
46 out << '>';
47 if ( i == end )
48 return out;
49 break;
50
51 // We need to translate any line break into exactly one <br>.
52 // Quoting HTML spec: "A line break is defined to be a carriage
53 // return (&#x000D;), a line feed (&#x000A;), or a carriage
54 // return/line feed pair."
55 case '\r':
56 {
57 wxString::const_iterator j(i + 1);
58 if ( j != end && *j == '\n' )
59 i = j;
60 }
61 // fall through
62 case '\n':
63 out << "<br>";
64 break;
65
66 default:
67 out << *i;
68 break;
69 }
70 }
71
72 return out;
73}
74
75
76//-----------------------------------------------------------------------------
77// The list handler:
78//-----------------------------------------------------------------------------
79
80
81TAG_HANDLER_BEGIN(PRE, "PRE")
82 TAG_HANDLER_CONSTR(PRE) { }
83
84 TAG_HANDLER_PROC(tag)
85 {
86 wxHtmlContainerCell *c;
87
88 const int fixed = m_WParser->GetFontFixed();
89 const int italic = m_WParser->GetFontItalic();
90 const int underlined = m_WParser->GetFontUnderlined();
91 const int bold = m_WParser->GetFontBold();
92 const int fsize = m_WParser->GetFontSize();
93 const wxHtmlWinParser::WhitespaceMode whitespace =
94 m_WParser->GetWhitespaceMode();
95
96 c = m_WParser->GetContainer();
97 m_WParser->SetWhitespaceMode(wxHtmlWinParser::Whitespace_Pre);
98 m_WParser->SetFontUnderlined(false);
99 m_WParser->SetFontBold(false);
100 m_WParser->SetFontItalic(false);
101 m_WParser->SetFontFixed(true);
102 m_WParser->SetFontSize(3);
103 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
104
105 m_WParser->CloseContainer();
106 c = m_WParser->OpenContainer();
107 c->SetWidthFloat(tag);
108 c = m_WParser->OpenContainer();
109 c->SetAlignHor(wxHTML_ALIGN_LEFT);
110 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
111
112 wxString srcMid = m_WParser->GetInnerSource(tag);
113
114 // setting Whitespace_Pre mode takes care of spaces and TABs, but
115 // not linebreaks, so we have to translate them into <br> by
116 // calling HtmlizeLinebreaks() here
117 ParseInnerSource(HtmlizeLinebreaks(srcMid));
118
119 m_WParser->CloseContainer();
120 m_WParser->CloseContainer();
121 c = m_WParser->OpenContainer();
122
123 m_WParser->SetWhitespaceMode(whitespace);
124 m_WParser->SetFontUnderlined(underlined);
125 m_WParser->SetFontBold(bold);
126 m_WParser->SetFontItalic(italic);
127 m_WParser->SetFontFixed(fixed);
128 m_WParser->SetFontSize(fsize);
129 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
130
131 return true;
132 }
133
134TAG_HANDLER_END(PRE)
135
136
137
138
139
140TAGS_MODULE_BEGIN(Pre)
141
142 TAGS_MODULE_ADD(PRE)
143
144TAGS_MODULE_END(Pre)
145
146#endif