1 /////////////////////////////////////////////////////////////////////////////
2 // Name: richtext/richtexthtml.cpp
3 // Purpose: HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/richtext/richtexthtml.h"
27 #include "wx/filename.h"
28 #include "wx/wfstream.h"
29 #include "wx/txtstrm.h"
31 IMPLEMENT_DYNAMIC_CLASS(wxRichTextHTMLHandler
, wxRichTextFileHandler
)
33 /// Can we handle this filename (if using files)? By default, checks the extension.
34 bool wxRichTextHTMLHandler::CanHandle(const wxString
& filename
) const
36 wxString path
, file
, ext
;
37 wxSplitPath(filename
, & path
, & file
, & ext
);
39 return (ext
.Lower() == wxT("html") || ext
.Lower() == wxT("htm"));
44 bool wxRichTextHTMLHandler::DoLoadFile(wxRichTextBuffer
*WXUNUSED(buffer
), wxInputStream
& WXUNUSED(stream
))
50 * We need to output only _changes_ in character formatting.
53 bool wxRichTextHTMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
57 wxTextOutputStream
str(stream
);
59 wxTextAttrEx currentParaStyle
= buffer
->GetAttributes();
60 wxTextAttrEx currentCharStyle
= buffer
->GetAttributes();
62 str
<< wxT("<html><head></head><body>\n");
64 wxRichTextObjectList::compatibility_iterator node
= buffer
->GetChildren().GetFirst();
67 wxRichTextParagraph
* para
= wxDynamicCast(node
->GetData(), wxRichTextParagraph
);
68 wxASSERT (para
!= NULL
);
72 OutputParagraphFormatting(currentParaStyle
, para
->GetAttributes(), stream
, true);
74 wxRichTextObjectList::compatibility_iterator node2
= para
->GetChildren().GetFirst();
77 wxRichTextObject
* obj
= node2
->GetData();
78 wxRichTextPlainText
* textObj
= wxDynamicCast(obj
, wxRichTextPlainText
);
79 if (textObj
&& !textObj
->IsEmpty())
81 OutputCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
, true);
83 str
<< textObj
->GetText();
85 OutputCharacterFormatting(currentCharStyle
, obj
->GetAttributes(), stream
, false);
88 node2
= node2
->GetNext();
91 OutputParagraphFormatting(currentParaStyle
, para
->GetAttributes(), stream
, false);
96 node
= node
->GetNext();
99 str
<< wxT("</body></html>\n");
104 /// Output character formatting
105 void wxRichTextHTMLHandler::OutputCharacterFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
, bool start
)
107 wxTextOutputStream
str(stream
);
110 bool isItalic
= false;
111 bool isUnderline
= false;
114 if (thisStyle
.GetFont().Ok())
116 if (thisStyle
.GetFont().GetWeight() == wxBOLD
)
118 if (thisStyle
.GetFont().GetStyle() == wxITALIC
)
120 if (thisStyle
.GetFont().GetUnderlined())
123 faceName
= thisStyle
.GetFont().GetFaceName();
146 /// Output paragraph formatting
147 void wxRichTextHTMLHandler::OutputParagraphFormatting(const wxTextAttrEx
& WXUNUSED(currentStyle
), const wxTextAttrEx
& thisStyle
, wxOutputStream
& stream
, bool start
)
149 // TODO: lists, indentation (using tables), fonts, right-align, ...
151 wxTextOutputStream
str(stream
);
152 bool isCentered
= false;
154 if (thisStyle
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
162 str
<< wxT("<center>");
167 str
<< wxT("</center>");