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 wxRichTextBox
;
173 else if (name
== wxT("table"))
174 return new wxRichTextBox
;
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
)
418 // For use with earlier versions of wxWidgets
419 #ifndef WXUNUSED_IN_UNICODE
421 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
423 #define WXUNUSED_IN_UNICODE(x) x
427 // write string to output
428 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
429 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
), wxMBConv
*convFile
)
431 if (str
.empty()) return;
435 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
436 stream
.Write((const char*)buf
, strlen((const char*)buf
));
440 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
441 stream
.Write((const char*)buf
, strlen((const char*)buf
));
444 if ( convFile
== NULL
)
445 stream
.Write(str
.mb_str(), str
.Len());
448 wxString
str2(str
.wc_str(*convMem
), *convFile
);
449 stream
.Write(str2
.mb_str(), str2
.Len());
454 static void OutputIndentation(wxOutputStream
& stream
, int indent
)
456 wxString str
= wxT("\n");
457 for (int i
= 0; i
< indent
; i
++)
458 str
<< wxT(' ') << wxT(' ');
459 ::OutputString(stream
, str
, NULL
, NULL
);
462 // Same as above, but create entities first.
463 // Translates '<' to "<", '>' to ">" and '&' to "&"
464 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
465 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
473 for (i
= 0; i
< len
; i
++)
477 // Original code excluded "&" but we _do_ want to convert
478 // the ampersand beginning & because otherwise when read in,
479 // the original "&" becomes "&".
481 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
482 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
484 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
488 OutputString(stream
, wxT("<"), NULL
, NULL
);
491 OutputString(stream
, wxT(">"), NULL
, NULL
);
494 OutputString(stream
, wxT("&"), NULL
, NULL
);
497 OutputString(stream
, wxT("""), NULL
, NULL
);
503 else if (wxUChar(c
) > 127)
505 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
507 wxString
s(wxT("&#"));
511 s
<< (int) wxUChar(c
);
514 OutputString(stream
, s
, NULL
, NULL
);
518 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
521 void wxRichTextXMLHandler::OutputString(wxOutputStream
& stream
, const wxString
& str
)
523 ::OutputString(stream
, str
, m_convMem
, m_convFile
);
526 void wxRichTextXMLHandler::OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
)
528 ::OutputStringEnt(stream
, str
, m_convMem
, m_convFile
);
531 void wxRichTextXMLHandler::OutputIndentation(wxOutputStream
& stream
, int indent
)
533 wxString str
= wxT("\n");
534 for (int i
= 0; i
< indent
; i
++)
535 str
<< wxT(' ') << wxT(' ');
536 ::OutputString(stream
, str
, NULL
, NULL
);
539 wxString
wxRichTextXMLHandler::AttributeToXML(const wxString
& str
)
547 for (i
= 0; i
< len
; i
++)
551 // Original code excluded "&" but we _do_ want to convert
552 // the ampersand beginning & because otherwise when read in,
553 // the original "&" becomes "&".
555 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
556 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
558 str1
+= str
.Mid(last
, i
- last
);
568 str1
+= wxT("&");
571 str1
+= wxT(""");
577 else if (wxUChar(c
) > 127)
579 str1
+= str
.Mid(last
, i
- last
);
581 wxString
s(wxT("&#"));
585 s
<< (int) wxUChar(c
);
592 str1
+= str
.Mid(last
, i
- last
);
596 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
598 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const int& v
)
600 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%d"), v
) << wxT("\"");
603 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const long& v
)
605 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%ld"), v
) << wxT("\"");
608 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const double& v
)
610 str
<< wxT(" ") << name
<< wxT("=\"") << wxString::Format(wxT("%.2f"), (float) v
) << wxT("\"");
613 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxChar
* s
)
615 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
618 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxString
& s
)
620 str
<< wxT(" ") << name
<< wxT("=\"") << s
<< wxT("\"");
623 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxColour
& col
)
625 str
<< wxT(" ") << name
<< wxT("=\"") << wxT("#") << ColourToHexString(col
) << wxT("\"");
628 static inline void AddAttribute(wxString
& str
, const wxString
& name
, const wxTextAttrDimension
& dim
)
632 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString((int) dim
.GetFlags());
633 str
<< wxT(" ") << name
<< wxT("=\"");
639 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
641 if (dims
.GetLeft().IsValid())
642 AddAttribute(str
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
643 if (dims
.GetRight().IsValid())
644 AddAttribute(str
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
645 if (dims
.GetTop().IsValid())
646 AddAttribute(str
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
647 if (dims
.GetBottom().IsValid())
648 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
651 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
653 if (border
.HasStyle())
654 AddAttribute(str
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
655 if (border
.HasColour())
656 AddAttribute(str
, rootName
+ wxString(wxT("-color")), border
.GetColour());
657 if (border
.HasWidth())
658 AddAttribute(str
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
661 static inline void AddAttribute(wxString
& str
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
663 AddAttribute(str
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
664 AddAttribute(str
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
665 AddAttribute(str
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
666 AddAttribute(str
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
670 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
672 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
674 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const int& v
)
676 node
->AddAttribute(name
, MakeString(v
));
679 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const long& v
)
681 node
->AddAttribute(name
, MakeString(v
));
684 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const double& v
)
686 node
->AddAttribute(name
, MakeString(v
));
689 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxString
& s
)
691 node
->AddAttribute(name
, s
);
694 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxColour
& col
)
696 node
->AddAttribute(name
, MakeString(col
));
699 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& name
, const wxTextAttrDimension
& dim
)
703 wxString value
= MakeString(dim
.GetValue()) + wxT(",") + MakeString(dim
.GetFlags());
704 AddAttribute(node
, name
, value
);
708 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrDimensions
& dims
)
710 if (dims
.GetLeft().IsValid())
711 AddAttribute(node
, rootName
+ wxString(wxT("-left")), dims
.GetLeft());
712 if (dims
.GetRight().IsValid())
713 AddAttribute(node
, rootName
+ wxString(wxT("-right")), dims
.GetRight());
714 if (dims
.GetTop().IsValid())
715 AddAttribute(node
, rootName
+ wxString(wxT("-top")), dims
.GetTop());
716 if (dims
.GetBottom().IsValid())
717 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), dims
.GetBottom());
720 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorder
& border
)
722 if (border
.HasStyle())
723 AddAttribute(node
, rootName
+ wxString(wxT("-style")), border
.GetStyle());
724 if (border
.HasColour())
725 AddAttribute(node
, rootName
+ wxString(wxT("-color")), border
.GetColour());
726 if (border
.HasWidth())
727 AddAttribute(node
, rootName
+ wxString(wxT("-width")), border
.GetWidth());
730 static inline void AddAttribute(wxXmlNode
* node
, const wxString
& rootName
, const wxTextAttrBorders
& borders
)
732 AddAttribute(node
, rootName
+ wxString(wxT("-left")), borders
.GetLeft());
733 AddAttribute(node
, rootName
+ wxString(wxT("-right")), borders
.GetRight());
734 AddAttribute(node
, rootName
+ wxString(wxT("-top")), borders
.GetTop());
735 AddAttribute(node
, rootName
+ wxString(wxT("-bottom")), borders
.GetBottom());
738 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
740 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
745 wxString
version(wxT("1.0") ) ;
747 bool deleteConvFile
= false;
748 wxString fileEncoding
;
749 //wxMBConv* convFile = NULL;
752 fileEncoding
= wxT("UTF-8");
753 m_convFile
= & wxConvUTF8
;
755 fileEncoding
= wxT("ISO-8859-1");
756 m_convFile
= & wxConvISO8859_1
;
759 // If SetEncoding has been called, change the output encoding.
760 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
762 if (m_encoding
== wxT("<System>"))
765 fileEncoding
= wxLocale::GetSystemEncodingName();
766 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
771 fileEncoding
= m_encoding
;
774 // GetSystemEncodingName may not have returned a name
775 if (fileEncoding
.empty())
777 fileEncoding
= wxT("UTF-8");
779 fileEncoding
= wxT("ISO-8859-1");
781 m_convFile
= new wxCSConv(fileEncoding
);
782 deleteConvFile
= true;
785 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT
786 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
787 wxStopWatch stopwatch
;
789 wxXmlDocument
* doc
= new wxXmlDocument
;
790 doc
->SetFileEncoding(fileEncoding
);
792 wxXmlNode
* rootNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("richtext"));
793 doc
->SetRoot(rootNode
);
794 rootNode
->AddAttribute(wxT("version"), wxT("1.0.0.0"));
795 rootNode
->AddAttribute(wxT("xmlns"), wxT("http://www.wxwidgets.org"));
797 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
799 wxXmlNode
* styleSheetNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("stylesheet"));
800 rootNode
->AddChild(styleSheetNode
);
802 wxString nameAndDescr
;
804 if (!buffer
->GetStyleSheet()->GetName().empty())
805 styleSheetNode
->AddAttribute(wxT("name"), buffer
->GetStyleSheet()->GetName());
807 if (!buffer
->GetStyleSheet()->GetDescription().empty())
808 styleSheetNode
->AddAttribute(wxT("description"), buffer
->GetStyleSheet()->GetDescription());
811 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
813 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
814 ExportStyleDefinition(styleSheetNode
, def
);
817 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
819 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
820 ExportStyleDefinition(styleSheetNode
, def
);
823 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
825 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
826 ExportStyleDefinition(styleSheetNode
, def
);
829 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
831 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
832 ExportStyleDefinition(styleSheetNode
, def
);
835 bool success
= ExportXML(rootNode
, *buffer
);
836 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
837 long t
= stopwatch
.Time();
838 wxLogDebug(wxT("Creating the document took %ldms"), t
);
839 wxMessageBox(wxString::Format(wxT("Creating the document took %ldms"), t
));
843 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
846 success
= doc
->Save(stream
);
847 #if wxRICHTEXT_USE_OUTPUT_TIMINGS
849 wxLogDebug(wxT("Save() took %ldms"), t2
);
850 wxMessageBox(wxString::Format(wxT("Save() took %ldms"), t2
));
857 // !(wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT && wxRICHTEXT_USE_XMLDOCUMENT_OUTPUT)
860 m_convMem
= wxConvCurrent
;
866 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
867 version
, fileEncoding
);
868 OutputString(stream
, s
);
869 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">"));
873 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
875 OutputIndentation(stream
, level
);
876 wxString nameAndDescr
;
877 if (!buffer
->GetStyleSheet()->GetName().empty())
878 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
879 if (!buffer
->GetStyleSheet()->GetDescription().empty())
880 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
881 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"));
885 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
887 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
888 ExportStyleDefinition(stream
, def
, level
+ 1);
891 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
893 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
894 ExportStyleDefinition(stream
, def
, level
+ 1);
897 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
899 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
900 ExportStyleDefinition(stream
, def
, level
+ 1);
903 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetBoxStyleCount(); i
++)
905 wxRichTextBoxStyleDefinition
* def
= buffer
->GetStyleSheet()->GetBoxStyle(i
);
906 ExportStyleDefinition(stream
, def
, level
+ 1);
909 OutputIndentation(stream
, level
);
910 OutputString(stream
, wxT("</stylesheet>"));
914 bool success
= ExportXML(stream
, *buffer
, level
);
916 OutputString(stream
, wxT("\n</richtext>"));
917 OutputString(stream
, wxT("\n"));
928 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
930 /// Recursively export an object
931 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxRichTextObject
& obj
, int indent
)
933 obj
.ExportXML(stream
, indent
, this);
938 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxRichTextStyleDefinition
* def
, int level
)
940 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
941 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
942 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
943 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
945 wxString baseStyle
= def
->GetBaseStyle();
946 wxString baseStyleProp
;
947 if (!baseStyle
.empty())
948 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
950 wxString descr
= def
->GetDescription();
953 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
957 OutputIndentation(stream
, level
);
958 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
962 wxString style
= AddAttributes(def
->GetStyle(), false);
964 OutputIndentation(stream
, level
);
965 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
967 OutputIndentation(stream
, level
);
968 OutputString(stream
, wxT("</style>"));
972 OutputIndentation(stream
, level
);
973 OutputString(stream
, wxT("</characterstyle>"));
977 OutputIndentation(stream
, level
);
979 if (!listDef
->GetNextStyle().empty())
980 baseStyleProp
<< wxT(" nextstyle=\"") << listDef
->GetNextStyle() << wxT("\"");
982 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"));
986 wxString style
= AddAttributes(def
->GetStyle(), true);
988 OutputIndentation(stream
, level
);
989 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
991 OutputIndentation(stream
, level
);
992 OutputString(stream
, wxT("</style>"));
995 for (i
= 0; i
< 10; i
++)
997 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1000 wxString style
= AddAttributes(def
->GetStyle(), true);
1001 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
1003 OutputIndentation(stream
, level
);
1004 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"));
1006 OutputIndentation(stream
, level
);
1007 OutputString(stream
, wxT("</style>"));
1013 OutputIndentation(stream
, level
);
1014 OutputString(stream
, wxT("</liststyle>"));
1018 OutputIndentation(stream
, level
);
1020 if (!paraDef
->GetNextStyle().empty())
1021 baseStyleProp
<< wxT(" nextstyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
1023 OutputString(stream
, wxT("<paragraphstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
1027 wxString style
= AddAttributes(def
->GetStyle(), true);
1029 OutputIndentation(stream
, level
);
1030 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1032 OutputIndentation(stream
, level
);
1033 OutputString(stream
, wxT("</style>"));
1037 OutputIndentation(stream
, level
);
1038 OutputString(stream
, wxT("</paragraphstyle>"));
1042 OutputIndentation(stream
, level
);
1044 OutputString(stream
, wxT("<boxstyle") + baseStyleProp
+ descrProp
+ wxT(">"));
1048 wxString style
= AddAttributes(def
->GetStyle(), true);
1050 OutputIndentation(stream
, level
);
1051 OutputString(stream
, wxT("<style ") + style
+ wxT(">"));
1053 OutputIndentation(stream
, level
);
1054 OutputString(stream
, wxT("</style>"));
1058 OutputIndentation(stream
, level
);
1059 OutputString(stream
, wxT("</boxstyle>"));
1066 /// Create a string containing style attributes
1067 wxString
wxRichTextXMLHandler::AddAttributes(const wxRichTextAttr
& attr
, bool isPara
)
1070 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
1071 AddAttribute(str
, wxT("textcolor"), attr
.GetTextColour());
1073 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
1074 AddAttribute(str
, wxT("bgcolor"), attr
.GetBackgroundColour());
1076 if (attr
.HasFontSize())
1077 AddAttribute(str
, wxT("fontsize"), attr
.GetFontSize());
1079 if (attr
.HasFontFamily())
1080 AddAttribute(str
, wxT("fontfamily"), attr
.GetFontFamily());
1082 if (attr
.HasFontItalic())
1083 AddAttribute(str
, wxT("fontstyle"), attr
.GetFontStyle());
1085 if (attr
.HasFontWeight())
1086 AddAttribute(str
, wxT("fontweight"), attr
.GetFontWeight());
1088 if (attr
.HasFontUnderlined())
1089 AddAttribute(str
, wxT("fontunderlined"), (int) attr
.GetFontUnderlined());
1091 if (attr
.HasFontFaceName())
1092 AddAttribute(str
, wxT("fontface"), attr
.GetFontFaceName());
1094 if (attr
.HasTextEffects())
1096 AddAttribute(str
, wxT("texteffects"), attr
.GetTextEffects());
1097 AddAttribute(str
, wxT("texteffectflags"), attr
.GetTextEffectFlags());
1100 if (!attr
.GetCharacterStyleName().empty())
1101 AddAttribute(str
, wxT("characterstyle"), attr
.GetCharacterStyleName());
1104 AddAttribute(str
, wxT("url"), AttributeToXML(attr
.GetURL()));
1108 if (attr
.HasAlignment())
1109 AddAttribute(str
, wxT("alignment"), (int) attr
.GetAlignment());
1111 if (attr
.HasLeftIndent())
1113 AddAttribute(str
, wxT("leftindent"), (int) attr
.GetLeftIndent());
1114 AddAttribute(str
, wxT("leftsubindent"), (int) attr
.GetLeftSubIndent());
1117 if (attr
.HasRightIndent())
1118 AddAttribute(str
, wxT("rightindent"), (int) attr
.GetRightIndent());
1120 if (attr
.HasParagraphSpacingAfter())
1121 AddAttribute(str
, wxT("parspacingafter"), (int) attr
.GetParagraphSpacingAfter());
1123 if (attr
.HasParagraphSpacingBefore())
1124 AddAttribute(str
, wxT("parspacingbefore"), (int) attr
.GetParagraphSpacingBefore());
1126 if (attr
.HasLineSpacing())
1127 AddAttribute(str
, wxT("linespacing"), (int) attr
.GetLineSpacing());
1129 if (attr
.HasBulletStyle())
1130 AddAttribute(str
, wxT("bulletstyle"), (int) attr
.GetBulletStyle());
1132 if (attr
.HasBulletNumber())
1133 AddAttribute(str
, wxT("bulletnumber"), (int) attr
.GetBulletNumber());
1135 if (attr
.HasBulletText())
1137 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1138 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1139 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1140 AddAttribute(str
, wxT("bulletsymbol"), (int) (attr
.GetBulletText()[0]));
1142 AddAttribute(str
, wxT("bullettext"), attr
.GetBulletText());
1144 AddAttribute(str
, wxT("bulletfont"), attr
.GetBulletFont());
1147 if (attr
.HasBulletName())
1148 AddAttribute(str
, wxT("bulletname"), attr
.GetBulletName());
1150 if (!attr
.GetParagraphStyleName().empty())
1151 AddAttribute(str
, wxT("parstyle"), attr
.GetParagraphStyleName());
1153 if (!attr
.GetListStyleName().empty())
1154 AddAttribute(str
, wxT("liststyle"), attr
.GetListStyleName());
1160 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1162 if (i
> 0) strTabs
<< wxT(",");
1163 strTabs
<< attr
.GetTabs()[i
];
1165 AddAttribute(str
, wxT("tabs"), strTabs
);
1168 if (attr
.HasPageBreak())
1170 AddAttribute(str
, wxT("pagebreak"), 1);
1173 if (attr
.HasOutlineLevel())
1174 AddAttribute(str
, wxT("outlinelevel"), (int) attr
.GetOutlineLevel());
1177 AddAttribute(str
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1178 AddAttribute(str
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1179 AddAttribute(str
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1180 AddAttribute(str
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1181 AddAttribute(str
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1182 AddAttribute(str
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1183 AddAttribute(str
, wxT("height"), attr
.GetTextBoxAttr().GetWidth());
1185 if (attr
.GetTextBoxAttr().HasFloatMode())
1188 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1189 value
= wxT("left");
1190 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1191 value
= wxT("right");
1193 value
= wxT("none");
1194 AddAttribute(str
, wxT("float"), value
);
1197 if (attr
.GetTextBoxAttr().HasClearMode())
1200 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1201 value
= wxT("left");
1202 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1203 value
= wxT("right");
1204 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1205 value
= wxT("both");
1207 value
= wxT("none");
1208 AddAttribute(str
, wxT("clear"), value
);
1211 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1212 AddAttribute(str
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1217 // Make a string from the given property. This can be overridden for custom variants.
1218 wxString
wxRichTextXMLHandler::MakeStringFromProperty(const wxVariant
& var
)
1220 return var
.MakeString();
1223 // Create a proprty from the string read from the XML file.
1224 wxVariant
wxRichTextXMLHandler::MakePropertyFromString(const wxString
& name
, const wxString
& value
, const wxString
& WXUNUSED(type
))
1226 wxVariant
var(value
, name
);
1227 // TODO: use type to create using common types
1231 // Write the properties
1232 bool wxRichTextXMLHandler::WriteProperties(wxOutputStream
& stream
, const wxRichTextProperties
& properties
, int level
)
1234 if (properties
.GetCount() > 0)
1238 OutputIndentation(stream
, level
);
1239 OutputString(stream
, wxT("<properties"));
1244 for (i
= 0; i
< properties
.GetCount(); i
++)
1246 const wxVariant
& var
= properties
[i
];
1249 const wxString
& name
= var
.GetName();
1250 wxString value
= MakeStringFromProperty(var
);
1252 OutputIndentation(stream
, level
);
1253 OutputString(stream
, wxT("<property name=\"") + name
+
1254 wxT("\" type=\"") + var
.GetType() + wxT("\" value=\""));
1255 OutputStringEnt(stream
, value
);
1256 OutputString(stream
, wxT("\"/>\n"));
1262 OutputIndentation(stream
, level
);
1263 OutputString(stream
, wxT("</properties>\n"));
1273 // wxRICHTEXT_HAVE_DIRECT_OUTPUT
1275 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1276 bool wxRichTextXMLHandler::ExportXML(wxXmlNode
* parent
, wxRichTextObject
& obj
)
1278 obj
.ExportXML(parent
, this);
1283 bool wxRichTextXMLHandler::ExportStyleDefinition(wxXmlNode
* parent
, wxRichTextStyleDefinition
* def
)
1285 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
1286 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
1287 wxRichTextBoxStyleDefinition
* boxDef
= wxDynamicCast(def
, wxRichTextBoxStyleDefinition
);
1288 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
1290 wxString baseStyle
= def
->GetBaseStyle();
1291 wxString descr
= def
->GetDescription();
1293 wxXmlNode
* defNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxEmptyString
);
1294 parent
->AddChild(defNode
);
1295 if (!baseStyle
.empty())
1296 defNode
->AddAttribute(wxT("basestyle"), baseStyle
);
1298 defNode
->AddAttribute(wxT("description"), descr
);
1300 wxXmlNode
* styleNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1301 defNode
->AddChild(styleNode
);
1305 defNode
->SetName(wxT("characterstyle"));
1306 AddAttributes(styleNode
, def
->GetStyle(), false);
1310 defNode
->SetName(wxT("liststyle"));
1312 if (!listDef
->GetNextStyle().empty())
1313 defNode
->AddAttribute(wxT("nextstyle"), listDef
->GetNextStyle());
1315 AddAttributes(styleNode
, def
->GetStyle(), true);
1318 for (i
= 0; i
< 10; i
++)
1320 wxRichTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
1323 wxXmlNode
* levelNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("style"));
1324 defNode
->AddChild(levelNode
);
1325 levelNode
->AddAttribute(wxT("level"), MakeString(i
+1));
1326 AddAttributes(levelNode
, * levelAttr
, true);
1332 defNode
->SetName(wxT("boxstyle"));
1334 AddAttributes(styleNode
, def
->GetStyle(), true);
1338 defNode
->SetName(wxT("paragraphstyle"));
1340 if (!paraDef
->GetNextStyle().empty())
1341 defNode
->AddAttribute(wxT("nextstyle"), paraDef
->GetNextStyle());
1343 AddAttributes(styleNode
, def
->GetStyle(), true);
1349 bool wxRichTextXMLHandler::AddAttributes(wxXmlNode
* node
, wxRichTextAttr
& attr
, bool isPara
)
1351 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
1352 node
->AddAttribute(wxT("textcolor"), MakeString(attr
.GetTextColour()));
1353 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
1354 node
->AddAttribute(wxT("bgcolor"), MakeString(attr
.GetBackgroundColour()));
1356 if (attr
.HasFontSize())
1357 node
->AddAttribute(wxT("fontsize"), MakeString(attr
.GetFontSize()));
1358 if (attr
.HasFontFamily())
1359 node
->AddAttribute(wxT("fontfamily"), MakeString(attr
.GetFontFamily()));
1360 if (attr
.HasFontItalic())
1361 node
->AddAttribute(wxT("fontstyle"), MakeString(attr
.GetFontStyle()));
1362 if (attr
.HasFontWeight())
1363 node
->AddAttribute(wxT("fontweight"), MakeString(attr
.GetFontWeight()));
1364 if (attr
.HasFontUnderlined())
1365 node
->AddAttribute(wxT("fontunderlined"), MakeString((int) attr
.GetFontUnderlined()));
1366 if (attr
.HasFontFaceName())
1367 node
->AddAttribute(wxT("fontface"), attr
.GetFontFaceName());
1369 if (attr
.HasTextEffects())
1371 node
->AddAttribute(wxT("texteffects"), MakeString(attr
.GetTextEffects()));
1372 node
->AddAttribute(wxT("texteffectflags"), MakeString(attr
.GetTextEffectFlags()));
1374 if (attr
.HasCharacterStyleName() && !attr
.GetCharacterStyleName().empty())
1375 node
->AddAttribute(wxT("characterstyle"), attr
.GetCharacterStyleName());
1378 node
->AddAttribute(wxT("url"), attr
.GetURL()); // TODO: do we need to wrap this in AttributeToXML?
1382 if (attr
.HasAlignment())
1383 node
->AddAttribute(wxT("alignment"), MakeString((int) attr
.GetAlignment()));
1385 if (attr
.HasLeftIndent())
1387 node
->AddAttribute(wxT("leftindent"), MakeString((int) attr
.GetLeftIndent()));
1388 node
->AddAttribute(wxT("leftsubindent"), MakeString((int) attr
.GetLeftSubIndent()));
1391 if (attr
.HasRightIndent())
1392 node
->AddAttribute(wxT("rightindent"), MakeString((int) attr
.GetRightIndent()));
1394 if (attr
.HasParagraphSpacingAfter())
1395 node
->AddAttribute(wxT("parspacingafter"), MakeString((int) attr
.GetParagraphSpacingAfter()));
1397 if (attr
.HasParagraphSpacingBefore())
1398 node
->AddAttribute(wxT("parspacingbefore"), MakeString((int) attr
.GetParagraphSpacingBefore()));
1400 if (attr
.HasLineSpacing())
1401 node
->AddAttribute(wxT("linespacing"), MakeString((int) attr
.GetLineSpacing()));
1403 if (attr
.HasBulletStyle())
1404 node
->AddAttribute(wxT("bulletstyle"), MakeString((int) attr
.GetBulletStyle()));
1406 if (attr
.HasBulletNumber())
1407 node
->AddAttribute(wxT("bulletnumber"), MakeString((int) attr
.GetBulletNumber()));
1409 if (attr
.HasBulletText())
1411 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1412 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1413 if (!attr
.GetBulletText().empty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1414 node
->AddAttribute(wxT("bulletsymbol"), MakeString((int) (attr
.GetBulletText()[0])));
1416 node
->AddAttribute(wxT("bullettext"), attr
.GetBulletText());
1418 if (!attr
.GetBulletFont().empty())
1419 node
->AddAttribute(wxT("bulletfont"), attr
.GetBulletFont());
1422 if (attr
.HasBulletName())
1423 node
->AddAttribute(wxT("bulletname"), attr
.GetBulletName());
1425 if (!attr
.GetParagraphStyleName().empty())
1426 node
->AddAttribute(wxT("parstyle"), attr
.GetParagraphStyleName());
1428 if (!attr
.GetListStyleName().empty())
1429 node
->AddAttribute(wxT("liststyle"), attr
.GetListStyleName());
1435 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1439 tabs
<< attr
.GetTabs()[i
];
1441 node
->AddAttribute(wxT("tabs"), tabs
);
1444 if (attr
.HasPageBreak())
1445 node
->AddAttribute(wxT("pagebreak"), wxT("1"));
1447 if (attr
.HasOutlineLevel())
1448 node
->AddAttribute(wxT("outlinelevel"), MakeString((int) attr
.GetOutlineLevel()));
1451 AddAttribute(node
, wxT("margin"), attr
.GetTextBoxAttr().GetMargins());
1452 AddAttribute(node
, wxT("padding"), attr
.GetTextBoxAttr().GetPadding());
1453 AddAttribute(node
, wxT("position"), attr
.GetTextBoxAttr().GetPosition());
1454 AddAttribute(node
, wxT("border"), attr
.GetTextBoxAttr().GetBorder());
1455 AddAttribute(node
, wxT("outline"), attr
.GetTextBoxAttr().GetOutline());
1456 AddAttribute(node
, wxT("width"), attr
.GetTextBoxAttr().GetWidth());
1457 AddAttribute(node
, wxT("height"), attr
.GetTextBoxAttr().GetWidth());
1459 if (attr
.GetTextBoxAttr().HasFloatMode())
1462 if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_LEFT
)
1463 value
= wxT("left");
1464 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_FLOAT_RIGHT
)
1465 value
= wxT("right");
1467 value
= wxT("none");
1468 AddAttribute(node
, wxT("float"), value
);
1471 if (attr
.GetTextBoxAttr().HasClearMode())
1474 if (attr
.GetTextBoxAttr().GetClearMode() == wxTEXT_BOX_ATTR_CLEAR_LEFT
)
1475 value
= wxT("left");
1476 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_CLEAR_RIGHT
)
1477 value
= wxT("right");
1478 else if (attr
.GetTextBoxAttr().GetFloatMode() == wxTEXT_BOX_ATTR_CLEAR_BOTH
)
1479 value
= wxT("both");
1481 value
= wxT("none");
1482 AddAttribute(node
, wxT("clear"), value
);
1485 if (attr
.GetTextBoxAttr().HasCollapseBorders())
1486 AddAttribute(node
, wxT("collapse-borders"), (int) attr
.GetTextBoxAttr().GetCollapseBorders());
1491 bool wxRichTextXMLHandler::WriteProperties(wxXmlNode
* node
, const wxRichTextProperties
& properties
)
1493 if (properties
.GetCount() > 0)
1495 wxXmlNode
* propertiesNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("properties"));
1496 node
->AddChild(propertiesNode
);
1498 for (i
= 0; i
< properties
.GetCount(); i
++)
1500 const wxVariant
& var
= properties
[i
];
1503 wxXmlNode
* propertyNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("property"));
1504 propertiesNode
->AddChild(propertyNode
);
1506 const wxString
& name
= var
.GetName();
1507 wxString value
= MakeStringFromProperty(var
);
1509 AddAttribute(propertyNode
, wxT("name"), name
);
1510 AddAttribute(propertyNode
, wxT("type"), var
.GetType());
1511 AddAttribute(propertyNode
, wxT("value"), value
);
1519 // wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1521 /// Replace face name with current name for platform.
1522 /// TODO: introduce a virtual function or settable table to
1523 /// do this comprehensively.
1524 bool wxRichTextFixFaceName(wxString
& facename
)
1526 if (facename
.empty())
1530 if (facename
== wxT("Times"))
1532 facename
= wxT("Times New Roman");
1535 else if (facename
== wxT("Helvetica"))
1537 facename
= wxT("Arial");
1540 else if (facename
== wxT("Courier"))
1542 facename
= wxT("Courier New");
1548 if (facename
== wxT("Times New Roman"))
1550 facename
= wxT("Times");
1553 else if (facename
== wxT("Arial"))
1555 facename
= wxT("Helvetica");
1558 else if (facename
== wxT("Courier New"))
1560 facename
= wxT("Courier");
1568 static inline long wxRichTextColourStringToLong(const wxString
& colStr
)
1570 if (!colStr
.IsEmpty())
1572 wxColour
col(colStr
);
1573 return col
.GetRGB();
1579 static inline wxTextAttrDimension
wxRichTextParseDimension(const wxString
& dimStr
)
1581 wxString valuePart
= dimStr
.BeforeFirst(wxT(','));
1583 if (dimStr
.Contains(wxT(",")))
1584 flagsPart
= dimStr
.AfterFirst(wxT(','));
1585 wxTextAttrDimension dim
;
1586 dim
.SetValue(wxAtoi(valuePart
));
1587 dim
.SetFlags(wxAtoi(flagsPart
));
1592 /// Import style parameters
1593 bool wxRichTextXMLHandler::ImportStyle(wxRichTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1595 wxXmlAttribute
* xmlAttr
= node
->GetAttributes();
1599 const wxString
& name
= xmlAttr
->GetName();
1600 const wxString
& value
= xmlAttr
->GetValue();
1603 if (name
== wxT("fontface"))
1608 if (GetFlags() & wxRICHTEXT_HANDLER_CONVERT_FACENAMES
)
1609 wxRichTextFixFaceName(v
);
1610 attr
.SetFontFaceName(v
);
1613 else if (name
== wxT("fontfamily"))
1616 attr
.SetFontFamily((wxFontFamily
)wxAtoi(value
));
1618 else if (name
== wxT("fontstyle"))
1621 attr
.SetFontStyle((wxFontStyle
)wxAtoi(value
));
1623 else if (name
== wxT("fontsize"))
1626 attr
.SetFontSize(wxAtoi(value
));
1628 else if (name
== wxT("fontweight"))
1631 attr
.SetFontWeight((wxFontWeight
) wxAtoi(value
));
1633 else if (name
== wxT("fontunderlined"))
1636 attr
.SetFontUnderlined(wxAtoi(value
) != 0);
1638 else if (name
== wxT("textcolor"))
1642 if (value
[0] == wxT('#'))
1643 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1645 attr
.SetTextColour(value
);
1648 else if (name
== wxT("bgcolor"))
1652 if (value
[0] == wxT('#'))
1653 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1655 attr
.SetBackgroundColour(value
);
1658 else if (name
== wxT("characterstyle"))
1661 attr
.SetCharacterStyleName(value
);
1663 else if (name
== wxT("texteffects"))
1666 attr
.SetTextEffects(wxAtoi(value
));
1668 else if (name
== wxT("texteffectflags"))
1671 attr
.SetTextEffectFlags(wxAtoi(value
));
1673 else if (name
== wxT("url"))
1680 if (name
== wxT("alignment"))
1683 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1685 else if (name
== wxT("leftindent"))
1688 attr
.SetLeftIndent(wxAtoi(value
), attr
.GetLeftSubIndent());
1690 else if (name
== wxT("leftsubindent"))
1693 attr
.SetLeftIndent(attr
.GetLeftIndent(), wxAtoi(value
));
1695 else if (name
== wxT("rightindent"))
1698 attr
.SetRightIndent(wxAtoi(value
));
1700 else if (name
== wxT("parspacingbefore"))
1703 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1705 else if (name
== wxT("parspacingafter"))
1708 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1710 else if (name
== wxT("linespacing"))
1713 attr
.SetLineSpacing(wxAtoi(value
));
1715 else if (name
== wxT("bulletstyle"))
1718 attr
.SetBulletStyle(wxAtoi(value
));
1720 else if (name
== wxT("bulletnumber"))
1723 attr
.SetBulletNumber(wxAtoi(value
));
1725 else if (name
== wxT("bulletsymbol"))
1729 wxChar ch
= wxAtoi(value
);
1732 attr
.SetBulletText(s
);
1735 else if (name
== wxT("bullettext"))
1739 attr
.SetBulletText(value
);
1742 else if (name
== wxT("bulletfont"))
1746 attr
.SetBulletFont(value
);
1749 else if (name
== wxT("bulletname"))
1753 attr
.SetBulletName(value
);
1756 else if (name
== wxT("parstyle"))
1760 attr
.SetParagraphStyleName(value
);
1763 else if (name
== wxT("liststyle"))
1767 attr
.SetListStyleName(value
);
1770 else if (name
== wxT("tabs"))
1775 wxStringTokenizer
tkz(value
, wxT(","));
1776 while (tkz
.HasMoreTokens())
1778 wxString token
= tkz
.GetNextToken();
1779 tabs
.Add(wxAtoi(token
));
1784 else if (name
== wxT("pagebreak"))
1788 attr
.SetPageBreak(wxAtoi(value
) != 0);
1791 else if (name
== wxT("outlinelevel"))
1795 attr
.SetOutlineLevel(wxAtoi(value
));
1808 if (name
== wxT("width"))
1810 attr
.GetTextBoxAttr().GetWidth().SetValue(wxRichTextParseDimension(value
));
1812 else if (name
== wxT("height"))
1814 attr
.GetTextBoxAttr().GetHeight().SetValue(wxRichTextParseDimension(value
));
1817 else if (name
== wxT("float"))
1819 if (value
== wxT("left"))
1820 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT
);
1821 else if (value
== wxT("right"))
1822 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT
);
1823 else if (value
== wxT("none"))
1824 attr
.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_NONE
);
1826 else if (name
== wxT("clear"))
1828 if (value
== wxT("left"))
1829 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_LEFT
);
1830 else if (value
== wxT("right"))
1831 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_RIGHT
);
1832 else if (value
== wxT("both"))
1833 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_BOTH
);
1834 else if (value
== wxT("none"))
1835 attr
.GetTextBoxAttr().SetClearMode(wxTEXT_BOX_ATTR_CLEAR_NONE
);
1837 else if (name
== wxT("collapse-borders"))
1838 attr
.GetTextBoxAttr().SetCollapseBorders((wxTextBoxAttrCollapseMode
) wxAtoi(value
));
1840 else if (name
.Contains(wxT("border-")))
1842 if (name
== wxT("border-left-style"))
1843 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetStyle(wxAtoi(value
));
1844 else if (name
== wxT("border-right-style"))
1845 attr
.GetTextBoxAttr().GetBorder().GetRight().SetStyle(wxAtoi(value
));
1846 else if (name
== wxT("border-top-style"))
1847 attr
.GetTextBoxAttr().GetBorder().GetTop().SetStyle(wxAtoi(value
));
1848 else if (name
== wxT("border-bottom-style"))
1849 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetStyle(wxAtoi(value
));
1851 else if (name
== wxT("border-left-colour"))
1852 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1853 else if (name
== wxT("border-right-colour"))
1854 attr
.GetTextBoxAttr().GetBorder().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1855 else if (name
== wxT("border-top-colour"))
1856 attr
.GetTextBoxAttr().GetBorder().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1857 else if (name
== wxT("border-bottom-colour"))
1858 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1860 else if (name
== wxT("border-left-width"))
1861 attr
.GetTextBoxAttr().GetBorder().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1862 else if (name
== wxT("border-right-width"))
1863 attr
.GetTextBoxAttr().GetBorder().GetRight().SetWidth(wxRichTextParseDimension(value
));
1864 else if (name
== wxT("border-top-width"))
1865 attr
.GetTextBoxAttr().GetBorder().GetTop().SetWidth(wxRichTextParseDimension(value
));
1866 else if (name
== wxT("border-bottom-width"))
1867 attr
.GetTextBoxAttr().GetBorder().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1869 else if (name
.Contains(wxT("outline-")))
1871 if (name
== wxT("outline-left-style"))
1872 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetStyle(wxAtoi(value
));
1873 else if (name
== wxT("outline-right-style"))
1874 attr
.GetTextBoxAttr().GetOutline().GetRight().SetStyle(wxAtoi(value
));
1875 else if (name
== wxT("outline-top-style"))
1876 attr
.GetTextBoxAttr().GetOutline().GetTop().SetStyle(wxAtoi(value
));
1877 else if (name
== wxT("outline-bottom-style"))
1878 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetStyle(wxAtoi(value
));
1880 else if (name
== wxT("outline-left-colour"))
1881 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetColour(wxRichTextColourStringToLong(value
));
1882 else if (name
== wxT("outline-right-colour"))
1883 attr
.GetTextBoxAttr().GetOutline().GetRight().SetColour(wxRichTextColourStringToLong(value
));
1884 else if (name
== wxT("outline-top-colour"))
1885 attr
.GetTextBoxAttr().GetOutline().GetTop().SetColour(wxRichTextColourStringToLong(value
));
1886 else if (name
== wxT("outline-bottom-colour"))
1887 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetColour(wxRichTextColourStringToLong(value
));
1889 else if (name
== wxT("outline-left-width"))
1890 attr
.GetTextBoxAttr().GetOutline().GetLeft().SetWidth(wxRichTextParseDimension(value
));
1891 else if (name
== wxT("outline-right-width"))
1892 attr
.GetTextBoxAttr().GetOutline().GetRight().SetWidth(wxRichTextParseDimension(value
));
1893 else if (name
== wxT("outline-top-width"))
1894 attr
.GetTextBoxAttr().GetOutline().GetTop().SetWidth(wxRichTextParseDimension(value
));
1895 else if (name
== wxT("outline-bottom-width"))
1896 attr
.GetTextBoxAttr().GetOutline().GetBottom().SetWidth(wxRichTextParseDimension(value
));
1898 else if (name
.Contains(wxT("margin-")))
1900 if (name
== wxT("margin-left"))
1901 attr
.GetTextBoxAttr().GetMargins().GetLeft().SetValue(wxRichTextParseDimension(value
));
1902 else if (name
== wxT("margin-right"))
1903 attr
.GetTextBoxAttr().GetMargins().GetRight().SetValue(wxRichTextParseDimension(value
));
1904 else if (name
== wxT("margin-top"))
1905 attr
.GetTextBoxAttr().GetMargins().GetTop().SetValue(wxRichTextParseDimension(value
));
1906 else if (name
== wxT("margin-bottom"))
1907 attr
.GetTextBoxAttr().GetMargins().GetBottom().SetValue(wxRichTextParseDimension(value
));
1909 else if (name
.Contains(wxT("padding-")))
1911 if (name
== wxT("padding-left"))
1912 attr
.GetTextBoxAttr().GetPadding().GetLeft().SetValue(wxRichTextParseDimension(value
));
1913 else if (name
== wxT("padding-right"))
1914 attr
.GetTextBoxAttr().GetPadding().GetRight().SetValue(wxRichTextParseDimension(value
));
1915 else if (name
== wxT("padding-top"))
1916 attr
.GetTextBoxAttr().GetPadding().GetTop().SetValue(wxRichTextParseDimension(value
));
1917 else if (name
== wxT("padding-bottom"))
1918 attr
.GetTextBoxAttr().GetPadding().GetBottom().SetValue(wxRichTextParseDimension(value
));
1920 else if (name
.Contains(wxT("position-")))
1922 if (name
== wxT("position-left"))
1923 attr
.GetTextBoxAttr().GetPosition().GetLeft().SetValue(wxRichTextParseDimension(value
));
1924 else if (name
== wxT("position-right"))
1925 attr
.GetTextBoxAttr().GetPosition().GetRight().SetValue(wxRichTextParseDimension(value
));
1926 else if (name
== wxT("position-top"))
1927 attr
.GetTextBoxAttr().GetPosition().GetTop().SetValue(wxRichTextParseDimension(value
));
1928 else if (name
== wxT("position-bottom"))
1929 attr
.GetTextBoxAttr().GetPosition().GetBottom().SetValue(wxRichTextParseDimension(value
));
1933 xmlAttr
= xmlAttr
->GetNext();
1942 // Import this object from XML
1943 bool wxRichTextObject::ImportFromXML(wxRichTextBuffer
* WXUNUSED(buffer
), wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
1945 handler
->ImportProperties(this, node
);
1946 handler
->ImportStyle(GetAttributes(), node
, UsesParagraphAttributes());
1953 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
1954 // Export this object directly to the given stream.
1955 bool wxRichTextObject::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
1957 ::OutputIndentation(stream
, indent
);
1958 ::OutputString(stream
, wxT("<") + GetXMLNodeName(), handler
->GetConvMem(), handler
->GetConvFile());
1960 wxString style
= handler
->AddAttributes(GetAttributes(), true);
1962 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
1964 if (GetProperties().GetCount() > 0)
1966 handler
->WriteProperties(stream
, GetProperties(), indent
);
1969 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
1973 for (i
= 0; i
< composite
->GetChildCount(); i
++)
1975 wxRichTextObject
* child
= composite
->GetChild(i
);
1976 child
->ExportXML(stream
, indent
+1, handler
);
1980 ::OutputIndentation(stream
, indent
);
1981 ::OutputString(stream
, wxT("</") + GetXMLNodeName() + wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
1986 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
1987 // Export this object to the given parent node, usually creating at least one child node.
1988 bool wxRichTextObject::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
1990 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
1991 parent
->AddChild(elementNode
);
1992 handler
->AddAttributes(elementNode
, GetAttributes(), true);
1993 handler
->WriteProperties(elementNode
, GetProperties());
1995 wxRichTextCompositeObject
* composite
= wxDynamicCast(this, wxRichTextCompositeObject
);
1999 for (i
= 0; i
< composite
->GetChildCount(); i
++)
2001 wxRichTextObject
* child
= composite
->GetChild(i
);
2002 child
->ExportXML(elementNode
, handler
);
2010 // Import this object from XML
2011 bool wxRichTextPlainText::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2013 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2015 if (node
->GetName() == wxT("text"))
2018 wxXmlNode
* textChild
= node
->GetChildren();
2021 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2022 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2024 wxString text2
= textChild
->GetContent();
2026 // Strip whitespace from end
2027 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
2028 text2
= text2
.Mid(0, text2
.length()-1);
2030 if (!text2
.empty() && text2
[0] == wxT('"'))
2031 text2
= text2
.Mid(1);
2032 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
2033 text2
= text2
.Mid(0, text2
.length() - 1);
2037 textChild
= textChild
->GetNext();
2042 else if (node
->GetName() == wxT("symbol"))
2044 // This is a symbol that XML can't read in the normal way
2046 wxXmlNode
* textChild
= node
->GetChildren();
2049 if (textChild
->GetType() == wxXML_TEXT_NODE
||
2050 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
2052 wxString text2
= textChild
->GetContent();
2055 textChild
= textChild
->GetNext();
2058 wxString actualText
;
2059 actualText
<< (wxChar
) wxAtoi(text
);
2060 SetText(actualText
);
2068 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2069 // Export this object directly to the given stream.
2070 bool wxRichTextPlainText::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2072 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2076 const wxString
& text
= GetText();
2077 int len
= (int) text
.Length();
2082 ::OutputIndentation(stream
, indent
);
2083 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2084 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2085 if (GetProperties().GetCount() > 0)
2087 handler
->WriteProperties(stream
, GetProperties(), indent
);
2088 ::OutputIndentation(stream
, indent
);
2090 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2092 else for (i
= 0; i
< len
; i
++)
2095 int c
= (int) text
[i
];
2097 int c
= (int) wxUChar(text
[i
]);
2099 if ((c
< 32 || c
== 34) && /* c != 9 && */ c
!= 10 && c
!= 13)
2103 wxString
fragment(text
.Mid(last
, i
-last
));
2104 if (!fragment
.empty())
2106 ::OutputIndentation(stream
, indent
);
2107 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2109 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2111 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2113 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2114 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2115 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2118 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2120 if (GetProperties().GetCount() > 0)
2122 handler
->WriteProperties(stream
, GetProperties(), indent
);
2123 ::OutputIndentation(stream
, indent
);
2125 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2130 // Output this character as a number in a separate tag, because XML can't cope
2131 // with entities below 32 except for 10 and 13
2133 ::OutputIndentation(stream
, indent
);
2134 ::OutputString(stream
, wxT("<symbol"), handler
->GetConvMem(), handler
->GetConvFile());
2136 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2137 ::OutputString(stream
, wxString::Format(wxT("%d"), c
), handler
->GetConvMem(), handler
->GetConvFile());
2139 if (GetProperties().GetCount() > 0)
2141 handler
->WriteProperties(stream
, GetProperties(), indent
);
2142 ::OutputIndentation(stream
, indent
);
2144 ::OutputString(stream
, wxT("</symbol>"), handler
->GetConvMem(), handler
->GetConvFile());
2152 fragment
= text
.Mid(last
, i
-last
);
2156 ::OutputIndentation(stream
, indent
);
2157 ::OutputString(stream
, wxT("<text"), handler
->GetConvMem(), handler
->GetConvFile());
2159 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2161 if (GetProperties().GetCount() > 0)
2163 handler
->WriteProperties(stream
, GetProperties(), indent
);
2164 ::OutputIndentation(stream
, indent
);
2167 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
2169 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2170 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2171 ::OutputString(stream
, wxT("\""), handler
->GetConvMem(), handler
->GetConvFile());
2174 ::OutputStringEnt(stream
, fragment
, handler
->GetConvMem(), handler
->GetConvFile());
2176 ::OutputString(stream
, wxT("</text>"), handler
->GetConvMem(), handler
->GetConvFile());
2182 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2183 // Export this object to the given parent node, usually creating at least one child node.
2184 bool wxRichTextPlainText::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2188 const wxString
& text
= GetText();
2189 int len
= (int) text
.Length();
2195 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2196 parent
->AddChild(elementNode
);
2198 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2199 handler
->WriteProperties(elementNode
, GetProperties());
2201 else for (i
= 0; i
< len
; i
++)
2204 int c
= (int) text
[i
];
2206 int c
= (int) wxUChar(text
[i
]);
2208 if ((c
< 32 || c
== 34) && c
!= 10 && c
!= 13)
2212 wxString
fragment(text
.Mid(last
, i
-last
));
2213 if (!fragment
.empty())
2215 // TODO: I'm assuming wxXmlDocument will output quotes if necessary
2216 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2217 parent
->AddChild(elementNode
);
2218 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2219 handler
->WriteProperties(elementNode
, GetProperties());
2221 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2222 elementNode
->AddChild(textNode
);
2224 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2225 fragment
= wxT("\"") + fragment
+ wxT("\"");
2227 textNode
->SetContent(fragment
);
2232 // Output this character as a number in a separate tag, because XML can't cope
2233 // with entities below 32 except for 10 and 13
2235 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("symbol"));
2236 parent
->AddChild(elementNode
);
2238 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2239 handler
->WriteProperties(elementNode
, GetProperties());
2241 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2242 elementNode
->AddChild(textNode
);
2243 textNode
->SetContent(wxString::Format(wxT("%d"), c
));
2253 fragment
= text
.Mid(last
, i
-last
);
2257 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("text"));
2258 parent
->AddChild(elementNode
);
2259 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2261 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2262 elementNode
->AddChild(textNode
);
2264 if (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' '))
2265 fragment
= wxT("\"") + fragment
+ wxT("\"");
2267 textNode
->SetContent(fragment
);
2274 // Import this object from XML
2275 bool wxRichTextImage::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2277 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2279 wxBitmapType imageType
= wxBITMAP_TYPE_PNG
;
2280 wxString value
= node
->GetAttribute(wxT("imagetype"), wxEmptyString
);
2283 int type
= wxAtoi(value
);
2285 // note: 0 == wxBITMAP_TYPE_INVALID
2286 if (type
<= 0 || type
>= wxBITMAP_TYPE_MAX
)
2288 wxLogWarning("Invalid bitmap type specified for <image> tag: %d", type
);
2292 imageType
= (wxBitmapType
)type
;
2298 wxXmlNode
* imageChild
= node
->GetChildren();
2301 wxString childName
= imageChild
->GetName();
2302 if (childName
== wxT("data"))
2304 wxXmlNode
* dataChild
= imageChild
->GetChildren();
2307 data
= dataChild
->GetContent();
2308 // wxLogDebug(data);
2309 dataChild
= dataChild
->GetNext();
2313 imageChild
= imageChild
->GetNext();
2318 wxStringInputStream
strStream(data
);
2320 GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
2328 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2329 // Export this object directly to the given stream.
2330 bool wxRichTextImage::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2332 wxString style
= handler
->AddAttributes(GetAttributes(), false);
2334 ::OutputIndentation(stream
, indent
);
2335 ::OutputString(stream
, wxT("<image"), handler
->GetConvMem(), handler
->GetConvFile());
2336 if (!GetImageBlock().Ok())
2339 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2343 ::OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\""), (int) GetImageBlock().GetImageType()) + style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2345 if (GetProperties().GetCount() > 0)
2347 handler
->WriteProperties(stream
, GetProperties(), indent
);
2348 ::OutputIndentation(stream
, indent
);
2351 ::OutputIndentation(stream
, indent
+1);
2352 ::OutputString(stream
, wxT("<data>"), handler
->GetConvMem(), handler
->GetConvFile());
2354 // wxStopWatch stopwatch;
2356 GetImageBlock().WriteHex(stream
);
2358 // wxLogDebug(wxT("Image conversion to hex took %ldms"), stopwatch.Time());
2360 ::OutputString(stream
, wxT("</data>\n"), handler
->GetConvMem(), handler
->GetConvFile());
2361 ::OutputIndentation(stream
, indent
);
2362 ::OutputString(stream
, wxT("</image>"), handler
->GetConvMem(), handler
->GetConvFile());
2367 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2368 // Export this object to the given parent node, usually creating at least one child node.
2369 bool wxRichTextImage::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2371 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("image"));
2372 parent
->AddChild(elementNode
);
2374 if (GetImageBlock().Ok())
2375 elementNode
->AddAttribute(wxT("imagetype"), MakeString((int) GetImageBlock().GetImageType()));
2377 handler
->AddAttributes(elementNode
, GetAttributes(), false);
2378 handler
->WriteProperties(elementNode
, GetProperties());
2380 wxXmlNode
* dataNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, wxT("data"));
2381 elementNode
->AddChild(dataNode
);
2382 wxXmlNode
* textNode
= new wxXmlNode(wxXML_TEXT_NODE
, wxT("text"));
2383 dataNode
->AddChild(textNode
);
2388 wxMemoryOutputStream stream
;
2389 if (GetImageBlock().WriteHex(stream
))
2391 if (stream
.GetSize() > 0)
2393 int size
= stream
.GetSize();
2395 int size2
= stream
.GetOutputStreamBuffer()->GetIntPosition();
2396 wxASSERT(size
== size2
);
2398 unsigned char* data
= new unsigned char[size
];
2399 stream
.CopyTo(data
, size
);
2400 strData
= wxString((const char*) data
, wxConvUTF8
, size
);
2404 strData
= wxEmptyString
;
2410 wxStringOutputStream
strStream(& strData
);
2411 GetImageBlock().WriteHex(strStream
);
2415 textNode
->SetContent(strData
);
2416 textNode
->SetNoConversion(true); // optimize speed
2423 // Import this object from XML
2424 bool wxRichTextParagraphLayoutBox::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2426 wxRichTextObject::ImportFromXML(buffer
, node
, handler
, recurse
);
2430 wxString partial
= node
->GetAttribute(wxT("partialparagraph"), wxEmptyString
);
2431 if (partial
== wxT("true"))
2432 SetPartialParagraph(true);
2434 wxXmlNode
* child
= wxRichTextXMLHandler::FindNode(node
, wxT("stylesheet"));
2435 if (child
&& (handler
->GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
2437 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
2438 wxString sheetName
= child
->GetAttribute(wxT("name"), wxEmptyString
);
2439 wxString sheetDescription
= child
->GetAttribute(wxT("description"), wxEmptyString
);
2440 sheet
->SetName(sheetName
);
2441 sheet
->SetDescription(sheetDescription
);
2443 wxXmlNode
* child2
= child
->GetChildren();
2446 handler
->ImportStyleDefinition(sheet
, child2
);
2448 child2
= child2
->GetNext();
2451 // Notify that styles have changed. If this is vetoed by the app,
2452 // the new sheet will be deleted. If it is not vetoed, the
2453 // old sheet will be deleted and replaced with the new one.
2454 buffer
->SetStyleSheetAndNotify(sheet
);
2460 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2461 // Export this object directly to the given stream.
2462 bool wxRichTextParagraphLayoutBox::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2464 ::OutputIndentation(stream
, indent
);
2465 wxString nodeName
= GetXMLNodeName();
2466 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2468 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2470 if (GetPartialParagraph())
2471 style
<< wxT(" partialparagraph=\"true\"");
2473 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2475 if (GetProperties().GetCount() > 0)
2477 handler
->WriteProperties(stream
, GetProperties(), indent
);
2481 for (i
= 0; i
< GetChildCount(); i
++)
2483 wxRichTextObject
* child
= GetChild(i
);
2484 child
->ExportXML(stream
, indent
+1, handler
);
2487 ::OutputIndentation(stream
, indent
);
2488 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2493 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2494 // Export this object to the given parent node, usually creating at least one child node.
2495 bool wxRichTextParagraphLayoutBox::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2497 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2498 parent
->AddChild(elementNode
);
2499 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2500 handler
->WriteProperties(elementNode
, GetProperties());
2502 if (GetPartialParagraph())
2503 elementNode
->AddAttribute(wxT("partialparagraph"), wxT("true"));
2506 for (i
= 0; i
< GetChildCount(); i
++)
2508 wxRichTextObject
* child
= GetChild(i
);
2509 child
->ExportXML(elementNode
, handler
);
2516 // Import this object from XML
2517 bool wxRichTextTable::ImportFromXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
, wxRichTextXMLHandler
* handler
, bool* recurse
)
2519 wxRichTextBox::ImportFromXML(buffer
, node
, handler
, recurse
);
2523 m_rowCount
= wxAtoi(node
->GetAttribute(wxT("rows"), wxEmptyString
));
2524 m_colCount
= wxAtoi(node
->GetAttribute(wxT("cols"), wxEmptyString
));
2526 wxXmlNode
* child
= node
->GetChildren();
2529 wxRichTextObject
* childObj
= handler
->CreateObjectForXMLName(this, child
->GetName());
2532 AppendChild(childObj
);
2533 handler
->ImportXML(buffer
, childObj
, child
);
2535 child
= child
->GetNext();
2538 m_cells
.Add(wxRichTextObjectPtrArray(), m_rowCount
);
2540 for (i
= 0; i
< m_rowCount
; i
++)
2542 wxRichTextObjectPtrArray
& colArray
= m_cells
[i
];
2543 for (j
= 0; j
< m_colCount
; j
++)
2545 int idx
= i
* m_colCount
+ j
;
2546 if (idx
< (int) GetChildren().GetCount())
2548 wxRichTextCell
* cell
= wxDynamicCast(GetChildren().Item(idx
)->GetData(), wxRichTextCell
);
2558 #if wxRICHTEXT_HAVE_DIRECT_OUTPUT
2559 // Export this object directly to the given stream.
2560 bool wxRichTextTable::ExportXML(wxOutputStream
& stream
, int indent
, wxRichTextXMLHandler
* handler
)
2562 ::OutputIndentation(stream
, indent
);
2563 wxString nodeName
= GetXMLNodeName();
2564 ::OutputString(stream
, wxT("<") + nodeName
, handler
->GetConvMem(), handler
->GetConvFile());
2566 wxString style
= handler
->AddAttributes(GetAttributes(), true);
2568 style
<< wxT(" rows=\"") << m_rowCount
<< wxT("\"");
2569 style
<< wxT(" cols=\"") << m_colCount
<< wxT("\"");
2571 ::OutputString(stream
, style
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2573 if (GetProperties().GetCount() > 0)
2575 handler
->WriteProperties(stream
, GetProperties(), indent
);
2579 for (i
= 0; i
< m_rowCount
; i
++)
2581 for (j
= 0; j
< m_colCount
; j
++)
2583 wxRichTextCell
* cell
= GetCell(i
, j
);
2584 cell
->ExportXML(stream
, indent
+1, handler
);
2588 ::OutputIndentation(stream
, indent
);
2589 ::OutputString(stream
, wxT("</") + nodeName
+ wxT(">"), handler
->GetConvMem(), handler
->GetConvFile());
2595 #if wxRICHTEXT_HAVE_XMLDOCUMENT_OUTPUT
2596 // Export this object to the given parent node, usually creating at least one child node.
2597 bool wxRichTextTable::ExportXML(wxXmlNode
* parent
, wxRichTextXMLHandler
* handler
)
2599 wxXmlNode
* elementNode
= new wxXmlNode(wxXML_ELEMENT_NODE
, GetXMLNodeName());
2600 parent
->AddChild(elementNode
);
2601 handler
->AddAttributes(elementNode
, GetAttributes(), true);
2602 handler
->WriteProperties(elementNode
, GetProperties());
2604 elementNode
->AddAttribute(wxT("rows"), wxString::Format(wxT("%d"), m_rowCount
));
2605 elementNode
->AddAttribute(wxT("cols"), wxString::Format(wxT("%d"), m_colCount
));
2608 for (i
= 0; i
< m_rowCount
; i
++)
2610 for (j
= 0; j
< m_colCount
; j
++)
2612 wxRichTextCell
* cell
= GetCell(i
, j
);
2613 cell
->ExportXML(elementNode
, handler
);
2623 // wxUSE_RICHTEXT && wxUSE_XML