]> git.saurik.com Git - wxWidgets.git/blob - src/richtext/richtexthtml.cpp
added missing include; minor reformatting
[wxWidgets.git] / src / richtext / richtexthtml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtexthtml.cpp
3 // Purpose: HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_RICHTEXT
20
21 #include "wx/richtext/richtexthtml.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/filename.h"
28 #include "wx/wfstream.h"
29 #include "wx/txtstrm.h"
30
31 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler, wxRichTextFileHandler)
32
33 /// Can we handle this filename (if using files)? By default, checks the extension.
34 bool wxRichTextHTMLHandler::CanHandle(const wxString& filename) const
35 {
36 wxString path, file, ext;
37 wxSplitPath(filename, & path, & file, & ext);
38
39 return (ext.Lower() == wxT("html") || ext.Lower() == wxT("htm"));
40 }
41
42
43 #if wxUSE_STREAMS
44 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer *WXUNUSED(buffer), wxInputStream& WXUNUSED(stream))
45 {
46 return false;
47 }
48
49 /*
50 * We need to output only _changes_ in character formatting.
51 */
52
53 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
54 {
55 buffer->Defragment();
56
57 wxTextOutputStream str(stream);
58
59 wxTextAttrEx currentParaStyle = buffer->GetAttributes();
60 wxTextAttrEx currentCharStyle = buffer->GetAttributes();
61
62 str << wxT("<html><head></head><body>\n");
63
64 wxRichTextObjectList::compatibility_iterator node = buffer->GetChildren().GetFirst();
65 while (node)
66 {
67 wxRichTextParagraph* para = wxDynamicCast(node->GetData(), wxRichTextParagraph);
68 wxASSERT (para != NULL);
69
70 if (para)
71 {
72 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, true);
73
74 wxRichTextObjectList::compatibility_iterator node2 = para->GetChildren().GetFirst();
75 while (node2)
76 {
77 wxRichTextObject* obj = node2->GetData();
78 wxRichTextPlainText* textObj = wxDynamicCast(obj, wxRichTextPlainText);
79 if (textObj && !textObj->IsEmpty())
80 {
81 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, true);
82
83 str << textObj->GetText();
84
85 OutputCharacterFormatting(currentCharStyle, obj->GetAttributes(), stream, false);
86 }
87
88 node2 = node2->GetNext();
89 }
90
91 OutputParagraphFormatting(currentParaStyle, para->GetAttributes(), stream, false);
92
93 str << wxT("<P>\n");
94 }
95
96 node = node->GetNext();
97 }
98
99 str << wxT("</body></html>\n");
100
101 return true;
102 }
103
104 /// Output character formatting
105 void wxRichTextHTMLHandler::OutputCharacterFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
106 {
107 wxTextOutputStream str(stream);
108
109 bool isBold = false;
110 bool isItalic = false;
111 bool isUnderline = false;
112 wxString faceName;
113
114 if (thisStyle.GetFont().Ok())
115 {
116 if (thisStyle.GetFont().GetWeight() == wxBOLD)
117 isBold = true;
118 if (thisStyle.GetFont().GetStyle() == wxITALIC)
119 isItalic = true;
120 if (thisStyle.GetFont().GetUnderlined())
121 isUnderline = true;
122
123 faceName = thisStyle.GetFont().GetFaceName();
124 }
125
126 if (start)
127 {
128 if (isBold)
129 str << wxT("<b>");
130 if (isItalic)
131 str << wxT("<i>");
132 if (isUnderline)
133 str << wxT("<u>");
134 }
135 else
136 {
137 if (isUnderline)
138 str << wxT("</u>");
139 if (isItalic)
140 str << wxT("</i>");
141 if (isBold)
142 str << wxT("</b>");
143 }
144 }
145
146 /// Output paragraph formatting
147 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx& WXUNUSED(currentStyle), const wxTextAttrEx& thisStyle, wxOutputStream& stream, bool start)
148 {
149 // TODO: lists, indentation (using tables), fonts, right-align, ...
150
151 wxTextOutputStream str(stream);
152 bool isCentered = false;
153
154 if (thisStyle.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
155 {
156 isCentered = true;
157 }
158
159 if (start)
160 {
161 if (isCentered)
162 str << wxT("<center>");
163 }
164 else
165 {
166 if (isCentered)
167 str << wxT("</center>");
168 }
169 }
170
171 #endif
172 // wxUSE_STREAMS
173
174 #endif
175 // wxUSE_RICHTEXT
176