1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextxml.cpp
3 // Purpose: XML and 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"
19 #if wxUSE_RICHTEXT && wxUSE_XML
21 #include "wx/richtext/richtextxml.h"
25 #include "wx/module.h"
29 #include "wx/filename.h"
30 #include "wx/clipbrd.h"
31 #include "wx/wfstream.h"
32 #include "wx/sstream.h"
33 #include "wx/txtstrm.h"
34 #include "wx/mstream.h"
35 #include "wx/tokenzr.h"
36 #include "wx/stopwatch.h"
37 #include "wx/xml/xml.h"
39 // Set to 1 for slower wxXmlDocument method, 0 for faster direct method.
40 // If we make wxXmlDocument::Save more efficient, we might switch to this
42 #define wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT 0
44 #if wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT && !wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
45 # error Must define wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT in richtextxml.h to use this method.
48 #if !wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT && !wxRICHTEXT_HAVE_DIRECT_OUTPUT
49 # error Must define wxRICHTEXT_HAVE_DIRECT_OUTPUT in richtextxml.h to use this method.
52 // Set to 1 to time file saving
53 #define wxRICHTEXT_USE_OUTPUT_TIMINGS 0
55 // Convert a colour to a 6-digit hex string
56 static wxString
ColourToHexString(const wxColour
& col
)
60 hex
+= wxDecToHex(col
.Red());
61 hex
+= wxDecToHex(col
.Green());
62 hex
+= wxDecToHex(col
.Blue());
67 // Convert 6-digit hex string to a colour
68 static wxColour
HexStringToColour(const wxString
& hex
)
70 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
71 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
72 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
74 return wxColour(r
, g
, b
);
77 static inline wxString
MakeString(const int& v
) { return wxString::Format(wxT("%d"), v
); }
78 static inline wxString
MakeString(const long& v
) { return wxString::Format(wxT("%ld"), v
); }
79 static inline wxString
MakeString(const double& v
) { return wxString::Format(wxT("%.2f"), (float) v
); }
80 static inline wxString
MakeString(const wxString
& s
) { return s
; }
81 static inline wxString
MakeString(const wxColour
& col
) { return wxT("#") + ColourToHexString(col
); }
83 static inline void AddString(wxString
& str
, const int& v
) { str
<< wxString::Format(wxT("%d"), v
); }
84 static inline void AddString(wxString
& str
, const long& v
) { str
<< wxString::Format(wxT("%ld"), v
); }
85 static inline void AddString(wxString
& str
, const double& v
) { str
<< wxString::Format(wxT("%.2f"), (float) v
); }
86 static inline void AddString(wxString
& str
, const wxChar
* s
) { str
<< s
; }
87 static inline void AddString(wxString
& str
, const wxString
& s
) { str
<< s
; }
88 static inline void AddString(wxString
& str
, const wxColour
& col
) { str
<< wxT("#") << ColourToHexString(col
); }
90 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler
, wxRichTextFileHandler
)
92 void wxRichTextXMLHandler::Init()
94 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
102 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
107 buffer
->ResetAndClearCommands();
110 wxXmlDocument
* xmlDoc
= new wxXmlDocument
;
113 // This is the encoding to convert to (memory encoding rather than file encoding)
114 wxString
encoding(wxT("UTF-8"));
116 #if !wxUSE_UNICODE && wxUSE_INTL
117 encoding
= wxLocale::GetSystemEncodingName();
120 if (!xmlDoc
->Load(stream
, encoding
))
122 buffer
->ResetAndClearCommands();
127 if (xmlDoc
->GetRoot() && xmlDoc
->GetRoot()->GetType() == wxXML_ELEMENT_NODE
&& xmlDoc
->GetRoot()->GetName() == wxT("richtext"))
129 wxXmlNode
* child
= xmlDoc
->GetRoot()->GetChildren();
132 if (child
->GetType() == wxXML_ELEMENT_NODE
)
134 wxString name
= child
->GetName();
135 if (name
== wxT("richtext-version"))
139 ImportXML(buffer
, buffer
, child
);
142 child
= child
->GetNext();
153 buffer
->UpdateRanges();
158 /// Creates an object given an XML element name
159 wxRichTextObject
* wxRichTextXMLHandler::CreateObjectForXMLName(wxRichTextObject
* WXUNUSED(parent
), const wxString
& name
) const
161 if (name
== wxT("text") || name
== wxT("symbol"))
162 return new wxRichTextPlainText
;
163 else if (name
== wxT("image"))
164 return new wxRichTextImage
;
165 else if (name
== wxT("paragraph"))
166 return new wxRichTextParagraph
;
167 else if (name
== wxT("paragraphlayout"))
168 return new wxRichTextParagraphLayoutBox
;
169 else if (name
== wxT("textbox"))
170 return new wxRichTextBox
;
171 else if (name
== wxT("cell"))
172 return new wxRichTextCell
;
173 else if (name
== wxT("table"))
174 return new wxRichTextTable
;
179 /// Recursively import an object
180 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer
* buffer
, wxRichTextObject
* obj
, wxXmlNode
* node
)
182 bool recurse
= false;
183 obj
->ImportFromXML(buffer
, node
, this, & recurse
);
185 // TODO: how to control whether to import children.
187 wxRichTextCompositeObject
* compositeParent
= wxDynamicCast(obj
, wxRichTextCompositeObject
);
188 if (recurse
&& compositeParent
)
190 wxXmlNode
* child
= node
->GetChildren();
193 if (child
->GetName() != wxT("stylesheet"))
195 wxRichTextObject
* childObj
= CreateObjectForXMLName(obj
, child
->GetName());
198 compositeParent
->AppendChild(childObj
);
199 ImportXML(buffer
, childObj
, child
);
202 child
= child
->GetNext();
209 bool wxRichTextXMLHandler::ImportProperties(wxRichTextObject
* obj
, wxXmlNode
* node
)
211 wxXmlNode
* child
= node
->GetChildren();
214 if (child
->GetName() == wxT("properties"))
216 wxXmlNode
* propertyChild
= child
->GetChildren();
217 while (propertyChild
)
219 if (propertyChild
->GetName() == wxT("property"))
221 wxString name
= propertyChild
->GetAttribute(wxT("name"), wxEmptyString
);
222 wxString value
= propertyChild
->GetAttribute(wxT("value"), wxEmptyString
);
223 wxString type
= propertyChild
->GetAttribute(wxT("type"), wxEmptyString
);
225 wxVariant var
= MakePropertyFromString(name
, value
, type
);
228 obj
->GetProperties().SetProperty(var
);
231 propertyChild
= propertyChild
->GetNext();
234 child
= child
->GetNext();
239 bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet
* sheet
, wxXmlNode
* node
)
241 wxString styleType
= node
->GetName();
242 wxString styleName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
243 wxString baseStyleName
= node
->GetAttribute(wxT("basestyle"), wxEmptyString
);
245 if (styleName
.empty())
248 if (styleType
== wxT("characterstyle"))
250 wxRichTextCharacterStyleDefinition
* def
= new wxRichTextCharacterStyleDefinition(styleName
);
251 def
->SetBaseStyle(baseStyleName
);
253 wxXmlNode
* child
= node
->GetChildren();
256 if (child
->GetName() == wxT("style"))
259 ImportStyle(attr
, child
, false);
262 child
= child
->GetNext();
265 sheet
->AddCharacterStyle(def
);
267 else if (styleType
== wxT("paragraphstyle"))
269 wxRichTextParagraphStyleDefinition
* def
= new wxRichTextParagraphStyleDefinition(styleName
);
271 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
272 def
->SetNextStyle(nextStyleName
);
273 def
->SetBaseStyle(baseStyleName
);
275 wxXmlNode
* child
= node
->GetChildren();
278 if (child
->GetName() == wxT("style"))
281 ImportStyle(attr
, child
, true);
284 child
= child
->GetNext();
287 sheet
->AddParagraphStyle(def
);
289 else if (styleType
== wxT("boxstyle"))
291 wxRichTextBoxStyleDefinition
* def
= new wxRichTextBoxStyleDefinition(styleName
);
293 def
->SetBaseStyle(baseStyleName
);
295 wxXmlNode
* child
= node
->GetChildren();
298 if (child
->GetName() == wxT("style"))
301 ImportStyle(attr
, child
, true);
304 child
= child
->GetNext();
307 sheet
->AddBoxStyle(def
);
309 else if (styleType
== wxT("liststyle"))
311 wxRichTextListStyleDefinition
* def
= new wxRichTextListStyleDefinition(styleName
);
313 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
314 def
->SetNextStyle(nextStyleName
);
315 def
->SetBaseStyle(baseStyleName
);
317 wxXmlNode
* child
= node
->GetChildren();
320 if (child
->GetName() == wxT("style"))
323 ImportStyle(attr
, child
, true);
325 wxString styleLevel
= child
->GetAttribute(wxT("level"), wxEmptyString
);
326 if (styleLevel
.empty())
332 int level
= wxAtoi(styleLevel
);
333 if (level
> 0 && level
<= 10)
335 def
->SetLevelAttributes(level
-1, attr
);
339 child
= child
->GetNext();
342 sheet
->AddListStyle(def
);
348 //-----------------------------------------------------------------------------
349 // xml support routines
350 //-----------------------------------------------------------------------------
352 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
354 return (GetParamNode(node
, param
) != NULL
);
357 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
359 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
361 wxXmlNode
*n
= node
->GetChildren();
365 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
373 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
376 if (n
== NULL
) return wxEmptyString
;
377 n
= n
->GetChildren();
381 if (n
->GetType() == wxXML_TEXT_NODE
||
382 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
383 return n
->GetContent();
386 return wxEmptyString
;
390 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
393 return GetNodeContent(node
);
395 return GetNodeContent(GetParamNode(node
, param
));
398 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
400 wxXmlNode
*parNode
= GetParamNode(node
, param
);
403 wxString
str1(GetNodeContent(parNode
));
407 wxXmlNode
* wxRichTextXMLHandler::FindNode(wxXmlNode
* node
, const wxString
& name
)
409 wxXmlNode
* child
= node
->GetChildren();
412 if (child
->GetName() == name
)
414 child
= child
->GetNext();
419 // For use with earlier versions of wxWidgets
420 #ifndef WXUNUSED_IN_UNICODE
422 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
424 #define WXUNUSED_IN_UNICODE(x) x
428 // write string to output
429 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
430 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
), wxMBConv
*convFile
)
432 if (str
.empty()) return;
436 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
437 stream
.Write((const char*)buf
, strlen((const char*)buf
));
441 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
442 stream
.Write((const char*)buf
, strlen((const char*)buf
));
445 if ( convFile
== NULL
)
446 stream
.Write(str
.mb_str(), str
.Len());
449 wxString
str2(str
.wc_str(*convMem
), *convFile
);
450 stream
.Write(str2
.mb_str(), str2
.Len());
455 static void OutputIndentation(wxOutputStream
& stream
, int indent
)
457 wxString str
= wxT("\n");
458 for (int i
= 0; i
< indent
; i
++)
459 str
<< wxT(' ') << wxT(' ');
460 ::OutputString(stream
, str
, NULL
, NULL
);
463 // Same as above, but create entities first.
464 // Translates '<' to "<", '>' to ">" and '&' to "&"
465 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
466 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
474 for (i
= 0; i
< len
; i
++)
478 // Original code excluded "&" but we _do_ want to convert
479 // the ampersand beginning & because otherwise when read in,
480 // the original "&" becomes "&".
482 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
483 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
485 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
489 OutputString(stream
, wxT("<"), NULL
, NULL
);
492 OutputString(stream
, wxT(">"), NULL
, NULL
);
495 OutputString(stream
, wxT("&"), NULL
, NULL
);
498 OutputString(stream
, wxT("""), NULL
, NULL
);
504 else if (wxUChar(c
) > 127)
506 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
508 wxString
s(wxT("&#"));
512 s
<< (int) wxUChar(c
);
515 OutputString(stream
, s
, NULL
, NULL
);
519 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
522 void wxRichTextXMLHandler::OutputString(wxOutputStream
& stream
, const wxString
& str
)
524 ::OutputString(stream
, str
, m_convMem
, m_convFile
);
527 void wxRichTextXMLHandler::OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
)
529 ::OutputStringEnt(stream
, str
, m_convMem
, m_convFile
);
532 void wxRichTextXMLHandler::OutputIndentation(wxOutputStream
& stream
, int indent
)
534 wxString str
= wxT("\n");
535 for (int i
= 0; i
< indent
; i
++)
536 str
<< wxT(' ') << wxT(' ');
537 ::OutputString(stream
, str
, NULL
, NULL
);
540 wxString
wxRichTextXMLHandler::AttributeToXML(const wxString
& str
)
548 for (i
= 0; i
< len
; i
++)
552 // Original code excluded "&" but we _do_ want to convert
553 // the ampersand beginning & because otherwise when read in,
554 // the original "&" becomes "&".
556 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
557 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
559 str1
+= str
.Mid(last
, i
- last
);
569 str1
+= wxT("&");
572 str1
+= wxT(""");
578 else if (wxUChar(c
) > 127)
580 str1
+= str
.Mid(last
, i
- last
);
582 wxString
s(wxT("&#"));
586 s
<< (int) wxUChar(c
);
593 str1
+= str
.Mid(last
, i
- last
);
597 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
599 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const int& v
)
601 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%d"), v
) << wxT("\"");
604 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const long& v
)
606 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%ld"), v
) << wxT("\"");
609 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const double& v
)
611 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%.2f"), (float) v
) << wxT("\"");
614 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxChar
* s
)
616 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
619 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxString
& s
)
621 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
624 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxColour
& col
)
626 str
<< wxT(" ") << name
<< wxT("=\"") << wxT("#") << ColourToHexString(col
) << wxT("\"");
629 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxTextAttrDimension
& dim
)
633 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString((int) dim
.GetFlags());
634 str
<< wxT(" ") << name
<< wxT("=\"");
640 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
642 if (dims
.GetLeft().IsValid())
643 AddAttribute(str
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
644 if (dims
.GetRight().IsValid())
645 AddAttribute(str
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
646 if (dims
.GetTop().IsValid())
647 AddAttribute(str
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
648 if (dims
.GetBottom().IsValid())
649 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
652 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
654 if (border
.HasStyle())
655 AddAttribute(str
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
656 if (border
.HasColour())
657 AddAttribute(str
, rootName
+ wxString(wxT("-color")), border
.GetColour());
658 if (border
.HasWidth())
659 AddAttribute(str
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
662 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
664 AddAttribute(str
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
665 AddAttribute(str
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
666 AddAttribute(str
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
667 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
671 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
673 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
675 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const int& v
)
677 node
->AddAttribute(name
, MakeString(v
));
680 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const long& v
)
682 node
->AddAttribute(name
, MakeString(v
));
685 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const double& v
)
687 node
->AddAttribute(name
, MakeString(v
));
690 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxString
& s
)
692 node
->AddAttribute(name
, s
);
695 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxColour
& col
)
697 node
->AddAttribute(name
, MakeString(col
));
700 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxTextAttrDimension
& dim
)
704 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString(dim
.GetFlags());
705 AddAttribute(node
, name
, value
);
709 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
711 if (dims
.GetLeft().IsValid())
712 AddAttribute(node
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
713 if (dims
.GetRight().IsValid())
714 AddAttribute(node
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
715 if (dims
.GetTop().IsValid())
716 AddAttribute(node
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
717 if (dims
.GetBottom().IsValid())
718 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
721 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
723 if (border
.HasStyle())
724 AddAttribute(node
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
725 if (border
.HasColour())
726 AddAttribute(node
, rootName
+ wxString(wxT("-color")), border
.GetColour());
727 if (border
.HasWidth())
728 AddAttribute(node
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
731 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
733 AddAttribute(node
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
734 AddAttribute(node
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
735 AddAttribute(node
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
736 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
739 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
741 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
746 wxString
version(wxT("1.0") ) ;
748 bool deleteConvFile
= false;
749 wxString fileEncoding
;
750 //wxMBConv* convFile = NULL;
753 fileEncoding
= wxT("UTF-8");
754 m_convFile
= & wxConvUTF8
;
756 fileEncoding
= wxT("ISO-8859-1");
757 m_convFile
= & wxConvISO8859_1
;
760 // If SetEncoding has been called, change the output encoding.
761 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
763 if (m_encoding
== wxT("<System>"))
766 fileEncoding
= wxLocale::GetSystemEncodingName();
767 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
772 fileEncoding
= m_encoding
;
775 // GetSystemEncodingName may not have returned a name
776 if (fileEncoding
.empty())
778 fileEncoding
= wxT("UTF-8");
780 fileEncoding
= wxT("ISO-8859-1");
782 m_convFile
= new wxCSConv(fileEncoding
);
783 deleteConvFile
= true;
786 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT
787 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
788 wxStopWatch stopwatch
;
790 wxXmlDocument
* doc
= new wxXmlDocument
;
791 doc
->SetFileEncoding(fileEncoding
);
793 wxXmlNode
* rootNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("richtext"));
794 doc
->SetRoot(rootNode
);
795 rootNode
->AddAttribute(wxT("version"), wxT("1.0.0.0"));
796 rootNode
->AddAttribute(wxT("xmlns"), wxT("http://www.wxwidgets.org"));
798 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
800 wxXmlNode
* styleSheetNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("stylesheet"));
801 rootNode
->AddChild(styleSheetNode
);
803 wxString nameAndDescr
;
805 if (!buffer
->GetStyleSheet()->GetName().empty())
806 styleSheetNode
->AddAttribute(wxT("name"), buffer
->GetStyleSheet()->GetName());
808 if (!buffer
->GetStyleSheet()->GetDescription().empty())
809 styleSheetNode
->AddAttribute(wxT("description"), buffer
->GetStyleSheet()->GetDescription());
812 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
814 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
815 ExportStyleDefinition(styleSheetNode
, def
);
818 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
820 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
821 ExportStyleDefinition(styleSheetNode
, def
);
824 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
826 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
827 ExportStyleDefinition(styleSheetNode
, def
);
830 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
832 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
833 ExportStyleDefinition(styleSheetNode
, def
);
836 bool success
= ExportXML(rootNode
, *buffer
);
837 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
838 long t
= stopwatch
.Time();
839 wxLogDebug(wxT("Creating the document took %ldms"), t
);
840 wxMessageBox(wxString::Format(wxT("Creating the document took %ldms"), t
));
844 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
847 success
= doc
->Save(stream
);
848 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
850 wxLogDebug(wxT("Save() took %ldms"), t2
);
851 wxMessageBox(wxString::Format(wxT("Save() took %ldms"), t2
));
858 // !(wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT)
861 m_convMem
= wxConvCurrent
;
867 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
868 version
, fileEncoding
);
869 OutputString(stream
, s
);
870 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">"));
874 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
876 OutputIndentation(stream
, level
);
877 wxString nameAndDescr
;
878 if (!buffer
->GetStyleSheet()->GetName().empty())
879 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
880 if (!buffer
->GetStyleSheet()->GetDescription().empty())
881 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
882 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"));
886 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
888 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
889 ExportStyleDefinition(stream
, def
, level
+ 1);
892 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
894 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
895 ExportStyleDefinition(stream
, def
, level
+ 1);
898 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
900 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
901 ExportStyleDefinition(stream
, def
, level
+ 1);
904 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
906 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
907 ExportStyleDefinition(stream
, def
, level
+ 1);
910 OutputIndentation(stream
, level
);
911 OutputString(stream
, wxT("</stylesheet>"));
915 bool success
= ExportXML(stream
, *buffer
, level
);
917 OutputString(stream
, wxT("\n</richtext>"));
918 OutputString(stream
, wxT("\n"));
929 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
931 /// Recursively export an object
932 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxRichTextObject
& obj
, int indent
)
934 obj
.ExportXML(stream
, indent
, this);
939 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxRichTextStyleDefinition
* def
, int level
)
941 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
942 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
943 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
944 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
946 wxString baseStyle
= def
->GetBaseStyle();
947 wxString baseStyleProp
;
948 if (!baseStyle
.empty())
949 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
951 wxString descr
= def
->GetDescription();
954 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
958 OutputIndentation(stream
, level
);
959 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
963 wxString style
= AddAttributes(def
->GetStyle(), false);
965 OutputIndentation(stream
, level
);
966 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
968 OutputIndentation(stream
, level
);
969 OutputString(stream
, wxT("</style>"));
973 OutputIndentation(stream
, level
);
974 OutputString(stream
, wxT("</characterstyle>"));
978 OutputIndentation(stream
, level
);
980 if (!listDef
->GetNextStyle().empty())
981 baseStyleProp
<< wxT(" nextstyle=\"") << listDef
->GetNextStyle() << wxT("\"");
983 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"));
987 wxString style
= AddAttributes(def
->GetStyle(), true);
989 OutputIndentation(stream
, level
);
990 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
992 OutputIndentation(stream
, level
);
993 OutputString(stream
, wxT("</style>"));
996 for (i
= 0; i
< 10; i
++)
998 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1001 wxString style
= AddAttributes(def
->GetStyle(), true);
1002 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
1004 OutputIndentation(stream
, level
);
1005 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"));
1007 OutputIndentation(stream
, level
);
1008 OutputString(stream
, wxT("</style>"));
1014 OutputIndentation(stream
, level
);
1015 OutputString(stream
, wxT("</liststyle>"));
1019 OutputIndentation(stream
, level
);
1021 if (!paraDef
->GetNextStyle().empty())
1022 baseStyleProp
<< wxT(" nextstyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
1024 OutputString(stream
, wxT("<paragraphstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
1028 wxString style
= AddAttributes(def
->GetStyle(), true);
1030 OutputIndentation(stream
, level
);
1031 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1033 OutputIndentation(stream
, level
);
1034 OutputString(stream
, wxT("</style>"));
1038 OutputIndentation(stream
, level
);
1039 OutputString(stream
, wxT("</paragraphstyle>"));
1043 OutputIndentation(stream
, level
);
1045 OutputString(stream
, wxT("<boxstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
1049 wxString style
= AddAttributes(def
->GetStyle(), true);
1051 OutputIndentation(stream
, level
);
1052 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1054 OutputIndentation(stream
, level
);
1055 OutputString(stream
, wxT("</style>"));
1059 OutputIndentation(stream
, level
);
1060 OutputString(stream
, wxT("</boxstyle>"));
1067 /// Create a string containing style attributes
1068 wxString
wxRichTextXMLHandler::AddAttributes(const wxRichTextAttr
& attr
, bool isPara
)
1071 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
1072 AddAttribute(str
, wxT("textcolor"), attr
.GetTextColour());
1074 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
1075 AddAttribute(str
, wxT("bgcolor"), attr
.GetBackgroundColour());
1077 if (attr
.HasFontSize())
1078 AddAttribute(str
, wxT("fontsize"), attr
.GetFontSize());
1080 if (attr
.HasFontFamily())
1081 AddAttribute(str
, wxT("fontfamily"), attr
.GetFontFamily());
1083 if (attr
.HasFontItalic())
1084 AddAttribute(str
, wxT("fontstyle"), attr
.GetFontStyle());
1086 if (attr
.HasFontWeight())
1087 AddAttribute(str
, wxT("fontweight"), attr
.GetFontWeight());
1089 if (attr
.HasFontUnderlined())
1090 AddAttribute(str
, wxT("fontunderlined"), (int) attr
.GetFontUnderlined());
1092 if (attr
.HasFontFaceName())
1093 AddAttribute(str
, wxT("fontface"), attr
.GetFontFaceName());
1095 if (attr
.HasTextEffects())
1097 AddAttribute(str
, wxT("texteffects"), attr
.GetTextEffects());
1098 AddAttribute(str
, wxT("texteffectflags"), attr
.GetTextEffectFlags());
1101 if (!attr
.GetCharacterStyleName().empty())
1102 AddAttribute(str
, wxT("characterstyle"), attr
.GetCharacterStyleName());
1105 AddAttribute(str
, wxT("url"), AttributeToXML(attr
.GetURL()));
1109 if (attr
.HasAlignment())
1110 AddAttribute(str
, wxT("alignment"), (int) attr
.GetAlignment());
1112 if (attr
.HasLeftIndent())
1114 AddAttribute(str
, wxT("leftindent"), (int) attr
.GetLeftIndent());
1115 AddAttribute(str
, wxT("leftsubindent"), (int) attr
.GetLeftSubIndent());
1118 if (attr
.HasRightIndent())
1119 AddAttribute(str
, wxT("rightindent"), (int) attr
.GetRightIndent());
1121 if (attr
.HasParagraphSpacingAfter())
1122 AddAttribute(str
, wxT("parspacingafter"), (int) attr
.GetParagraphSpacingAfter());
1124 if (attr
.HasParagraphSpacingBefore())
1125 AddAttribute(str
, wxT("parspacingbefore"), (int) attr
.GetParagraphSpacingBefore());
1127 if (attr
.HasLineSpacing())
1128 AddAttribute(str
, wxT("linespacing"), (int) attr
.GetLineSpacing());
1130 if (attr
.HasBulletStyle())
1131 AddAttribute(str
, wxT("bulletstyle"), (int) attr
.GetBulletStyle());
1133 if (attr
.HasBulletNumber())
1134 AddAttribute(str
, wxT("bulletnumber"), (int) attr
.GetBulletNumber());
1136 if (attr
.HasBulletText())
1138 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1139 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1140 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1141 AddAttribute(str
, wxT("bulletsymbol"), (int) (attr
.GetBulletText()[0]));
1143 AddAttribute(str
, wxT("bullettext"), attr
.GetBulletText());
1145 AddAttribute(str
, wxT("bulletfont"), attr
.GetBulletFont());
1148 if (attr
.HasBulletName())
1149 AddAttribute(str
, wxT("bulletname"), attr
.GetBulletName());
1151 if (!attr
.GetParagraphStyleName().empty())
1152 AddAttribute(str
, wxT("parstyle"), attr
.GetParagraphStyleName());
1154 if (!attr
.GetListStyleName().empty())
1155 AddAttribute(str
, wxT("liststyle"), attr
.GetListStyleName());
1161 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1163 if (i
> 0) strTabs
<< wxT(",");
1164 strTabs
<< attr
.GetTabs()[i
];
1166 AddAttribute(str
, wxT("tabs"), strTabs
);
1169 if (attr
.HasPageBreak())
1171 AddAttribute(str
, wxT("pagebreak"), 1);
1174 if (attr
.HasOutlineLevel())
1175 AddAttribute(str
, wxT("outlinelevel"), (int) attr
.GetOutlineLevel());
1178 AddAttribute(str
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1179 AddAttribute(str
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1180 AddAttribute(str
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1181 AddAttribute(str
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1182 AddAttribute(str
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1183 AddAttribute(str
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1184 AddAttribute(str
, wxT("height"), attr
.GetTextBoxAttr().GetWidth());
1186 if (attr
.GetTextBoxAttr().HasFloatMode())
1189 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1190 value
= wxT("left");
1191 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1192 value
= wxT("right");
1194 value
= wxT("none");
1195 AddAttribute(str
, wxT("float"), value
);
1198 if (attr
.GetTextBoxAttr().HasClearMode())
1201 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1202 value
= wxT("left");
1203 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1204 value
= wxT("right");
1205 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1206 value
= wxT("both");
1208 value
= wxT("none");
1209 AddAttribute(str
, wxT("clear"), value
);
1212 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1213 AddAttribute(str
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1218 // Make a string from the given property. This can be overridden for custom variants.
1219 wxString
wxRichTextXMLHandler::MakeStringFromProperty(const wxVariant
& var
)
1221 return var
.MakeString();
1224 // Create a proprty from the string read from the XML file.
1225 wxVariant
wxRichTextXMLHandler::MakePropertyFromString(const wxString
& name
, const wxString
& value
, const wxString
& WXUNUSED(type
))
1227 wxVariant
var(value
, name
);
1228 // TODO: use type to create using common types
1232 // Write the properties
1233 bool wxRichTextXMLHandler::WriteProperties(wxOutputStream
& stream
, const wxRichTextProperties
& properties
, int level
)
1235 if (properties
.GetCount() > 0)
1239 OutputIndentation(stream
, level
);
1240 OutputString(stream
, wxT("<properties"));
1245 for (i
= 0; i
< properties
.GetCount(); i
++)
1247 const wxVariant
& var
= properties
[i
];
1250 const wxString
& name
= var
.GetName();
1251 wxString value
= MakeStringFromProperty(var
);
1253 OutputIndentation(stream
, level
);
1254 OutputString(stream
, wxT("<property name=\"") + name
+
1255 wxT("\" type=\"") + var
.GetType() + wxT("\" value=\""));
1256 OutputStringEnt(stream
, value
);
1257 OutputString(stream
, wxT("\"/>\n"));
1263 OutputIndentation(stream
, level
);
1264 OutputString(stream
, wxT("</properties>\n"));
1274 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
1276 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1277 bool wxRichTextXMLHandler::ExportXML(wxXmlNode
* parent
, wxRichTextObject
& obj
)
1279 obj
.ExportXML(parent
, this);
1284 bool wxRichTextXMLHandler::ExportStyleDefinition(wxXmlNode
* parent
, wxRichTextStyleDefinition
* def
)
1286 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
1287 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
1288 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
1289 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
1291 wxString baseStyle
= def
->GetBaseStyle();
1292 wxString descr
= def
->GetDescription();
1294 wxXmlNode
* defNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxEmptyString
);
1295 parent
->AddChild(defNode
);
1296 if (!baseStyle
.empty())
1297 defNode
->AddAttribute(wxT("basestyle"), baseStyle
);
1299 defNode
->AddAttribute(wxT("description"), descr
);
1301 wxXmlNode
* styleNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1302 defNode
->AddChild(styleNode
);
1306 defNode
->SetName(wxT("characterstyle"));
1307 AddAttributes(styleNode
, def
->GetStyle(), false);
1311 defNode
->SetName(wxT("liststyle"));
1313 if (!listDef
->GetNextStyle().empty())
1314 defNode
->AddAttribute(wxT("nextstyle"), listDef
->GetNextStyle());
1316 AddAttributes(styleNode
, def
->GetStyle(), true);
1319 for (i
= 0; i
< 10; i
++)
1321 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1324 wxXmlNode
* levelNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1325 defNode
->AddChild(levelNode
);
1326 levelNode
->AddAttribute(wxT("level"), MakeString(i
+1));
1327 AddAttributes(levelNode
, * levelAttr
, true);
1333 defNode
->SetName(wxT("boxstyle"));
1335 AddAttributes(styleNode
, def
->GetStyle(), true);
1339 defNode
->SetName(wxT("paragraphstyle"));
1341 if (!paraDef
->GetNextStyle().empty())
1342 defNode
->AddAttribute(wxT("nextstyle"), paraDef
->GetNextStyle());
1344 AddAttributes(styleNode
, def
->GetStyle(), true);
1350 bool wxRichTextXMLHandler::AddAttributes(wxXmlNode
* node
, wxRichTextAttr
& attr
, bool isPara
)
1352 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
1353 node
->AddAttribute(wxT("textcolor"), MakeString(attr
.GetTextColour()));
1354 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
1355 node
->AddAttribute(wxT("bgcolor"), MakeString(attr
.GetBackgroundColour()));
1357 if (attr
.HasFontSize())
1358 node
->AddAttribute(wxT("fontsize"), MakeString(attr
.GetFontSize()));
1359 if (attr
.HasFontFamily())
1360 node
->AddAttribute(wxT("fontfamily"), MakeString(attr
.GetFontFamily()));
1361 if (attr
.HasFontItalic())
1362 node
->AddAttribute(wxT("fontstyle"), MakeString(attr
.GetFontStyle()));
1363 if (attr
.HasFontWeight())
1364 node
->AddAttribute(wxT("fontweight"), MakeString(attr
.GetFontWeight()));
1365 if (attr
.HasFontUnderlined())
1366 node
->AddAttribute(wxT("fontunderlined"), MakeString((int) attr
.GetFontUnderlined()));
1367 if (attr
.HasFontFaceName())
1368 node
->AddAttribute(wxT("fontface"), attr
.GetFontFaceName());
1370 if (attr
.HasTextEffects())
1372 node
->AddAttribute(wxT("texteffects"), MakeString(attr
.GetTextEffects()));
1373 node
->AddAttribute(wxT("texteffectflags"), MakeString(attr
.GetTextEffectFlags()));
1375 if (attr
.HasCharacterStyleName() && !attr
.GetCharacterStyleName().empty())
1376 node
->AddAttribute(wxT("characterstyle"), attr
.GetCharacterStyleName());
1379 node
->AddAttribute(wxT("url"), attr
.GetURL()); // TODO: do we need to wrap this in AttributeToXML?
1383 if (attr
.HasAlignment())
1384 node
->AddAttribute(wxT("alignment"), MakeString((int) attr
.GetAlignment()));
1386 if (attr
.HasLeftIndent())
1388 node
->AddAttribute(wxT("leftindent"), MakeString((int) attr
.GetLeftIndent()));
1389 node
->AddAttribute(wxT("leftsubindent"), MakeString((int) attr
.GetLeftSubIndent()));
1392 if (attr
.HasRightIndent())
1393 node
->AddAttribute(wxT("rightindent"), MakeString((int) attr
.GetRightIndent()));
1395 if (attr
.HasParagraphSpacingAfter())
1396 node
->AddAttribute(wxT("parspacingafter"), MakeString((int) attr
.GetParagraphSpacingAfter()));
1398 if (attr
.HasParagraphSpacingBefore())
1399 node
->AddAttribute(wxT("parspacingbefore"), MakeString((int) attr
.GetParagraphSpacingBefore()));
1401 if (attr
.HasLineSpacing())
1402 node
->AddAttribute(wxT("linespacing"), MakeString((int) attr
.GetLineSpacing()));
1404 if (attr
.HasBulletStyle())
1405 node
->AddAttribute(wxT("bulletstyle"), MakeString((int) attr
.GetBulletStyle()));
1407 if (attr
.HasBulletNumber())
1408 node
->AddAttribute(wxT("bulletnumber"), MakeString((int) attr
.GetBulletNumber()));
1410 if (attr
.HasBulletText())
1412 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1413 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1414 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1415 node
->AddAttribute(wxT("bulletsymbol"), MakeString((int) (attr
.GetBulletText()[0])));
1417 node
->AddAttribute(wxT("bullettext"), attr
.GetBulletText());
1419 if (!attr
.GetBulletFont().empty())
1420 node
->AddAttribute(wxT("bulletfont"), attr
.GetBulletFont());
1423 if (attr
.HasBulletName())
1424 node
->AddAttribute(wxT("bulletname"), attr
.GetBulletName());
1426 if (!attr
.GetParagraphStyleName().empty())
1427 node
->AddAttribute(wxT("parstyle"), attr
.GetParagraphStyleName());
1429 if (!attr
.GetListStyleName().empty())
1430 node
->AddAttribute(wxT("liststyle"), attr
.GetListStyleName());
1436 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1440 tabs
<< attr
.GetTabs()[i
];
1442 node
->AddAttribute(wxT("tabs"), tabs
);
1445 if (attr
.HasPageBreak())
1446 node
->AddAttribute(wxT("pagebreak"), wxT("1"));
1448 if (attr
.HasOutlineLevel())
1449 node
->AddAttribute(wxT("outlinelevel"), MakeString((int) attr
.GetOutlineLevel()));
1452 AddAttribute(node
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1453 AddAttribute(node
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1454 AddAttribute(node
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1455 AddAttribute(node
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1456 AddAttribute(node
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1457 AddAttribute(node
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1458 AddAttribute(node
, wxT("height"), attr
.GetTextBoxAttr().GetWidth());
1460 if (attr
.GetTextBoxAttr().HasFloatMode())
1463 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1464 value
= wxT("left");
1465 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1466 value
= wxT("right");
1468 value
= wxT("none");
1469 AddAttribute(node
, wxT("float"), value
);
1472 if (attr
.GetTextBoxAttr().HasClearMode())
1475 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1476 value
= wxT("left");
1477 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1478 value
= wxT("right");
1479 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1480 value
= wxT("both");
1482 value
= wxT("none");
1483 AddAttribute(node
, wxT("clear"), value
);
1486 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1487 AddAttribute(node
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1492 bool wxRichTextXMLHandler::WriteProperties(wxXmlNode
* node
, const wxRichTextProperties
& properties
)
1494 if (properties
.GetCount() > 0)
1496 wxXmlNode
* propertiesNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("properties"));
1497 node
->AddChild(propertiesNode
);
1499 for (i
= 0; i
< properties
.GetCount(); i
++)
1501 const wxVariant
& var
= properties
[i
];
1504 wxXmlNode
* propertyNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("property"));
1505 propertiesNode
->AddChild(propertyNode
);
1507 const wxString
& name
= var
.GetName();
1508 wxString value
= MakeStringFromProperty(var
);
1510 AddAttribute(propertyNode
, wxT("name"), name
);
1511 AddAttribute(propertyNode
, wxT("type"), var
.GetType());
1512 AddAttribute(propertyNode
, wxT("value"), value
);
1520 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1522 /// Replace face name with current name for platform.
1523 /// TODO: introduce a virtual function or settable table to
1524 /// do this comprehensively.
1525 bool wxRichTextFixFaceName(wxString
& facename
)
1527 if (facename
.empty())
1531 if (facename
== wxT("Times"))
1533 facename
= wxT("Times New Roman");
1536 else if (facename
== wxT("Helvetica"))
1538 facename
= wxT("Arial");
1541 else if (facename
== wxT("Courier"))
1543 facename
= wxT("Courier New");
1549 if (facename
== wxT("Times New Roman"))
1551 facename
= wxT("Times");
1554 else if (facename
== wxT("Arial"))
1556 facename
= wxT("Helvetica");
1559 else if (facename
== wxT("Courier New"))
1561 facename
= wxT("Courier");
1569 static inline long wxRichTextColourStringToLong(const wxString
& colStr
)
1571 if (!colStr
.IsEmpty())
1573 wxColour
col(colStr
);
1574 return col
.GetRGB();
1580 static inline wxTextAttrDimension
wxRichTextParseDimension(const wxString
& dimStr
)
1582 wxString valuePart
= dimStr
.BeforeFirst(wxT(','));
1584 if (dimStr
.Contains(wxT(",")))
1585 flagsPart
= dimStr
.AfterFirst(wxT(','));
1586 wxTextAttrDimension dim
;
1587 dim
.SetValue(wxAtoi(valuePart
));
1588 dim
.SetFlags(wxAtoi(flagsPart
));
1593 /// Import style parameters
1594 bool wxRichTextXMLHandler::ImportStyle(wxRichTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1596 wxXmlAttribute
* xmlAttr
= node
->GetAttributes();
1600 const wxString
& name
= xmlAttr
->GetName();
1601 const wxString
& value
= xmlAttr
->GetValue();
1604 if (name
== wxT("fontface"))
1609 if (GetFlags() & wxRICHTEXT_HANDLER_CONVERT_FACENAMES
)
1610 wxRichTextFixFaceName(v
);
1611 attr
.SetFontFaceName(v
);
1614 else if (name
== wxT("fontfamily"))
1617 attr
.SetFontFamily((wxFontFamily
)wxAtoi(value
));
1619 else if (name
== wxT("fontstyle"))
1622 attr
.SetFontStyle((wxFontStyle
)wxAtoi(value
));
1624 else if (name
== wxT("fontsize"))
1627 attr
.SetFontSize(wxAtoi(value
));
1629 else if (name
== wxT("fontweight"))
1632 attr
.SetFontWeight((wxFontWeight
) wxAtoi(value
));
1634 else if (name
== wxT("fontunderlined"))
1637 attr
.SetFontUnderlined(wxAtoi(value
) != 0);
1639 else if (name
== wxT("textcolor"))
1643 if (value
[0] == wxT('#'))
1644 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1646 attr
.SetTextColour(value
);
1649 else if (name
== wxT("bgcolor"))
1653 if (value
[0] == wxT('#'))
1654 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1656 attr
.SetBackgroundColour(value
);
1659 else if (name
== wxT("characterstyle"))
1662 attr
.SetCharacterStyleName(value
);
1664 else if (name
== wxT("texteffects"))
1667 attr
.SetTextEffects(wxAtoi(value
));
1669 else if (name
== wxT("texteffectflags"))
1672 attr
.SetTextEffectFlags(wxAtoi(value
));
1674 else if (name
== wxT("url"))
1681 if (name
== wxT("alignment"))
1684 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1686 else if (name
== wxT("leftindent"))
1689 attr
.SetLeftIndent(wxAtoi(value
), attr
.GetLeftSubIndent());
1691 else if (name
== wxT("leftsubindent"))
1694 attr
.SetLeftIndent(attr
.GetLeftIndent(), wxAtoi(value
));
1696 else if (name
== wxT("rightindent"))
1699 attr
.SetRightIndent(wxAtoi(value
));
1701 else if (name
== wxT("parspacingbefore"))
1704 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1706 else if (name
== wxT("parspacingafter"))
1709 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1711 else if (name
== wxT("linespacing"))
1714 attr
.SetLineSpacing(wxAtoi(value
));
1716 else if (name
== wxT("bulletstyle"))
1719 attr
.SetBulletStyle(wxAtoi(value
));
1721 else if (name
== wxT("bulletnumber"))
1724 attr
.SetBulletNumber(wxAtoi(value
));
1726 else if (name
== wxT("bulletsymbol"))
1730 wxChar ch
= wxAtoi(value
);
1733 attr
.SetBulletText(s
);
1736 else if (name
== wxT("bullettext"))
1740 attr
.SetBulletText(value
);
1743 else if (name
== wxT("bulletfont"))
1747 attr
.SetBulletFont(value
);
1750 else if (name
== wxT("bulletname"))
1754 attr
.SetBulletName(value
);
1757 else if (name
== wxT("parstyle"))
1761 attr
.SetParagraphStyleName(value
);
1764 else if (name
== wxT("liststyle"))
1768 attr
.SetListStyleName(value
);
1771 else if (name
== wxT("tabs"))
1776 wxStringTokenizer
tkz(value
, wxT(","));
1777 while (tkz
.HasMoreTokens())
1779 wxString token
= tkz
.GetNextToken();
1780 tabs
.Add(wxAtoi(token
));
1785 else if (name
== wxT("pagebreak"))
1789 attr
.SetPageBreak(wxAtoi(value
) != 0);
1792 else if (name
== wxT("outlinelevel"))
1796 attr
.SetOutlineLevel(wxAtoi(value
));
1809 if (name
== wxT("width"))
1811 attr
.GetTextBoxAttr().GetWidth().SetValue(wxRichTextParseDimension(value
));
1813 else if (name
== wxT("height"))
1815 attr
.GetTextBoxAttr().GetHeight().SetValue(wxRichTextParseDimension(value
));
1818 else if (name
== wxT("float"))
1820 if (value
== wxT("left"))
1821 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT
);
1822 else if (value
== wxT("right"))
1823 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT
);
1824 else if (value
== wxT("none"))
1825 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_NONE
);
1827 else if (name
== wxT("clear"))
1829 if (value
== wxT("left"))
1830 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_LEFT
);
1831 else if (value
== wxT("right"))
1832 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_RIGHT
);
1833 else if (value
== wxT("both"))
1834 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_BOTH
);
1835 else if (value
== wxT("none"))
1836 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_NONE
);
1838 else if (name
== wxT("collapse-borders"))
1839 attr
.GetTextBoxAttr().SetCollapseBorders((wxTextBoxAttrCollapseMode
) wxAtoi(value
));
1841 else if (name
.Contains(wxT("border-")))
1843 if (name
== wxT("border-left-style"))
1844 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetStyle(wxAtoi(value
));
1845 else if (name
== wxT("border-right-style"))
1846 attr
.GetTextBoxAttr().GetBorder().GetRight().SetStyle(wxAtoi(value
));
1847 else if (name
== wxT("border-top-style"))
1848 attr
.GetTextBoxAttr().GetBorder().GetTop().SetStyle(wxAtoi(value
));
1849 else if (name
== wxT("border-bottom-style"))
1850 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetStyle(wxAtoi(value
));
1852 else if (name
== wxT("border-left-colour"))
1853 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1854 else if (name
== wxT("border-right-colour"))
1855 attr
.GetTextBoxAttr().GetBorder().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1856 else if (name
== wxT("border-top-colour"))
1857 attr
.GetTextBoxAttr().GetBorder().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1858 else if (name
== wxT("border-bottom-colour"))
1859 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1861 else if (name
== wxT("border-left-width"))
1862 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1863 else if (name
== wxT("border-right-width"))
1864 attr
.GetTextBoxAttr().GetBorder().GetRight().SetWidth(wxRichTextParseDimension(value
));
1865 else if (name
== wxT("border-top-width"))
1866 attr
.GetTextBoxAttr().GetBorder().GetTop().SetWidth(wxRichTextParseDimension(value
));
1867 else if (name
== wxT("border-bottom-width"))
1868 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1870 else if (name
.Contains(wxT("outline-")))
1872 if (name
== wxT("outline-left-style"))
1873 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetStyle(wxAtoi(value
));
1874 else if (name
== wxT("outline-right-style"))
1875 attr
.GetTextBoxAttr().GetOutline().GetRight().SetStyle(wxAtoi(value
));
1876 else if (name
== wxT("outline-top-style"))
1877 attr
.GetTextBoxAttr().GetOutline().GetTop().SetStyle(wxAtoi(value
));
1878 else if (name
== wxT("outline-bottom-style"))
1879 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetStyle(wxAtoi(value
));
1881 else if (name
== wxT("outline-left-colour"))
1882 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1883 else if (name
== wxT("outline-right-colour"))
1884 attr
.GetTextBoxAttr().GetOutline().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1885 else if (name
== wxT("outline-top-colour"))
1886 attr
.GetTextBoxAttr().GetOutline().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1887 else if (name
== wxT("outline-bottom-colour"))
1888 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1890 else if (name
== wxT("outline-left-width"))
1891 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1892 else if (name
== wxT("outline-right-width"))
1893 attr
.GetTextBoxAttr().GetOutline().GetRight().SetWidth(wxRichTextParseDimension(value
));
1894 else if (name
== wxT("outline-top-width"))
1895 attr
.GetTextBoxAttr().GetOutline().GetTop().SetWidth(wxRichTextParseDimension(value
));
1896 else if (name
== wxT("outline-bottom-width"))
1897 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1899 else if (name
.Contains(wxT("margin-")))
1901 if (name
== wxT("margin-left"))
1902 attr
.GetTextBoxAttr().GetMargins().GetLeft().SetValue(wxRichTextParseDimension(value
));
1903 else if (name
== wxT("margin-right"))
1904 attr
.GetTextBoxAttr().GetMargins().GetRight().SetValue(wxRichTextParseDimension(value
));
1905 else if (name
== wxT("margin-top"))
1906 attr
.GetTextBoxAttr().GetMargins().GetTop().SetValue(wxRichTextParseDimension(value
));
1907 else if (name
== wxT("margin-bottom"))
1908 attr
.GetTextBoxAttr().GetMargins().GetBottom().SetValue(wxRichTextParseDimension(value
));
1910 else if (name
.Contains(wxT("padding-")))
1912 if (name
== wxT("padding-left"))
1913 attr
.GetTextBoxAttr().GetPadding().GetLeft().SetValue(wxRichTextParseDimension(value
));
1914 else if (name
== wxT("padding-right"))
1915 attr
.GetTextBoxAttr().GetPadding().GetRight().SetValue(wxRichTextParseDimension(value
));
1916 else if (name
== wxT("padding-top"))
1917 attr
.GetTextBoxAttr().GetPadding().GetTop().SetValue(wxRichTextParseDimension(value
));
1918 else if (name
== wxT("padding-bottom"))
1919 attr
.GetTextBoxAttr().GetPadding().GetBottom().SetValue(wxRichTextParseDimension(value
));
1921 else if (name
.Contains(wxT("position-")))
1923 if (name
== wxT("position-left"))
1924 attr
.GetTextBoxAttr().GetPosition().GetLeft().SetValue(wxRichTextParseDimension(value
));
1925 else if (name
== wxT("position-right"))
1926 attr
.GetTextBoxAttr().GetPosition().GetRight().SetValue(wxRichTextParseDimension(value
));
1927 else if (name
== wxT("position-top"))
1928 attr
.GetTextBoxAttr().GetPosition().GetTop().SetValue(wxRichTextParseDimension(value
));
1929 else if (name
== wxT("position-bottom"))
1930 attr
.GetTextBoxAttr().GetPosition().GetBottom().SetValue(wxRichTextParseDimension(value
));
1934 xmlAttr
= xmlAttr
->GetNext();
1943 // Import this object from XML
1944 bool wxRichTextObject::ImportFromXML(wxRichTextBuffer
* WXUNUSED(buffer
), wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
1946 handler
->ImportProperties(this, node
);
1947 handler
->ImportStyle(GetAttributes(), node
, UsesParagraphAttributes());
1954 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
1955 // Export this object directly to the given stream.
1956 bool wxRichTextObject::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
1958 ::OutputIndentation(stream
, indent
);
1959 ::OutputString(stream
, wxT("<") + GetXMLNodeName(), handler
->GetConvMem(), handler
->GetConvFile());
1961 wxString style
= handler
->AddAttributes(GetAttributes(), true);
1963 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
1965 if (GetProperties().GetCount() > 0)
1967 handler
->WriteProperties(stream
, GetProperties(), indent
);
1970 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
1974 for (i
= 0; i
< composite
->GetChildCount(); i
++)
1976 wxRichTextObject
* child
= composite
->GetChild(i
);
1977 child
->ExportXML(stream
, indent
+1, handler
);
1981 ::OutputIndentation(stream
, indent
);
1982 ::OutputString(stream
, wxT("</") + GetXMLNodeName() + wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
1987 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1988 // Export this object to the given parent node, usually creating at least one child node.
1989 bool wxRichTextObject::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
1991 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
1992 parent
->AddChild(elementNode
);
1993 handler
->AddAttributes(elementNode
, GetAttributes(), true);
1994 handler
->WriteProperties(elementNode
, GetProperties());
1996 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
2000 for (i
= 0; i
< composite
->GetChildCount(); i
++)
2002 wxRichTextObject
* child
= composite
->GetChild(i
);
2003 child
->ExportXML(elementNode
, handler
);
2011 // Import this object from XML
2012 bool wxRichTextPlainText::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2014 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2016 if (node
->GetName() == wxT("text"))
2019 wxXmlNode
* textChild
= node
->GetChildren();
2022 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2023 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2025 wxString text2
= textChild
->GetContent();
2027 // Strip whitespace from end
2028 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
2029 text2
= text2
.Mid(0, text2
.length()-1);
2031 if (!text2
.empty() && text2
[0] == wxT('"'))
2032 text2
= text2
.Mid(1);
2033 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
2034 text2
= text2
.Mid(0, text2
.length() - 1);
2038 textChild
= textChild
->GetNext();
2043 else if (node
->GetName() == wxT("symbol"))
2045 // This is a symbol that XML can't read in the normal way
2047 wxXmlNode
* textChild
= node
->GetChildren();
2050 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2051 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2053 wxString text2
= textChild
->GetContent();
2056 textChild
= textChild
->GetNext();
2059 wxString actualText
;
2060 actualText
<< (wxChar
) wxAtoi(text
);
2061 SetText(actualText
);
2069 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2070 // Export this object directly to the given stream.
2071 bool wxRichTextPlainText::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2073 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2077 const wxString
& text
= GetText();
2078 int len
= (int) text
.Length();
2083 ::OutputIndentation(stream
, indent
);
2084 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2085 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2086 if (GetProperties().GetCount() > 0)
2088 handler
->WriteProperties(stream
, GetProperties(), indent
);
2089 ::OutputIndentation(stream
, indent
);
2091 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2093 else for (i
= 0; i
< len
; i
++)
2096 int c
= (int) text
[i
];
2098 int c
= (int) wxUChar(text
[i
]);
2100 if ((c
< 32 || c
== 34) && /* c != 9 && */ c
!= 10 && c
!= 13)
2104 wxString
fragment(text
.Mid(last
, i
-last
));
2105 if (!fragment
.empty())
2107 ::OutputIndentation(stream
, indent
);
2108 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2110 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2112 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2114 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2115 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2116 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2119 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2121 if (GetProperties().GetCount() > 0)
2123 handler
->WriteProperties(stream
, GetProperties(), indent
);
2124 ::OutputIndentation(stream
, indent
);
2126 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2131 // Output this character as a number in a separate tag, because XML can't cope
2132 // with entities below 32 except for 10 and 13
2134 ::OutputIndentation(stream
, indent
);
2135 ::OutputString(stream
, wxT("<symbol"), handler
->GetConvMem(), handler
->GetConvFile());
2137 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2138 ::OutputString(stream
, wxString::Format(wxT("%d"), c
), handler
->GetConvMem(), handler
->GetConvFile());
2140 if (GetProperties().GetCount() > 0)
2142 handler
->WriteProperties(stream
, GetProperties(), indent
);
2143 ::OutputIndentation(stream
, indent
);
2145 ::OutputString(stream
, wxT("</symbol>"), handler
->GetConvMem(), handler
->GetConvFile());
2153 fragment
= text
.Mid(last
, i
-last
);
2157 ::OutputIndentation(stream
, indent
);
2158 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2160 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2162 if (GetProperties().GetCount() > 0)
2164 handler
->WriteProperties(stream
, GetProperties(), indent
);
2165 ::OutputIndentation(stream
, indent
);
2168 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2170 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2171 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2172 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2175 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2177 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2183 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2184 // Export this object to the given parent node, usually creating at least one child node.
2185 bool wxRichTextPlainText::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2189 const wxString
& text
= GetText();
2190 int len
= (int) text
.Length();
2196 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2197 parent
->AddChild(elementNode
);
2199 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2200 handler
->WriteProperties(elementNode
, GetProperties());
2202 else for (i
= 0; i
< len
; i
++)
2205 int c
= (int) text
[i
];
2207 int c
= (int) wxUChar(text
[i
]);
2209 if ((c
< 32 || c
== 34) && c
!= 10 && c
!= 13)
2213 wxString
fragment(text
.Mid(last
, i
-last
));
2214 if (!fragment
.empty())
2216 // TODO: I'm assuming wxXmlDocument will output quotes if necessary
2217 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2218 parent
->AddChild(elementNode
);
2219 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2220 handler
->WriteProperties(elementNode
, GetProperties());
2222 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2223 elementNode
->AddChild(textNode
);
2225 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2226 fragment
= wxT("\"") + fragment
+ wxT("\"");
2228 textNode
->SetContent(fragment
);
2233 // Output this character as a number in a separate tag, because XML can't cope
2234 // with entities below 32 except for 10 and 13
2236 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("symbol"));
2237 parent
->AddChild(elementNode
);
2239 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2240 handler
->WriteProperties(elementNode
, GetProperties());
2242 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2243 elementNode
->AddChild(textNode
);
2244 textNode
->SetContent(wxString::Format(wxT("%d"), c
));
2254 fragment
= text
.Mid(last
, i
-last
);
2258 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2259 parent
->AddChild(elementNode
);
2260 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2262 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2263 elementNode
->AddChild(textNode
);
2265 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2266 fragment
= wxT("\"") + fragment
+ wxT("\"");
2268 textNode
->SetContent(fragment
);
2275 // Import this object from XML
2276 bool wxRichTextImage::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2278 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2280 wxBitmapType imageType
= wxBITMAP_TYPE_PNG
;
2281 wxString value
= node
->GetAttribute(wxT("imagetype"), wxEmptyString
);
2284 int type
= wxAtoi(value
);
2286 // note: 0 == wxBITMAP_TYPE_INVALID
2287 if (type
<= 0 || type
>= wxBITMAP_TYPE_MAX
)
2289 wxLogWarning("Invalid bitmap type specified for <image> tag: %d", type
);
2293 imageType
= (wxBitmapType
)type
;
2299 wxXmlNode
* imageChild
= node
->GetChildren();
2302 wxString childName
= imageChild
->GetName();
2303 if (childName
== wxT("data"))
2305 wxXmlNode
* dataChild
= imageChild
->GetChildren();
2308 data
= dataChild
->GetContent();
2309 // wxLogDebug(data);
2310 dataChild
= dataChild
->GetNext();
2314 imageChild
= imageChild
->GetNext();
2319 wxStringInputStream
strStream(data
);
2321 GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
2329 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2330 // Export this object directly to the given stream.
2331 bool wxRichTextImage::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2333 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2335 ::OutputIndentation(stream
, indent
);
2336 ::OutputString(stream
, wxT("<image"), handler
->GetConvMem(), handler
->GetConvFile());
2337 if (!GetImageBlock().Ok())
2340 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2344 ::OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\""), (int) GetImageBlock().GetImageType()) + style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2346 if (GetProperties().GetCount() > 0)
2348 handler
->WriteProperties(stream
, GetProperties(), indent
);
2349 ::OutputIndentation(stream
, indent
);
2352 ::OutputIndentation(stream
, indent
+1);
2353 ::OutputString(stream
, wxT("<data>"), handler
->GetConvMem(), handler
->GetConvFile());
2355 // wxStopWatch stopwatch;
2357 GetImageBlock().WriteHex(stream
);
2359 // wxLogDebug(wxT("Image conversion to hex took %ldms"), stopwatch.Time());
2361 ::OutputString(stream
, wxT("</data>\n"), handler
->GetConvMem(), handler
->GetConvFile());
2362 ::OutputIndentation(stream
, indent
);
2363 ::OutputString(stream
, wxT("</image>"), handler
->GetConvMem(), handler
->GetConvFile());
2368 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2369 // Export this object to the given parent node, usually creating at least one child node.
2370 bool wxRichTextImage::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2372 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("image"));
2373 parent
->AddChild(elementNode
);
2375 if (GetImageBlock().Ok())
2376 elementNode
->AddAttribute(wxT("imagetype"), MakeString((int) GetImageBlock().GetImageType()));
2378 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2379 handler
->WriteProperties(elementNode
, GetProperties());
2381 wxXmlNode
* dataNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("data"));
2382 elementNode
->AddChild(dataNode
);
2383 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2384 dataNode
->AddChild(textNode
);
2389 wxMemoryOutputStream stream
;
2390 if (GetImageBlock().WriteHex(stream
))
2392 if (stream
.GetSize() > 0)
2394 int size
= stream
.GetSize();
2396 int size2
= stream
.GetOutputStreamBuffer()->GetIntPosition();
2397 wxASSERT(size
== size2
);
2399 unsigned char* data
= new unsigned char[size
];
2400 stream
.CopyTo(data
, size
);
2401 strData
= wxString((const char*) data
, wxConvUTF8
, size
);
2405 strData
= wxEmptyString
;
2411 wxStringOutputStream
strStream(& strData
);
2412 GetImageBlock().WriteHex(strStream
);
2416 textNode
->SetContent(strData
);
2417 textNode
->SetNoConversion(true); // optimize speed
2424 // Import this object from XML
2425 bool wxRichTextParagraphLayoutBox::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2427 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2431 wxString partial
= node
->GetAttribute(wxT("partialparagraph"), wxEmptyString
);
2432 if (partial
== wxT("true"))
2433 SetPartialParagraph(true);
2435 wxXmlNode
* child
= wxRichTextXMLHandler::FindNode(node
, wxT("stylesheet"));
2436 if (child
&& (handler
->GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
2438 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
2439 wxString sheetName
= child
->GetAttribute(wxT("name"), wxEmptyString
);
2440 wxString sheetDescription
= child
->GetAttribute(wxT("description"), wxEmptyString
);
2441 sheet
->SetName(sheetName
);
2442 sheet
->SetDescription(sheetDescription
);
2444 wxXmlNode
* child2
= child
->GetChildren();
2447 handler
->ImportStyleDefinition(sheet
, child2
);
2449 child2
= child2
->GetNext();
2452 // Notify that styles have changed. If this is vetoed by the app,
2453 // the new sheet will be deleted. If it is not vetoed, the
2454 // old sheet will be deleted and replaced with the new one.
2455 buffer
->SetStyleSheetAndNotify(sheet
);
2461 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2462 // Export this object directly to the given stream.
2463 bool wxRichTextParagraphLayoutBox::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2465 ::OutputIndentation(stream
, indent
);
2466 wxString nodeName
= GetXMLNodeName();
2467 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2469 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2471 if (GetPartialParagraph())
2472 style
<< wxT(" partialparagraph=\"true\"");
2474 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2476 if (GetProperties().GetCount() > 0)
2478 handler
->WriteProperties(stream
, GetProperties(), indent
);
2482 for (i
= 0; i
< GetChildCount(); i
++)
2484 wxRichTextObject
* child
= GetChild(i
);
2485 child
->ExportXML(stream
, indent
+1, handler
);
2488 ::OutputIndentation(stream
, indent
);
2489 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2494 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2495 // Export this object to the given parent node, usually creating at least one child node.
2496 bool wxRichTextParagraphLayoutBox::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2498 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2499 parent
->AddChild(elementNode
);
2500 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2501 handler
->WriteProperties(elementNode
, GetProperties());
2503 if (GetPartialParagraph())
2504 elementNode
->AddAttribute(wxT("partialparagraph"), wxT("true"));
2507 for (i
= 0; i
< GetChildCount(); i
++)
2509 wxRichTextObject
* child
= GetChild(i
);
2510 child
->ExportXML(elementNode
, handler
);
2517 // Import this object from XML
2518 bool wxRichTextTable::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2520 wxRichTextBox::ImportFromXML(buffer
, node
, handler
, recurse
);
2524 m_rowCount
= wxAtoi(node
->GetAttribute(wxT("rows"), wxEmptyString
));
2525 m_colCount
= wxAtoi(node
->GetAttribute(wxT("cols"), wxEmptyString
));
2527 wxXmlNode
* child
= node
->GetChildren();
2530 wxRichTextObject
* childObj
= handler
->CreateObjectForXMLName(this, child
->GetName());
2533 AppendChild(childObj
);
2534 handler
->ImportXML(buffer
, childObj
, child
);
2536 child
= child
->GetNext();
2539 m_cells
.Add(wxRichTextObjectPtrArray(), m_rowCount
);
2541 for (i
= 0; i
< m_rowCount
; i
++)
2543 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
2544 for (j
= 0; j
< m_colCount
; j
++)
2546 int idx
= i
* m_colCount
+ j
;
2547 if (idx
< (int) GetChildren().GetCount())
2549 wxRichTextCell
* cell
= wxDynamicCast(GetChildren().Item(idx
)->GetData(), wxRichTextCell
);
2559 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2560 // Export this object directly to the given stream.
2561 bool wxRichTextTable::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2563 ::OutputIndentation(stream
, indent
);
2564 wxString nodeName
= GetXMLNodeName();
2565 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2567 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2569 style
<< wxT(" rows=\"") << m_rowCount
<< wxT("\"");
2570 style
<< wxT(" cols=\"") << m_colCount
<< wxT("\"");
2572 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2574 if (GetProperties().GetCount() > 0)
2576 handler
->WriteProperties(stream
, GetProperties(), indent
);
2580 for (i
= 0; i
< m_rowCount
; i
++)
2582 for (j
= 0; j
< m_colCount
; j
++)
2584 wxRichTextCell
* cell
= GetCell(i
, j
);
2585 cell
->ExportXML(stream
, indent
+1, handler
);
2589 ::OutputIndentation(stream
, indent
);
2590 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2596 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2597 // Export this object to the given parent node, usually creating at least one child node.
2598 bool wxRichTextTable::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2600 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2601 parent
->AddChild(elementNode
);
2602 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2603 handler
->WriteProperties(elementNode
, GetProperties());
2605 elementNode
->AddAttribute(wxT("rows"), wxString::Format(wxT("%d"), m_rowCount
));
2606 elementNode
->AddAttribute(wxT("cols"), wxString::Format(wxT("%d"), m_colCount
));
2609 for (i
= 0; i
< m_rowCount
; i
++)
2611 for (j
= 0; j
< m_colCount
; j
++)
2613 wxRichTextCell
* cell
= GetCell(i
, j
);
2614 cell
->ExportXML(elementNode
, handler
);
2624 // wxUSE_RICHTEXT && wxUSE_XML