]> git.saurik.com Git - wxWidgets.git/blob - src/html/m_pre.cpp
On the Mac the Alt key is a character modifier like Shift, so let char
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #endif
24
25 #include "wx/html/forcelnk.h"
26 #include "wx/html/m_templ.h"
27
28 #include "wx/html/htmlcell.h"
29 #include "wx/tokenzr.h"
30 #include "wx/encconv.h"
31
32 FORCE_LINK_ME(m_pre)
33
34 // replaces '\t', ' ' and '\n' with HTML markup:
35 static wxString LINKAGEMODE HtmlizeWhitespaces(const wxString& str)
36 {
37 wxString out;
38 size_t len = str.Len();
39 size_t linepos = 0;
40 for (size_t i = 0; i < len; i++)
41 {
42 switch (str[i])
43 {
44 case wxT('<'):
45 while (i < len && str[i] != wxT('>'))
46 {
47 out << str[i++];
48 linepos++;
49 }
50 out << wxT('>');
51 linepos++;
52 break;
53 case wxT(' '):
54 out << wxT("&nbsp;");
55 linepos++;
56 break;
57 case wxT('\n'):
58 out << wxT("<br>");
59 linepos = 0;
60 break;
61 case wxT('\t'):
62 {
63 for (size_t j = 8 - linepos % 8; j > 0; j--)
64 out << wxT("&nbsp;");
65 linepos += 8 - linepos % 8;
66 }
67 break;
68 default:
69 out << str[i];
70 linepos++;
71 break;
72 }
73 }
74 return out;
75 }
76
77
78 //-----------------------------------------------------------------------------
79 // The list handler:
80 //-----------------------------------------------------------------------------
81
82
83 TAG_HANDLER_BEGIN(PRE, "PRE")
84 TAG_HANDLER_CONSTR(PRE) { }
85
86 TAG_HANDLER_PROC(tag)
87 {
88 wxHtmlContainerCell *c;
89
90 int fixed = m_WParser->GetFontFixed(),
91 italic = m_WParser->GetFontItalic(),
92 underlined = m_WParser->GetFontUnderlined(),
93 bold = m_WParser->GetFontBold(),
94 fsize = m_WParser->GetFontSize();
95
96 c = m_WParser->GetContainer();
97 m_WParser->SetFontUnderlined(false);
98 m_WParser->SetFontBold(false);
99 m_WParser->SetFontItalic(false);
100 m_WParser->SetFontFixed(true);
101 m_WParser->SetFontSize(3);
102 c->InsertCell(new wxHtmlFontCell(m_WParser->CreateCurrentFont()));
103
104 m_WParser->CloseContainer();
105 c = m_WParser->OpenContainer();
106 c->SetWidthFloat(tag);
107 c = m_WParser->OpenContainer();
108 c->SetAlignHor(wxHTML_ALIGN_LEFT);
109 c->SetIndent(m_WParser->GetCharHeight(), wxHTML_INDENT_TOP);
110
111 wxString srcMid =
112 m_WParser->GetSource()->Mid(tag.GetBeginPos(),
113 tag.GetEndPos1() - tag.GetBeginPos());
114 // It is safe to temporarily change the source being parsed,
115 // provided we restore the state back after parsing
116 m_Parser->SetSourceAndSaveState(HtmlizeWhitespaces(srcMid));
117 m_Parser->DoParsing();
118 m_Parser->RestoreState();
119
120 m_WParser->CloseContainer();
121 m_WParser->CloseContainer();
122 c = m_WParser->OpenContainer();
123
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
134 TAG_HANDLER_END(PRE)
135
136
137
138
139
140 TAGS_MODULE_BEGIN(Pre)
141
142 TAGS_MODULE_ADD(PRE)
143
144 TAGS_MODULE_END(Pre)
145
146 #endif