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 if (node
->GetName() == name
&& name
== wxT("stylesheet"))
412 wxXmlNode
* child
= node
->GetChildren();
415 if (child
->GetName() == name
)
417 child
= child
->GetNext();
422 // For use with earlier versions of wxWidgets
423 #ifndef WXUNUSED_IN_UNICODE
425 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
427 #define WXUNUSED_IN_UNICODE(x) x
431 // write string to output
432 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
433 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
), wxMBConv
*convFile
)
435 if (str
.empty()) return;
439 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
440 stream
.Write((const char*)buf
, strlen((const char*)buf
));
444 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
445 stream
.Write((const char*)buf
, strlen((const char*)buf
));
448 if ( convFile
== NULL
)
449 stream
.Write(str
.mb_str(), str
.Len());
452 wxString
str2(str
.wc_str(*convMem
), *convFile
);
453 stream
.Write(str2
.mb_str(), str2
.Len());
458 static void OutputIndentation(wxOutputStream
& stream
, int indent
)
460 wxString str
= wxT("\n");
461 for (int i
= 0; i
< indent
; i
++)
462 str
<< wxT(' ') << wxT(' ');
463 ::OutputString(stream
, str
, NULL
, NULL
);
466 // Same as above, but create entities first.
467 // Translates '<' to "<", '>' to ">" and '&' to "&"
468 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
469 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
477 for (i
= 0; i
< len
; i
++)
481 // Original code excluded "&" but we _do_ want to convert
482 // the ampersand beginning & because otherwise when read in,
483 // the original "&" becomes "&".
485 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
486 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
488 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
492 OutputString(stream
, wxT("<"), NULL
, NULL
);
495 OutputString(stream
, wxT(">"), NULL
, NULL
);
498 OutputString(stream
, wxT("&"), NULL
, NULL
);
501 OutputString(stream
, wxT("""), NULL
, NULL
);
507 else if (wxUChar(c
) > 127)
509 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
511 wxString
s(wxT("&#"));
515 s
<< (int) wxUChar(c
);
518 OutputString(stream
, s
, NULL
, NULL
);
522 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
525 void wxRichTextXMLHandler::OutputString(wxOutputStream
& stream
, const wxString
& str
)
527 ::OutputString(stream
, str
, m_convMem
, m_convFile
);
530 void wxRichTextXMLHandler::OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
)
532 ::OutputStringEnt(stream
, str
, m_convMem
, m_convFile
);
535 void wxRichTextXMLHandler::OutputIndentation(wxOutputStream
& stream
, int indent
)
537 wxString str
= wxT("\n");
538 for (int i
= 0; i
< indent
; i
++)
539 str
<< wxT(' ') << wxT(' ');
540 ::OutputString(stream
, str
, NULL
, NULL
);
543 wxString
wxRichTextXMLHandler::AttributeToXML(const wxString
& str
)
551 for (i
= 0; i
< len
; i
++)
555 // Original code excluded "&" but we _do_ want to convert
556 // the ampersand beginning & because otherwise when read in,
557 // the original "&" becomes "&".
559 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
560 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
562 str1
+= str
.Mid(last
, i
- last
);
572 str1
+= wxT("&");
575 str1
+= wxT(""");
581 else if (wxUChar(c
) > 127)
583 str1
+= str
.Mid(last
, i
- last
);
585 wxString
s(wxT("&#"));
589 s
<< (int) wxUChar(c
);
596 str1
+= str
.Mid(last
, i
- last
);
600 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
602 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const int& v
)
604 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%d"), v
) << wxT("\"");
607 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const long& v
)
609 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%ld"), v
) << wxT("\"");
612 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const double& v
)
614 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%.2f"), (float) v
) << wxT("\"");
617 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxChar
* s
)
619 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
622 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxString
& s
)
624 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
627 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxColour
& col
)
629 str
<< wxT(" ") << name
<< wxT("=\"") << wxT("#") << ColourToHexString(col
) << wxT("\"");
632 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxTextAttrDimension
& dim
)
636 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString((int) dim
.GetFlags());
637 str
<< wxT(" ") << name
<< wxT("=\"");
643 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
645 if (dims
.GetLeft().IsValid())
646 AddAttribute(str
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
647 if (dims
.GetRight().IsValid())
648 AddAttribute(str
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
649 if (dims
.GetTop().IsValid())
650 AddAttribute(str
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
651 if (dims
.GetBottom().IsValid())
652 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
655 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
657 if (border
.HasStyle())
658 AddAttribute(str
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
659 if (border
.HasColour())
660 AddAttribute(str
, rootName
+ wxString(wxT("-color")), border
.GetColour());
661 if (border
.HasWidth())
662 AddAttribute(str
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
665 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
667 AddAttribute(str
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
668 AddAttribute(str
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
669 AddAttribute(str
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
670 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
674 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
676 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
678 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const int& v
)
680 node
->AddAttribute(name
, MakeString(v
));
683 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const long& v
)
685 node
->AddAttribute(name
, MakeString(v
));
688 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const double& v
)
690 node
->AddAttribute(name
, MakeString(v
));
693 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxString
& s
)
695 node
->AddAttribute(name
, s
);
698 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxColour
& col
)
700 node
->AddAttribute(name
, MakeString(col
));
703 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxTextAttrDimension
& dim
)
707 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString(dim
.GetFlags());
708 AddAttribute(node
, name
, value
);
712 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
714 if (dims
.GetLeft().IsValid())
715 AddAttribute(node
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
716 if (dims
.GetRight().IsValid())
717 AddAttribute(node
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
718 if (dims
.GetTop().IsValid())
719 AddAttribute(node
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
720 if (dims
.GetBottom().IsValid())
721 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
724 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
726 if (border
.HasStyle())
727 AddAttribute(node
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
728 if (border
.HasColour())
729 AddAttribute(node
, rootName
+ wxString(wxT("-color")), border
.GetColour());
730 if (border
.HasWidth())
731 AddAttribute(node
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
734 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
736 AddAttribute(node
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
737 AddAttribute(node
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
738 AddAttribute(node
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
739 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
742 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
744 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
749 wxString
version(wxT("1.0") ) ;
751 bool deleteConvFile
= false;
752 wxString fileEncoding
;
753 //wxMBConv* convFile = NULL;
756 fileEncoding
= wxT("UTF-8");
757 m_convFile
= & wxConvUTF8
;
759 fileEncoding
= wxT("ISO-8859-1");
760 m_convFile
= & wxConvISO8859_1
;
763 // If SetEncoding has been called, change the output encoding.
764 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
766 if (m_encoding
== wxT("<System>"))
769 fileEncoding
= wxLocale::GetSystemEncodingName();
770 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
775 fileEncoding
= m_encoding
;
778 // GetSystemEncodingName may not have returned a name
779 if (fileEncoding
.empty())
781 fileEncoding
= wxT("UTF-8");
783 fileEncoding
= wxT("ISO-8859-1");
785 m_convFile
= new wxCSConv(fileEncoding
);
786 deleteConvFile
= true;
789 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT
790 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
791 wxStopWatch stopwatch
;
793 wxXmlDocument
* doc
= new wxXmlDocument
;
794 doc
->SetFileEncoding(fileEncoding
);
796 wxXmlNode
* rootNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("richtext"));
797 doc
->SetRoot(rootNode
);
798 rootNode
->AddAttribute(wxT("version"), wxT("1.0.0.0"));
799 rootNode
->AddAttribute(wxT("xmlns"), wxT("http://www.wxwidgets.org"));
801 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
803 wxXmlNode
* styleSheetNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("stylesheet"));
804 rootNode
->AddChild(styleSheetNode
);
806 wxString nameAndDescr
;
808 if (!buffer
->GetStyleSheet()->GetName().empty())
809 styleSheetNode
->AddAttribute(wxT("name"), buffer
->GetStyleSheet()->GetName());
811 if (!buffer
->GetStyleSheet()->GetDescription().empty())
812 styleSheetNode
->AddAttribute(wxT("description"), buffer
->GetStyleSheet()->GetDescription());
815 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
817 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
818 ExportStyleDefinition(styleSheetNode
, def
);
821 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
823 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
824 ExportStyleDefinition(styleSheetNode
, def
);
827 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
829 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
830 ExportStyleDefinition(styleSheetNode
, def
);
833 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
835 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
836 ExportStyleDefinition(styleSheetNode
, def
);
839 bool success
= ExportXML(rootNode
, *buffer
);
840 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
841 long t
= stopwatch
.Time();
842 wxLogDebug(wxT("Creating the document took %ldms"), t
);
843 wxMessageBox(wxString::Format(wxT("Creating the document took %ldms"), t
));
847 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
850 success
= doc
->Save(stream
);
851 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
853 wxLogDebug(wxT("Save() took %ldms"), t2
);
854 wxMessageBox(wxString::Format(wxT("Save() took %ldms"), t2
));
861 // !(wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT)
864 m_convMem
= wxConvCurrent
;
870 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
871 version
, fileEncoding
);
872 OutputString(stream
, s
);
873 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">"));
877 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
879 OutputIndentation(stream
, level
);
880 wxString nameAndDescr
;
881 if (!buffer
->GetStyleSheet()->GetName().empty())
882 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
883 if (!buffer
->GetStyleSheet()->GetDescription().empty())
884 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
885 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"));
889 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
891 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
892 ExportStyleDefinition(stream
, def
, level
+ 1);
895 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
897 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
898 ExportStyleDefinition(stream
, def
, level
+ 1);
901 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
903 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
904 ExportStyleDefinition(stream
, def
, level
+ 1);
907 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
909 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
910 ExportStyleDefinition(stream
, def
, level
+ 1);
913 OutputIndentation(stream
, level
);
914 OutputString(stream
, wxT("</stylesheet>"));
918 bool success
= ExportXML(stream
, *buffer
, level
);
920 OutputString(stream
, wxT("\n</richtext>"));
921 OutputString(stream
, wxT("\n"));
932 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
934 /// Recursively export an object
935 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxRichTextObject
& obj
, int indent
)
937 obj
.ExportXML(stream
, indent
, this);
942 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxRichTextStyleDefinition
* def
, int level
)
944 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
945 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
946 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
947 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
949 wxString name
= def
->GetName();
952 nameProp
= wxT(" name=\"") + AttributeToXML(name
) + wxT("\"");
954 wxString baseStyle
= def
->GetBaseStyle();
955 wxString baseStyleProp
;
956 if (!baseStyle
.empty())
957 baseStyleProp
= wxT(" basestyle=\"") + AttributeToXML(baseStyle
) + wxT("\"");
959 wxString descr
= def
->GetDescription();
962 descrProp
= wxT(" description=\"") + AttributeToXML(descr
) + wxT("\"");
966 OutputIndentation(stream
, level
);
967 OutputString(stream
, wxT("<characterstyle") + nameProp
+ baseStyleProp
+ descrProp
+ wxT(">"));
971 wxString style
= AddAttributes(def
->GetStyle(), false);
973 OutputIndentation(stream
, level
);
974 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
976 OutputIndentation(stream
, level
);
977 OutputString(stream
, wxT("</style>"));
981 OutputIndentation(stream
, level
);
982 OutputString(stream
, wxT("</characterstyle>"));
986 OutputIndentation(stream
, level
);
988 if (!listDef
->GetNextStyle().empty())
989 baseStyleProp
<< wxT(" nextstyle=\"") << AttributeToXML(listDef
->GetNextStyle()) << wxT("\"");
991 OutputString(stream
, wxT("<liststyle") + nameProp
+ baseStyleProp
+ descrProp
+ wxT(">"));
995 wxString style
= AddAttributes(def
->GetStyle(), true);
997 OutputIndentation(stream
, level
);
998 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1000 OutputIndentation(stream
, level
);
1001 OutputString(stream
, wxT("</style>"));
1004 for (i
= 0; i
< 10; i
++)
1006 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1009 wxString style
= AddAttributes(def
->GetStyle(), true);
1010 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
1012 OutputIndentation(stream
, level
);
1013 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"));
1015 OutputIndentation(stream
, level
);
1016 OutputString(stream
, wxT("</style>"));
1022 OutputIndentation(stream
, level
);
1023 OutputString(stream
, wxT("</liststyle>"));
1027 OutputIndentation(stream
, level
);
1029 if (!paraDef
->GetNextStyle().empty())
1030 baseStyleProp
<< wxT(" nextstyle=\"") << AttributeToXML(paraDef
->GetNextStyle()) << wxT("\"");
1032 OutputString(stream
, wxT("<paragraphstyle") + nameProp
+ baseStyleProp
+ descrProp
+ wxT(">"));
1036 wxString style
= AddAttributes(def
->GetStyle(), true);
1038 OutputIndentation(stream
, level
);
1039 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1041 OutputIndentation(stream
, level
);
1042 OutputString(stream
, wxT("</style>"));
1046 OutputIndentation(stream
, level
);
1047 OutputString(stream
, wxT("</paragraphstyle>"));
1051 OutputIndentation(stream
, level
);
1053 OutputString(stream
, wxT("<boxstyle") + nameProp
+ baseStyleProp
+ descrProp
+ wxT(">"));
1057 wxString style
= AddAttributes(def
->GetStyle(), true);
1059 OutputIndentation(stream
, level
);
1060 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1062 OutputIndentation(stream
, level
);
1063 OutputString(stream
, wxT("</style>"));
1067 OutputIndentation(stream
, level
);
1068 OutputString(stream
, wxT("</boxstyle>"));
1075 /// Create a string containing style attributes
1076 wxString
wxRichTextXMLHandler::AddAttributes(const wxRichTextAttr
& attr
, bool isPara
)
1079 if (attr
.HasTextColour() && attr
.GetTextColour().IsOk())
1080 AddAttribute(str
, wxT("textcolor"), attr
.GetTextColour());
1082 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().IsOk())
1083 AddAttribute(str
, wxT("bgcolor"), attr
.GetBackgroundColour());
1085 if (attr
.HasFontSize())
1086 AddAttribute(str
, wxT("fontsize"), attr
.GetFontSize());
1088 if (attr
.HasFontFamily())
1089 AddAttribute(str
, wxT("fontfamily"), attr
.GetFontFamily());
1091 if (attr
.HasFontItalic())
1092 AddAttribute(str
, wxT("fontstyle"), attr
.GetFontStyle());
1094 if (attr
.HasFontWeight())
1095 AddAttribute(str
, wxT("fontweight"), attr
.GetFontWeight());
1097 if (attr
.HasFontUnderlined())
1098 AddAttribute(str
, wxT("fontunderlined"), (int) attr
.GetFontUnderlined());
1100 if (attr
.HasFontFaceName())
1101 AddAttribute(str
, wxT("fontface"), AttributeToXML(attr
.GetFontFaceName()));
1103 if (attr
.HasTextEffects())
1105 AddAttribute(str
, wxT("texteffects"), attr
.GetTextEffects());
1106 AddAttribute(str
, wxT("texteffectflags"), attr
.GetTextEffectFlags());
1109 if (!attr
.GetCharacterStyleName().empty())
1110 AddAttribute(str
, wxT("characterstyle"), AttributeToXML(attr
.GetCharacterStyleName()));
1113 AddAttribute(str
, wxT("url"), AttributeToXML(attr
.GetURL()));
1117 if (attr
.HasAlignment())
1118 AddAttribute(str
, wxT("alignment"), (int) attr
.GetAlignment());
1120 if (attr
.HasLeftIndent())
1122 AddAttribute(str
, wxT("leftindent"), (int) attr
.GetLeftIndent());
1123 AddAttribute(str
, wxT("leftsubindent"), (int) attr
.GetLeftSubIndent());
1126 if (attr
.HasRightIndent())
1127 AddAttribute(str
, wxT("rightindent"), (int) attr
.GetRightIndent());
1129 if (attr
.HasParagraphSpacingAfter())
1130 AddAttribute(str
, wxT("parspacingafter"), (int) attr
.GetParagraphSpacingAfter());
1132 if (attr
.HasParagraphSpacingBefore())
1133 AddAttribute(str
, wxT("parspacingbefore"), (int) attr
.GetParagraphSpacingBefore());
1135 if (attr
.HasLineSpacing())
1136 AddAttribute(str
, wxT("linespacing"), (int) attr
.GetLineSpacing());
1138 if (attr
.HasBulletStyle())
1139 AddAttribute(str
, wxT("bulletstyle"), (int) attr
.GetBulletStyle());
1141 if (attr
.HasBulletNumber())
1142 AddAttribute(str
, wxT("bulletnumber"), (int) attr
.GetBulletNumber());
1144 if (attr
.HasBulletText())
1146 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1147 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1148 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1149 AddAttribute(str
, wxT("bulletsymbol"), (int) (attr
.GetBulletText()[0]));
1151 AddAttribute(str
, wxT("bullettext"), AttributeToXML(attr
.GetBulletText()));
1153 AddAttribute(str
, wxT("bulletfont"), attr
.GetBulletFont());
1156 if (attr
.HasBulletName())
1157 AddAttribute(str
, wxT("bulletname"), AttributeToXML(attr
.GetBulletName()));
1159 if (!attr
.GetParagraphStyleName().empty())
1160 AddAttribute(str
, wxT("parstyle"), AttributeToXML(attr
.GetParagraphStyleName()));
1162 if (!attr
.GetListStyleName().empty())
1163 AddAttribute(str
, wxT("liststyle"), AttributeToXML(attr
.GetListStyleName()));
1165 if (!attr
.GetTextBoxAttr().GetBoxStyleName().empty())
1166 AddAttribute(str
, wxT("boxstyle"), AttributeToXML(attr
.GetTextBoxAttr().GetBoxStyleName()));
1172 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1174 if (i
> 0) strTabs
<< wxT(",");
1175 strTabs
<< attr
.GetTabs()[i
];
1177 AddAttribute(str
, wxT("tabs"), strTabs
);
1180 if (attr
.HasPageBreak())
1182 AddAttribute(str
, wxT("pagebreak"), 1);
1185 if (attr
.HasOutlineLevel())
1186 AddAttribute(str
, wxT("outlinelevel"), (int) attr
.GetOutlineLevel());
1189 AddAttribute(str
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1190 AddAttribute(str
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1191 AddAttribute(str
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1192 AddAttribute(str
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1193 AddAttribute(str
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1194 AddAttribute(str
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1195 AddAttribute(str
, wxT("height"), attr
.GetTextBoxAttr().GetHeight());
1196 AddAttribute(str
, wxT("minwidth"), attr
.GetTextBoxAttr().GetMinSize().GetWidth());
1197 AddAttribute(str
, wxT("minheight"), attr
.GetTextBoxAttr().GetMinSize().GetHeight());
1198 AddAttribute(str
, wxT("maxwidth"), attr
.GetTextBoxAttr().GetMaxSize().GetWidth());
1199 AddAttribute(str
, wxT("maxheight"), attr
.GetTextBoxAttr().GetMaxSize().GetHeight());
1201 if (attr
.GetTextBoxAttr().HasVerticalAlignment())
1204 if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP
)
1206 else if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_CENTRE
)
1207 value
= wxT("centre");
1208 else if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_BOTTOM
)
1209 value
= wxT("bottom");
1211 value
= wxT("none");
1212 AddAttribute(str
, wxT("verticalalignment"), value
);
1215 if (attr
.GetTextBoxAttr().HasFloatMode())
1218 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1219 value
= wxT("left");
1220 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1221 value
= wxT("right");
1223 value
= wxT("none");
1224 AddAttribute(str
, wxT("float"), value
);
1227 if (attr
.GetTextBoxAttr().HasClearMode())
1230 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1231 value
= wxT("left");
1232 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1233 value
= wxT("right");
1234 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1235 value
= wxT("both");
1237 value
= wxT("none");
1238 AddAttribute(str
, wxT("clear"), value
);
1241 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1242 AddAttribute(str
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1247 // Make a string from the given property. This can be overridden for custom variants.
1248 wxString
wxRichTextXMLHandler::MakeStringFromProperty(const wxVariant
& var
)
1250 return var
.MakeString();
1253 // Create a proprty from the string read from the XML file.
1254 wxVariant
wxRichTextXMLHandler::MakePropertyFromString(const wxString
& name
, const wxString
& value
, const wxString
& WXUNUSED(type
))
1256 wxVariant
var(value
, name
);
1257 // TODO: use type to create using common types
1261 // Write the properties
1262 bool wxRichTextXMLHandler::WriteProperties(wxOutputStream
& stream
, const wxRichTextProperties
& properties
, int level
)
1264 if (properties
.GetCount() > 0)
1268 OutputIndentation(stream
, level
);
1269 OutputString(stream
, wxT("<properties"));
1274 for (i
= 0; i
< properties
.GetCount(); i
++)
1276 const wxVariant
& var
= properties
[i
];
1279 const wxString
& name
= var
.GetName();
1280 wxString value
= MakeStringFromProperty(var
);
1282 OutputIndentation(stream
, level
);
1283 OutputString(stream
, wxT("<property name=\"") + name
+
1284 wxT("\" type=\"") + var
.GetType() + wxT("\" value=\""));
1285 OutputStringEnt(stream
, value
);
1286 OutputString(stream
, wxT("\"/>\n"));
1292 OutputIndentation(stream
, level
);
1293 OutputString(stream
, wxT("</properties>\n"));
1303 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
1305 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1306 bool wxRichTextXMLHandler::ExportXML(wxXmlNode
* parent
, wxRichTextObject
& obj
)
1308 obj
.ExportXML(parent
, this);
1313 bool wxRichTextXMLHandler::ExportStyleDefinition(wxXmlNode
* parent
, wxRichTextStyleDefinition
* def
)
1315 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
1316 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
1317 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
1318 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
1320 wxString baseStyle
= def
->GetBaseStyle();
1321 wxString descr
= def
->GetDescription();
1323 wxXmlNode
* defNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxEmptyString
);
1324 parent
->AddChild(defNode
);
1325 if (!baseStyle
.empty())
1326 defNode
->AddAttribute(wxT("basestyle"), baseStyle
);
1328 defNode
->AddAttribute(wxT("description"), descr
);
1330 wxXmlNode
* styleNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1331 defNode
->AddChild(styleNode
);
1335 defNode
->SetName(wxT("characterstyle"));
1336 AddAttributes(styleNode
, def
->GetStyle(), false);
1340 defNode
->SetName(wxT("liststyle"));
1342 if (!listDef
->GetNextStyle().empty())
1343 defNode
->AddAttribute(wxT("nextstyle"), listDef
->GetNextStyle());
1345 AddAttributes(styleNode
, def
->GetStyle(), true);
1348 for (i
= 0; i
< 10; i
++)
1350 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1353 wxXmlNode
* levelNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1354 defNode
->AddChild(levelNode
);
1355 levelNode
->AddAttribute(wxT("level"), MakeString(i
+1));
1356 AddAttributes(levelNode
, * levelAttr
, true);
1362 defNode
->SetName(wxT("boxstyle"));
1364 AddAttributes(styleNode
, def
->GetStyle(), true);
1368 defNode
->SetName(wxT("paragraphstyle"));
1370 if (!paraDef
->GetNextStyle().empty())
1371 defNode
->AddAttribute(wxT("nextstyle"), paraDef
->GetNextStyle());
1373 AddAttributes(styleNode
, def
->GetStyle(), true);
1379 bool wxRichTextXMLHandler::AddAttributes(wxXmlNode
* node
, wxRichTextAttr
& attr
, bool isPara
)
1381 if (attr
.HasTextColour() && attr
.GetTextColour().IsOk())
1382 node
->AddAttribute(wxT("textcolor"), MakeString(attr
.GetTextColour()));
1383 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().IsOk())
1384 node
->AddAttribute(wxT("bgcolor"), MakeString(attr
.GetBackgroundColour()));
1386 if (attr
.HasFontSize())
1387 node
->AddAttribute(wxT("fontsize"), MakeString(attr
.GetFontSize()));
1388 if (attr
.HasFontFamily())
1389 node
->AddAttribute(wxT("fontfamily"), MakeString(attr
.GetFontFamily()));
1390 if (attr
.HasFontItalic())
1391 node
->AddAttribute(wxT("fontstyle"), MakeString(attr
.GetFontStyle()));
1392 if (attr
.HasFontWeight())
1393 node
->AddAttribute(wxT("fontweight"), MakeString(attr
.GetFontWeight()));
1394 if (attr
.HasFontUnderlined())
1395 node
->AddAttribute(wxT("fontunderlined"), MakeString((int) attr
.GetFontUnderlined()));
1396 if (attr
.HasFontFaceName())
1397 node
->AddAttribute(wxT("fontface"), attr
.GetFontFaceName());
1399 if (attr
.HasTextEffects())
1401 node
->AddAttribute(wxT("texteffects"), MakeString(attr
.GetTextEffects()));
1402 node
->AddAttribute(wxT("texteffectflags"), MakeString(attr
.GetTextEffectFlags()));
1404 if (attr
.HasCharacterStyleName() && !attr
.GetCharacterStyleName().empty())
1405 node
->AddAttribute(wxT("characterstyle"), attr
.GetCharacterStyleName());
1408 node
->AddAttribute(wxT("url"), attr
.GetURL()); // TODO: do we need to wrap this in AttributeToXML?
1412 if (attr
.HasAlignment())
1413 node
->AddAttribute(wxT("alignment"), MakeString((int) attr
.GetAlignment()));
1415 if (attr
.HasLeftIndent())
1417 node
->AddAttribute(wxT("leftindent"), MakeString((int) attr
.GetLeftIndent()));
1418 node
->AddAttribute(wxT("leftsubindent"), MakeString((int) attr
.GetLeftSubIndent()));
1421 if (attr
.HasRightIndent())
1422 node
->AddAttribute(wxT("rightindent"), MakeString((int) attr
.GetRightIndent()));
1424 if (attr
.HasParagraphSpacingAfter())
1425 node
->AddAttribute(wxT("parspacingafter"), MakeString((int) attr
.GetParagraphSpacingAfter()));
1427 if (attr
.HasParagraphSpacingBefore())
1428 node
->AddAttribute(wxT("parspacingbefore"), MakeString((int) attr
.GetParagraphSpacingBefore()));
1430 if (attr
.HasLineSpacing())
1431 node
->AddAttribute(wxT("linespacing"), MakeString((int) attr
.GetLineSpacing()));
1433 if (attr
.HasBulletStyle())
1434 node
->AddAttribute(wxT("bulletstyle"), MakeString((int) attr
.GetBulletStyle()));
1436 if (attr
.HasBulletNumber())
1437 node
->AddAttribute(wxT("bulletnumber"), MakeString((int) attr
.GetBulletNumber()));
1439 if (attr
.HasBulletText())
1441 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1442 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1443 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1444 node
->AddAttribute(wxT("bulletsymbol"), MakeString((int) (attr
.GetBulletText()[0])));
1446 node
->AddAttribute(wxT("bullettext"), attr
.GetBulletText());
1448 if (!attr
.GetBulletFont().empty())
1449 node
->AddAttribute(wxT("bulletfont"), attr
.GetBulletFont());
1452 if (attr
.HasBulletName())
1453 node
->AddAttribute(wxT("bulletname"), attr
.GetBulletName());
1455 if (!attr
.GetParagraphStyleName().empty())
1456 node
->AddAttribute(wxT("parstyle"), attr
.GetParagraphStyleName());
1458 if (!attr
.GetListStyleName().empty())
1459 node
->AddAttribute(wxT("liststyle"), attr
.GetListStyleName());
1461 if (!attr
.GetTextBoxAttr().GetBoxStyleName().empty())
1462 node
->AddAttribute(wxT("boxstyle"), attr
.GetTextBoxAttr().GetBoxStyleName());
1468 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1472 tabs
<< attr
.GetTabs()[i
];
1474 node
->AddAttribute(wxT("tabs"), tabs
);
1477 if (attr
.HasPageBreak())
1478 node
->AddAttribute(wxT("pagebreak"), wxT("1"));
1480 if (attr
.HasOutlineLevel())
1481 node
->AddAttribute(wxT("outlinelevel"), MakeString((int) attr
.GetOutlineLevel()));
1484 AddAttribute(node
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1485 AddAttribute(node
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1486 AddAttribute(node
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1487 AddAttribute(node
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1488 AddAttribute(node
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1489 AddAttribute(node
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1490 AddAttribute(node
, wxT("height"), attr
.GetTextBoxAttr().GetHeight());
1491 AddAttribute(node
, wxT("minwidth"), attr
.GetTextBoxAttr().GetMinSize().GetWidth());
1492 AddAttribute(node
, wxT("minheight"), attr
.GetTextBoxAttr().GetMinSize().GetHeight());
1493 AddAttribute(node
, wxT("maxwidth"), attr
.GetTextBoxAttr().GetMaxSize().GetWidth());
1494 AddAttribute(node
, wxT("maxheight"), attr
.GetTextBoxAttr().GetMaxSize().GetHeight());
1496 if (attr
.GetTextBoxAttr().HasVerticalAlignment())
1499 if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP
)
1501 else if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_CENTRE
)
1502 value
= wxT("centre");
1503 else if (attr
.GetTextBoxAttr().GetVerticalAlignment() == wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_BOTTOM
)
1504 value
= wxT("bottom");
1506 value
= wxT("none");
1507 AddAttribute(node
, wxT("verticalalignment"), value
);
1510 if (attr
.GetTextBoxAttr().HasFloatMode())
1513 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1514 value
= wxT("left");
1515 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1516 value
= wxT("right");
1518 value
= wxT("none");
1519 AddAttribute(node
, wxT("float"), value
);
1522 if (attr
.GetTextBoxAttr().HasClearMode())
1525 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1526 value
= wxT("left");
1527 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1528 value
= wxT("right");
1529 else if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1530 value
= wxT("both");
1532 value
= wxT("none");
1533 AddAttribute(node
, wxT("clear"), value
);
1536 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1537 AddAttribute(node
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1542 bool wxRichTextXMLHandler::WriteProperties(wxXmlNode
* node
, const wxRichTextProperties
& properties
)
1544 if (properties
.GetCount() > 0)
1546 wxXmlNode
* propertiesNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("properties"));
1547 node
->AddChild(propertiesNode
);
1549 for (i
= 0; i
< properties
.GetCount(); i
++)
1551 const wxVariant
& var
= properties
[i
];
1554 wxXmlNode
* propertyNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("property"));
1555 propertiesNode
->AddChild(propertyNode
);
1557 const wxString
& name
= var
.GetName();
1558 wxString value
= MakeStringFromProperty(var
);
1560 AddAttribute(propertyNode
, wxT("name"), name
);
1561 AddAttribute(propertyNode
, wxT("type"), var
.GetType());
1562 AddAttribute(propertyNode
, wxT("value"), value
);
1570 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1572 /// Replace face name with current name for platform.
1573 /// TODO: introduce a virtual function or settable table to
1574 /// do this comprehensively.
1575 bool wxRichTextFixFaceName(wxString
& facename
)
1577 if (facename
.empty())
1581 if (facename
== wxT("Times"))
1583 facename
= wxT("Times New Roman");
1586 else if (facename
== wxT("Helvetica"))
1588 facename
= wxT("Arial");
1591 else if (facename
== wxT("Courier"))
1593 facename
= wxT("Courier New");
1599 if (facename
== wxT("Times New Roman"))
1601 facename
= wxT("Times");
1604 else if (facename
== wxT("Arial"))
1606 facename
= wxT("Helvetica");
1609 else if (facename
== wxT("Courier New"))
1611 facename
= wxT("Courier");
1619 static inline long wxRichTextColourStringToLong(const wxString
& colStr
)
1621 if (!colStr
.IsEmpty())
1623 wxColour
col(colStr
);
1624 return col
.GetRGB();
1630 static inline wxTextAttrDimension
wxRichTextParseDimension(const wxString
& dimStr
)
1632 wxString valuePart
= dimStr
.BeforeFirst(wxT(','));
1634 if (dimStr
.Contains(wxT(",")))
1635 flagsPart
= dimStr
.AfterFirst(wxT(','));
1636 wxTextAttrDimension dim
;
1637 dim
.SetValue(wxAtoi(valuePart
));
1638 dim
.SetFlags(wxAtoi(flagsPart
));
1643 /// Import style parameters
1644 bool wxRichTextXMLHandler::ImportStyle(wxRichTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1646 wxXmlAttribute
* xmlAttr
= node
->GetAttributes();
1650 const wxString
& name
= xmlAttr
->GetName();
1651 const wxString
& value
= xmlAttr
->GetValue();
1654 if (name
== wxT("fontface"))
1659 if (GetFlags() & wxRICHTEXT_HANDLER_CONVERT_FACENAMES
)
1660 wxRichTextFixFaceName(v
);
1661 attr
.SetFontFaceName(v
);
1664 else if (name
== wxT("fontfamily"))
1667 attr
.SetFontFamily((wxFontFamily
)wxAtoi(value
));
1669 else if (name
== wxT("fontstyle"))
1672 attr
.SetFontStyle((wxFontStyle
)wxAtoi(value
));
1674 else if (name
== wxT("fontsize"))
1677 attr
.SetFontSize(wxAtoi(value
));
1679 else if (name
== wxT("fontweight"))
1682 attr
.SetFontWeight((wxFontWeight
) wxAtoi(value
));
1684 else if (name
== wxT("fontunderlined"))
1687 attr
.SetFontUnderlined(wxAtoi(value
) != 0);
1689 else if (name
== wxT("textcolor"))
1693 if (value
[0] == wxT('#'))
1694 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1696 attr
.SetTextColour(value
);
1699 else if (name
== wxT("bgcolor"))
1703 if (value
[0] == wxT('#'))
1704 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1706 attr
.SetBackgroundColour(value
);
1709 else if (name
== wxT("characterstyle"))
1712 attr
.SetCharacterStyleName(value
);
1714 else if (name
== wxT("texteffects"))
1717 attr
.SetTextEffects(wxAtoi(value
));
1719 else if (name
== wxT("texteffectflags"))
1722 attr
.SetTextEffectFlags(wxAtoi(value
));
1724 else if (name
== wxT("url"))
1731 if (name
== wxT("alignment"))
1734 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1736 else if (name
== wxT("leftindent"))
1739 attr
.SetLeftIndent(wxAtoi(value
), attr
.GetLeftSubIndent());
1741 else if (name
== wxT("leftsubindent"))
1744 attr
.SetLeftIndent(attr
.GetLeftIndent(), wxAtoi(value
));
1746 else if (name
== wxT("rightindent"))
1749 attr
.SetRightIndent(wxAtoi(value
));
1751 else if (name
== wxT("parspacingbefore"))
1754 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1756 else if (name
== wxT("parspacingafter"))
1759 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1761 else if (name
== wxT("linespacing"))
1764 attr
.SetLineSpacing(wxAtoi(value
));
1766 else if (name
== wxT("bulletstyle"))
1769 attr
.SetBulletStyle(wxAtoi(value
));
1771 else if (name
== wxT("bulletnumber"))
1774 attr
.SetBulletNumber(wxAtoi(value
));
1776 else if (name
== wxT("bulletsymbol"))
1780 wxChar ch
= wxAtoi(value
);
1783 attr
.SetBulletText(s
);
1786 else if (name
== wxT("bullettext"))
1790 attr
.SetBulletText(value
);
1793 else if (name
== wxT("bulletfont"))
1797 attr
.SetBulletFont(value
);
1800 else if (name
== wxT("bulletname"))
1804 attr
.SetBulletName(value
);
1807 else if (name
== wxT("parstyle"))
1811 attr
.SetParagraphStyleName(value
);
1814 else if (name
== wxT("liststyle"))
1818 attr
.SetListStyleName(value
);
1821 else if (name
== wxT("boxstyle"))
1825 attr
.GetTextBoxAttr().SetBoxStyleName(value
);
1828 else if (name
== wxT("tabs"))
1833 wxStringTokenizer
tkz(value
, wxT(","));
1834 while (tkz
.HasMoreTokens())
1836 wxString token
= tkz
.GetNextToken();
1837 tabs
.Add(wxAtoi(token
));
1842 else if (name
== wxT("pagebreak"))
1846 attr
.SetPageBreak(wxAtoi(value
) != 0);
1849 else if (name
== wxT("outlinelevel"))
1853 attr
.SetOutlineLevel(wxAtoi(value
));
1866 if (name
== wxT("width"))
1868 attr
.GetTextBoxAttr().GetWidth().SetValue(wxRichTextParseDimension(value
));
1870 else if (name
== wxT("height"))
1872 attr
.GetTextBoxAttr().GetHeight().SetValue(wxRichTextParseDimension(value
));
1874 else if (name
== wxT("minwidth"))
1876 attr
.GetTextBoxAttr().GetMinSize().GetWidth().SetValue(wxRichTextParseDimension(value
));
1878 else if (name
== wxT("minheight"))
1880 attr
.GetTextBoxAttr().GetMinSize().GetHeight().SetValue(wxRichTextParseDimension(value
));
1882 else if (name
== wxT("maxwidth"))
1884 attr
.GetTextBoxAttr().GetMaxSize().GetWidth().SetValue(wxRichTextParseDimension(value
));
1886 else if (name
== wxT("maxheight"))
1888 attr
.GetTextBoxAttr().GetMaxSize().GetHeight().SetValue(wxRichTextParseDimension(value
));
1891 else if (name
== wxT("verticalalignment"))
1893 if (value
== wxT("top"))
1894 attr
.GetTextBoxAttr().SetVerticalAlignment(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_TOP
);
1895 else if (value
== wxT("centre"))
1896 attr
.GetTextBoxAttr().SetVerticalAlignment(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_CENTRE
);
1897 else if (value
== wxT("bottom"))
1898 attr
.GetTextBoxAttr().SetVerticalAlignment(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_BOTTOM
);
1899 else if (value
== wxT("none"))
1900 attr
.GetTextBoxAttr().SetVerticalAlignment(wxTEXT_BOX_ATTR_VERTICAL_ALIGNMENT_NONE
);
1902 else if (name
== wxT("float"))
1904 if (value
== wxT("left"))
1905 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT
);
1906 else if (value
== wxT("right"))
1907 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT
);
1908 else if (value
== wxT("none"))
1909 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_NONE
);
1911 else if (name
== wxT("clear"))
1913 if (value
== wxT("left"))
1914 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_LEFT
);
1915 else if (value
== wxT("right"))
1916 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_RIGHT
);
1917 else if (value
== wxT("both"))
1918 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_BOTH
);
1919 else if (value
== wxT("none"))
1920 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_NONE
);
1922 else if (name
== wxT("collapse-borders"))
1923 attr
.GetTextBoxAttr().SetCollapseBorders((wxTextBoxAttrCollapseMode
) wxAtoi(value
));
1925 else if (name
.Contains(wxT("border-")))
1927 if (name
== wxT("border-left-style"))
1928 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetStyle(wxAtoi(value
));
1929 else if (name
== wxT("border-right-style"))
1930 attr
.GetTextBoxAttr().GetBorder().GetRight().SetStyle(wxAtoi(value
));
1931 else if (name
== wxT("border-top-style"))
1932 attr
.GetTextBoxAttr().GetBorder().GetTop().SetStyle(wxAtoi(value
));
1933 else if (name
== wxT("border-bottom-style"))
1934 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetStyle(wxAtoi(value
));
1936 else if (name
== wxT("border-left-colour"))
1937 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1938 else if (name
== wxT("border-right-colour"))
1939 attr
.GetTextBoxAttr().GetBorder().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1940 else if (name
== wxT("border-top-colour"))
1941 attr
.GetTextBoxAttr().GetBorder().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1942 else if (name
== wxT("border-bottom-colour"))
1943 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1945 else if (name
== wxT("border-left-width"))
1946 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1947 else if (name
== wxT("border-right-width"))
1948 attr
.GetTextBoxAttr().GetBorder().GetRight().SetWidth(wxRichTextParseDimension(value
));
1949 else if (name
== wxT("border-top-width"))
1950 attr
.GetTextBoxAttr().GetBorder().GetTop().SetWidth(wxRichTextParseDimension(value
));
1951 else if (name
== wxT("border-bottom-width"))
1952 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1954 else if (name
.Contains(wxT("outline-")))
1956 if (name
== wxT("outline-left-style"))
1957 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetStyle(wxAtoi(value
));
1958 else if (name
== wxT("outline-right-style"))
1959 attr
.GetTextBoxAttr().GetOutline().GetRight().SetStyle(wxAtoi(value
));
1960 else if (name
== wxT("outline-top-style"))
1961 attr
.GetTextBoxAttr().GetOutline().GetTop().SetStyle(wxAtoi(value
));
1962 else if (name
== wxT("outline-bottom-style"))
1963 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetStyle(wxAtoi(value
));
1965 else if (name
== wxT("outline-left-colour"))
1966 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1967 else if (name
== wxT("outline-right-colour"))
1968 attr
.GetTextBoxAttr().GetOutline().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1969 else if (name
== wxT("outline-top-colour"))
1970 attr
.GetTextBoxAttr().GetOutline().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1971 else if (name
== wxT("outline-bottom-colour"))
1972 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1974 else if (name
== wxT("outline-left-width"))
1975 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1976 else if (name
== wxT("outline-right-width"))
1977 attr
.GetTextBoxAttr().GetOutline().GetRight().SetWidth(wxRichTextParseDimension(value
));
1978 else if (name
== wxT("outline-top-width"))
1979 attr
.GetTextBoxAttr().GetOutline().GetTop().SetWidth(wxRichTextParseDimension(value
));
1980 else if (name
== wxT("outline-bottom-width"))
1981 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1983 else if (name
.Contains(wxT("margin-")))
1985 if (name
== wxT("margin-left"))
1986 attr
.GetTextBoxAttr().GetMargins().GetLeft().SetValue(wxRichTextParseDimension(value
));
1987 else if (name
== wxT("margin-right"))
1988 attr
.GetTextBoxAttr().GetMargins().GetRight().SetValue(wxRichTextParseDimension(value
));
1989 else if (name
== wxT("margin-top"))
1990 attr
.GetTextBoxAttr().GetMargins().GetTop().SetValue(wxRichTextParseDimension(value
));
1991 else if (name
== wxT("margin-bottom"))
1992 attr
.GetTextBoxAttr().GetMargins().GetBottom().SetValue(wxRichTextParseDimension(value
));
1994 else if (name
.Contains(wxT("padding-")))
1996 if (name
== wxT("padding-left"))
1997 attr
.GetTextBoxAttr().GetPadding().GetLeft().SetValue(wxRichTextParseDimension(value
));
1998 else if (name
== wxT("padding-right"))
1999 attr
.GetTextBoxAttr().GetPadding().GetRight().SetValue(wxRichTextParseDimension(value
));
2000 else if (name
== wxT("padding-top"))
2001 attr
.GetTextBoxAttr().GetPadding().GetTop().SetValue(wxRichTextParseDimension(value
));
2002 else if (name
== wxT("padding-bottom"))
2003 attr
.GetTextBoxAttr().GetPadding().GetBottom().SetValue(wxRichTextParseDimension(value
));
2005 else if (name
.Contains(wxT("position-")))
2007 if (name
== wxT("position-left"))
2008 attr
.GetTextBoxAttr().GetPosition().GetLeft().SetValue(wxRichTextParseDimension(value
));
2009 else if (name
== wxT("position-right"))
2010 attr
.GetTextBoxAttr().GetPosition().GetRight().SetValue(wxRichTextParseDimension(value
));
2011 else if (name
== wxT("position-top"))
2012 attr
.GetTextBoxAttr().GetPosition().GetTop().SetValue(wxRichTextParseDimension(value
));
2013 else if (name
== wxT("position-bottom"))
2014 attr
.GetTextBoxAttr().GetPosition().GetBottom().SetValue(wxRichTextParseDimension(value
));
2018 xmlAttr
= xmlAttr
->GetNext();
2027 // Import this object from XML
2028 bool wxRichTextObject::ImportFromXML(wxRichTextBuffer
* WXUNUSED(buffer
), wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2030 handler
->ImportProperties(this, node
);
2031 handler
->ImportStyle(GetAttributes(), node
, UsesParagraphAttributes());
2038 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2039 // Export this object directly to the given stream.
2040 bool wxRichTextObject::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2042 ::OutputIndentation(stream
, indent
);
2043 ::OutputString(stream
, wxT("<") + GetXMLNodeName(), handler
->GetConvMem(), handler
->GetConvFile());
2045 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2047 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2049 if (GetProperties().GetCount() > 0)
2051 handler
->WriteProperties(stream
, GetProperties(), indent
);
2054 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
2058 for (i
= 0; i
< composite
->GetChildCount(); i
++)
2060 wxRichTextObject
* child
= composite
->GetChild(i
);
2061 child
->ExportXML(stream
, indent
+1, handler
);
2065 ::OutputIndentation(stream
, indent
);
2066 ::OutputString(stream
, wxT("</") + GetXMLNodeName() + wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2071 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2072 // Export this object to the given parent node, usually creating at least one child node.
2073 bool wxRichTextObject::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2075 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2076 parent
->AddChild(elementNode
);
2077 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2078 handler
->WriteProperties(elementNode
, GetProperties());
2080 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
2084 for (i
= 0; i
< composite
->GetChildCount(); i
++)
2086 wxRichTextObject
* child
= composite
->GetChild(i
);
2087 child
->ExportXML(elementNode
, handler
);
2095 // Import this object from XML
2096 bool wxRichTextPlainText::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2098 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2100 if (node
->GetName() == wxT("text"))
2103 wxXmlNode
* textChild
= node
->GetChildren();
2106 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2107 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2109 wxString text2
= textChild
->GetContent();
2111 // Strip whitespace from end
2112 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
2113 text2
= text2
.Mid(0, text2
.length()-1);
2115 if (!text2
.empty() && text2
[0] == wxT('"'))
2116 text2
= text2
.Mid(1);
2117 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
2118 text2
= text2
.Mid(0, text2
.length() - 1);
2122 textChild
= textChild
->GetNext();
2127 else if (node
->GetName() == wxT("symbol"))
2129 // This is a symbol that XML can't read in the normal way
2131 wxXmlNode
* textChild
= node
->GetChildren();
2134 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2135 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2137 wxString text2
= textChild
->GetContent();
2140 textChild
= textChild
->GetNext();
2143 wxString actualText
;
2144 actualText
<< (wxChar
) wxAtoi(text
);
2145 SetText(actualText
);
2153 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2154 // Export this object directly to the given stream.
2155 bool wxRichTextPlainText::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2157 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2161 const wxString
& text
= GetText();
2162 int len
= (int) text
.Length();
2167 ::OutputIndentation(stream
, indent
);
2168 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2169 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2170 if (GetProperties().GetCount() > 0)
2172 handler
->WriteProperties(stream
, GetProperties(), indent
);
2173 ::OutputIndentation(stream
, indent
);
2175 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2177 else for (i
= 0; i
< len
; i
++)
2180 int c
= (int) text
[i
];
2182 int c
= (int) wxUChar(text
[i
]);
2184 if ((c
< 32 || c
== 34) && /* c != 9 && */ c
!= 10 && c
!= 13)
2188 wxString
fragment(text
.Mid(last
, i
-last
));
2189 if (!fragment
.empty())
2191 ::OutputIndentation(stream
, indent
);
2192 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2194 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2196 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2198 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2199 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2200 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2203 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2205 if (GetProperties().GetCount() > 0)
2207 handler
->WriteProperties(stream
, GetProperties(), indent
);
2208 ::OutputIndentation(stream
, indent
);
2210 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2215 // Output this character as a number in a separate tag, because XML can't cope
2216 // with entities below 32 except for 10 and 13
2218 ::OutputIndentation(stream
, indent
);
2219 ::OutputString(stream
, wxT("<symbol"), handler
->GetConvMem(), handler
->GetConvFile());
2221 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2222 ::OutputString(stream
, wxString::Format(wxT("%d"), c
), handler
->GetConvMem(), handler
->GetConvFile());
2224 if (GetProperties().GetCount() > 0)
2226 handler
->WriteProperties(stream
, GetProperties(), indent
);
2227 ::OutputIndentation(stream
, indent
);
2229 ::OutputString(stream
, wxT("</symbol>"), handler
->GetConvMem(), handler
->GetConvFile());
2237 fragment
= text
.Mid(last
, i
-last
);
2241 ::OutputIndentation(stream
, indent
);
2242 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2244 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2246 if (GetProperties().GetCount() > 0)
2248 handler
->WriteProperties(stream
, GetProperties(), indent
);
2249 ::OutputIndentation(stream
, indent
);
2252 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2254 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2255 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2256 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2259 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2261 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2267 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2268 // Export this object to the given parent node, usually creating at least one child node.
2269 bool wxRichTextPlainText::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2273 const wxString
& text
= GetText();
2274 int len
= (int) text
.Length();
2280 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2281 parent
->AddChild(elementNode
);
2283 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2284 handler
->WriteProperties(elementNode
, GetProperties());
2286 else for (i
= 0; i
< len
; i
++)
2289 int c
= (int) text
[i
];
2291 int c
= (int) wxUChar(text
[i
]);
2293 if ((c
< 32 || c
== 34) && c
!= 10 && c
!= 13)
2297 wxString
fragment(text
.Mid(last
, i
-last
));
2298 if (!fragment
.empty())
2300 // TODO: I'm assuming wxXmlDocument will output quotes if necessary
2301 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2302 parent
->AddChild(elementNode
);
2303 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2304 handler
->WriteProperties(elementNode
, GetProperties());
2306 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2307 elementNode
->AddChild(textNode
);
2309 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2310 fragment
= wxT("\"") + fragment
+ wxT("\"");
2312 textNode
->SetContent(fragment
);
2317 // Output this character as a number in a separate tag, because XML can't cope
2318 // with entities below 32 except for 10 and 13
2320 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("symbol"));
2321 parent
->AddChild(elementNode
);
2323 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2324 handler
->WriteProperties(elementNode
, GetProperties());
2326 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2327 elementNode
->AddChild(textNode
);
2328 textNode
->SetContent(wxString::Format(wxT("%d"), c
));
2338 fragment
= text
.Mid(last
, i
-last
);
2342 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2343 parent
->AddChild(elementNode
);
2344 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2346 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2347 elementNode
->AddChild(textNode
);
2349 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2350 fragment
= wxT("\"") + fragment
+ wxT("\"");
2352 textNode
->SetContent(fragment
);
2359 // Import this object from XML
2360 bool wxRichTextImage::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2362 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2364 wxBitmapType imageType
= wxBITMAP_TYPE_PNG
;
2365 wxString value
= node
->GetAttribute(wxT("imagetype"), wxEmptyString
);
2368 int type
= wxAtoi(value
);
2370 // note: 0 == wxBITMAP_TYPE_INVALID
2371 if (type
<= 0 || type
>= wxBITMAP_TYPE_MAX
)
2373 wxLogWarning("Invalid bitmap type specified for <image> tag: %d", type
);
2377 imageType
= (wxBitmapType
)type
;
2383 wxXmlNode
* imageChild
= node
->GetChildren();
2386 wxString childName
= imageChild
->GetName();
2387 if (childName
== wxT("data"))
2389 wxXmlNode
* dataChild
= imageChild
->GetChildren();
2392 data
= dataChild
->GetContent();
2393 // wxLogDebug(data);
2394 dataChild
= dataChild
->GetNext();
2398 imageChild
= imageChild
->GetNext();
2403 wxStringInputStream
strStream(data
);
2405 GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
2413 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2414 // Export this object directly to the given stream.
2415 bool wxRichTextImage::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2417 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2419 ::OutputIndentation(stream
, indent
);
2420 ::OutputString(stream
, wxT("<image"), handler
->GetConvMem(), handler
->GetConvFile());
2421 if (!GetImageBlock().IsOk())
2424 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2428 ::OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\""), (int) GetImageBlock().GetImageType()) + style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2430 if (GetProperties().GetCount() > 0)
2432 handler
->WriteProperties(stream
, GetProperties(), indent
);
2433 ::OutputIndentation(stream
, indent
);
2436 ::OutputIndentation(stream
, indent
+1);
2437 ::OutputString(stream
, wxT("<data>"), handler
->GetConvMem(), handler
->GetConvFile());
2439 // wxStopWatch stopwatch;
2441 GetImageBlock().WriteHex(stream
);
2443 // wxLogDebug(wxT("Image conversion to hex took %ldms"), stopwatch.Time());
2445 ::OutputString(stream
, wxT("</data>\n"), handler
->GetConvMem(), handler
->GetConvFile());
2446 ::OutputIndentation(stream
, indent
);
2447 ::OutputString(stream
, wxT("</image>"), handler
->GetConvMem(), handler
->GetConvFile());
2452 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2453 // Export this object to the given parent node, usually creating at least one child node.
2454 bool wxRichTextImage::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2456 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("image"));
2457 parent
->AddChild(elementNode
);
2459 if (GetImageBlock().IsOk())
2460 elementNode
->AddAttribute(wxT("imagetype"), MakeString((int) GetImageBlock().GetImageType()));
2462 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2463 handler
->WriteProperties(elementNode
, GetProperties());
2465 wxXmlNode
* dataNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("data"));
2466 elementNode
->AddChild(dataNode
);
2467 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2468 dataNode
->AddChild(textNode
);
2473 wxMemoryOutputStream stream
;
2474 if (GetImageBlock().WriteHex(stream
))
2476 if (stream
.GetSize() > 0)
2478 int size
= stream
.GetSize();
2480 int size2
= stream
.GetOutputStreamBuffer()->GetIntPosition();
2481 wxASSERT(size
== size2
);
2483 unsigned char* data
= new unsigned char[size
];
2484 stream
.CopyTo(data
, size
);
2485 strData
= wxString((const char*) data
, wxConvUTF8
, size
);
2489 strData
= wxEmptyString
;
2495 wxStringOutputStream
strStream(& strData
);
2496 GetImageBlock().WriteHex(strStream
);
2500 textNode
->SetContent(strData
);
2501 textNode
->SetNoConversion(true); // optimize speed
2508 // Import this object from XML
2509 bool wxRichTextParagraphLayoutBox::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2511 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2515 wxString partial
= node
->GetAttribute(wxT("partialparagraph"), wxEmptyString
);
2516 if (partial
== wxT("true"))
2517 SetPartialParagraph(true);
2519 wxXmlNode
* child
= wxRichTextXMLHandler::FindNode(node
, wxT("stylesheet"));
2520 if (child
&& (handler
->GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
2522 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
2523 wxString sheetName
= child
->GetAttribute(wxT("name"), wxEmptyString
);
2524 wxString sheetDescription
= child
->GetAttribute(wxT("description"), wxEmptyString
);
2525 sheet
->SetName(sheetName
);
2526 sheet
->SetDescription(sheetDescription
);
2528 wxXmlNode
* child2
= child
->GetChildren();
2531 handler
->ImportStyleDefinition(sheet
, child2
);
2533 child2
= child2
->GetNext();
2536 // Notify that styles have changed. If this is vetoed by the app,
2537 // the new sheet will be deleted. If it is not vetoed, the
2538 // old sheet will be deleted and replaced with the new one.
2539 buffer
->SetStyleSheetAndNotify(sheet
);
2545 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2546 // Export this object directly to the given stream.
2547 bool wxRichTextParagraphLayoutBox::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2549 ::OutputIndentation(stream
, indent
);
2550 wxString nodeName
= GetXMLNodeName();
2551 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2553 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2555 if (GetPartialParagraph())
2556 style
<< wxT(" partialparagraph=\"true\"");
2558 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2560 if (GetProperties().GetCount() > 0)
2562 handler
->WriteProperties(stream
, GetProperties(), indent
);
2566 for (i
= 0; i
< GetChildCount(); i
++)
2568 wxRichTextObject
* child
= GetChild(i
);
2569 child
->ExportXML(stream
, indent
+1, handler
);
2572 ::OutputIndentation(stream
, indent
);
2573 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2578 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2579 // Export this object to the given parent node, usually creating at least one child node.
2580 bool wxRichTextParagraphLayoutBox::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2582 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2583 parent
->AddChild(elementNode
);
2584 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2585 handler
->WriteProperties(elementNode
, GetProperties());
2587 if (GetPartialParagraph())
2588 elementNode
->AddAttribute(wxT("partialparagraph"), wxT("true"));
2591 for (i
= 0; i
< GetChildCount(); i
++)
2593 wxRichTextObject
* child
= GetChild(i
);
2594 child
->ExportXML(elementNode
, handler
);
2601 // Import this object from XML
2602 bool wxRichTextTable::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2604 wxRichTextBox::ImportFromXML(buffer
, node
, handler
, recurse
);
2608 m_rowCount
= wxAtoi(node
->GetAttribute(wxT("rows"), wxEmptyString
));
2609 m_colCount
= wxAtoi(node
->GetAttribute(wxT("cols"), wxEmptyString
));
2611 wxXmlNode
* child
= node
->GetChildren();
2614 wxRichTextObject
* childObj
= handler
->CreateObjectForXMLName(this, child
->GetName());
2617 AppendChild(childObj
);
2618 handler
->ImportXML(buffer
, childObj
, child
);
2620 child
= child
->GetNext();
2623 m_cells
.Add(wxRichTextObjectPtrArray(), m_rowCount
);
2625 for (i
= 0; i
< m_rowCount
; i
++)
2627 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
2628 for (j
= 0; j
< m_colCount
; j
++)
2630 int idx
= i
* m_colCount
+ j
;
2631 if (idx
< (int) GetChildren().GetCount())
2633 wxRichTextCell
* cell
= wxDynamicCast(GetChildren().Item(idx
)->GetData(), wxRichTextCell
);
2643 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2644 // Export this object directly to the given stream.
2645 bool wxRichTextTable::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2647 ::OutputIndentation(stream
, indent
);
2648 wxString nodeName
= GetXMLNodeName();
2649 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2651 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2653 style
<< wxT(" rows=\"") << m_rowCount
<< wxT("\"");
2654 style
<< wxT(" cols=\"") << m_colCount
<< wxT("\"");
2656 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2658 if (GetProperties().GetCount() > 0)
2660 handler
->WriteProperties(stream
, GetProperties(), indent
);
2664 for (i
= 0; i
< m_rowCount
; i
++)
2666 for (j
= 0; j
< m_colCount
; j
++)
2668 wxRichTextCell
* cell
= GetCell(i
, j
);
2669 cell
->ExportXML(stream
, indent
+1, handler
);
2673 ::OutputIndentation(stream
, indent
);
2674 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2680 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2681 // Export this object to the given parent node, usually creating at least one child node.
2682 bool wxRichTextTable::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2684 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2685 parent
->AddChild(elementNode
);
2686 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2687 handler
->WriteProperties(elementNode
, GetProperties());
2689 elementNode
->AddAttribute(wxT("rows"), wxString::Format(wxT("%d"), m_rowCount
));
2690 elementNode
->AddAttribute(wxT("cols"), wxString::Format(wxT("%d"), m_colCount
));
2693 for (i
= 0; i
< m_rowCount
; i
++)
2695 for (j
= 0; j
< m_colCount
; j
++)
2697 wxRichTextCell
* cell
= GetCell(i
, j
);
2698 cell
->ExportXML(elementNode
, handler
);
2708 // wxUSE_RICHTEXT && wxUSE_XML