preserve TAB characters when copying HTML <pre> content to clipboard
[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 size_t len = str.Len();
37 for (size_t i = 0; i < len; i++)
38 {
39 switch ( str[i].GetValue() )
40 {
41 case '<':
42 while (i < len && str[i] != '>')
43 {
44 out << str[i++];
45 }
46 out << '>';
47 break;
48 case '\n':
49 out << "<br>";
50 break;
51 default:
52 out << str[i];
53 break;
54 }
55 }
56 return out;
57 }
58
59
60 //-----------------------------------------------------------------------------
61 // The list handler:
62 //-----------------------------------------------------------------------------
63
64
65 TAG_HANDLER_BEGIN(PRE, "PRE")
66 TAG_HANDLER_CONSTR(PRE) { }
67
68 TAG_HANDLER_PROC(tag)
69 {
70 wxHtmlContainerCell *c;
71
72 const int fixed = m_WParser->GetFontFixed();
73 const int italic = m_WParser->GetFontItalic();
74 const int underlined = m_WParser->GetFontUnderlined();
75 const int bold = m_WParser->GetFontBold();
76 const int fsize = m_WParser->GetFontSize();
77 const wxHtmlWinParser::WhitespaceMode whitespace =
78 m_WParser->GetWhitespaceMode();
79
80 c = m_WParser->GetContainer();
81 m_WParser->SetWhitespaceMode(wxHtmlWinParser::Whitespace_Pre);
82 m_WParser->SetFontUnderlined(false);
83 m_WParser->SetFontBold(false);
84 m_WParser->SetFontItalic(false);
85 m_WParser->SetFontFixed(true);
86 m_WParser->SetFontSize(3);
87 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
88
89 m_WParser->CloseContainer();
90 c = m_WParser->OpenContainer();
91 c->SetWidthFloat(tag);
92 c = m_WParser->OpenContainer();
93 c->SetAlignHor(wxHTML_ALIGN_LEFT);
94 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
95
96 wxString srcMid = m_WParser->GetInnerSource(tag);
97
98 // setting Whitespace_Pre mode takes care of spaces and TABs, but
99 // not linebreaks, so we have to translate them into <br> by
100 // calling HtmlizeLinebreaks() here
101 ParseInnerSource(HtmlizeLinebreaks(srcMid));
102
103 m_WParser->CloseContainer();
104 m_WParser->CloseContainer();
105 c = m_WParser->OpenContainer();
106
107 m_WParser->SetWhitespaceMode(whitespace);
108 m_WParser->SetFontUnderlined(underlined);
109 m_WParser->SetFontBold(bold);
110 m_WParser->SetFontItalic(italic);
111 m_WParser->SetFontFixed(fixed);
112 m_WParser->SetFontSize(fsize);
113 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
114
115 return true;
116 }
117
118 TAG_HANDLER_END(PRE)
119
120
121
122
123
124 TAGS_MODULE_BEGIN(Pre)
125
126 TAGS_MODULE_ADD(PRE)
127
128 TAGS_MODULE_END(Pre)
129
130 #endif