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"
28 #include "wx/filename.h"
29 #include "wx/clipbrd.h"
30 #include "wx/wfstream.h"
31 #include "wx/sstream.h"
32 #include "wx/txtstrm.h"
33 #include "wx/tokenzr.h"
34 #include "wx/xml/xml.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler
, wxRichTextFileHandler
)
39 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
44 buffer
->ResetAndClearCommands();
47 wxXmlDocument
* xmlDoc
= new wxXmlDocument
;
50 // This is the encoding to convert to (memory encoding rather than file encoding)
51 wxString
encoding(wxT("UTF-8"));
53 #if !wxUSE_UNICODE && wxUSE_INTL
54 encoding
= wxLocale::GetSystemEncodingName();
57 if (!xmlDoc
->Load(stream
, encoding
))
59 buffer
->ResetAndClearCommands();
64 if (xmlDoc
->GetRoot() && xmlDoc
->GetRoot()->GetType() == wxXML_ELEMENT_NODE
&& xmlDoc
->GetRoot()->GetName() == wxT("richtext"))
66 wxXmlNode
* child
= xmlDoc
->GetRoot()->GetChildren();
69 if (child
->GetType() == wxXML_ELEMENT_NODE
)
71 wxString name
= child
->GetName();
72 if (name
== wxT("richtext-version"))
76 ImportXML(buffer
, child
);
79 child
= child
->GetNext();
90 buffer
->UpdateRanges();
95 /// Recursively import an object
96 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
)
98 wxString name
= node
->GetName();
100 bool doneChildren
= false;
102 if (name
== wxT("paragraphlayout"))
104 wxString partial
= node
->GetAttribute(wxT("partialparagraph"), wxEmptyString
);
105 if (partial
== wxT("true"))
106 buffer
->SetPartialParagraph(true);
108 else if (name
== wxT("paragraph"))
110 wxRichTextParagraph
* para
= new wxRichTextParagraph(buffer
);
111 buffer
->AppendChild(para
);
113 GetStyle(para
->GetAttributes(), node
, true);
115 wxXmlNode
* child
= node
->GetChildren();
118 wxString childName
= child
->GetName();
119 if (childName
== wxT("text"))
122 wxXmlNode
* textChild
= child
->GetChildren();
125 if (textChild
->GetType() == wxXML_TEXT_NODE
||
126 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
128 wxString text2
= textChild
->GetContent();
130 // Strip whitespace from end
131 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
132 text2
= text2
.Mid(0, text2
.length()-1);
134 if (!text2
.empty() && text2
[0] == wxT('"'))
135 text2
= text2
.Mid(1);
136 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
137 text2
= text2
.Mid(0, text2
.length() - 1);
141 textChild
= textChild
->GetNext();
144 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, para
);
145 GetStyle(textObject
->GetAttributes(), child
, false);
147 para
->AppendChild(textObject
);
149 else if (childName
== wxT("symbol"))
151 // This is a symbol that XML can't read in the normal way
153 wxXmlNode
* textChild
= child
->GetChildren();
156 if (textChild
->GetType() == wxXML_TEXT_NODE
||
157 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
159 wxString text2
= textChild
->GetContent();
162 textChild
= textChild
->GetNext();
166 actualText
<< (wxChar
) wxAtoi(text
);
168 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(actualText
, para
);
169 GetStyle(textObject
->GetAttributes(), child
, false);
171 para
->AppendChild(textObject
);
173 else if (childName
== wxT("image"))
175 int imageType
= wxBITMAP_TYPE_PNG
;
176 wxString value
= node
->GetAttribute(wxT("imagetype"), wxEmptyString
);
178 imageType
= wxAtoi(value
);
182 wxXmlNode
* imageChild
= child
->GetChildren();
185 wxString childName
= imageChild
->GetName();
186 if (childName
== wxT("data"))
188 wxXmlNode
* dataChild
= imageChild
->GetChildren();
191 data
= dataChild
->GetContent();
193 dataChild
= dataChild
->GetNext();
197 imageChild
= imageChild
->GetNext();
202 wxRichTextImage
* imageObj
= new wxRichTextImage(para
);
203 GetStyle(imageObj
->GetAttributes(), child
, false);
204 para
->AppendChild(imageObj
);
206 wxStringInputStream
strStream(data
);
208 imageObj
->GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
211 child
= child
->GetNext();
216 else if (name
== wxT("stylesheet"))
218 if (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
)
220 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
221 wxString sheetName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
222 wxString sheetDescription
= node
->GetAttribute(wxT("description"), wxEmptyString
);
223 sheet
->SetName(sheetName
);
224 sheet
->SetDescription(sheetDescription
);
226 wxXmlNode
* child
= node
->GetChildren();
229 ImportStyleDefinition(sheet
, child
);
231 child
= child
->GetNext();
234 // Notify that styles have changed. If this is vetoed by the app,
235 // the new sheet will be deleted. If it is not vetoed, the
236 // old sheet will be deleted and replaced with the new one.
237 buffer
->SetStyleSheetAndNotify(sheet
);
244 wxXmlNode
* child
= node
->GetChildren();
247 ImportXML(buffer
, child
);
248 child
= child
->GetNext();
255 bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet
* sheet
, wxXmlNode
* node
)
257 wxString styleType
= node
->GetName();
258 wxString styleName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
259 wxString baseStyleName
= node
->GetAttribute(wxT("basestyle"), wxEmptyString
);
261 if (styleName
.IsEmpty())
264 if (styleType
== wxT("characterstyle"))
266 wxRichTextCharacterStyleDefinition
* def
= new wxRichTextCharacterStyleDefinition(styleName
);
267 def
->SetBaseStyle(baseStyleName
);
269 wxXmlNode
* child
= node
->GetChildren();
272 if (child
->GetName() == wxT("style"))
275 GetStyle(attr
, child
, false);
278 child
= child
->GetNext();
281 sheet
->AddCharacterStyle(def
);
283 else if (styleType
== wxT("paragraphstyle"))
285 wxRichTextParagraphStyleDefinition
* def
= new wxRichTextParagraphStyleDefinition(styleName
);
287 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
288 def
->SetNextStyle(nextStyleName
);
289 def
->SetBaseStyle(baseStyleName
);
291 wxXmlNode
* child
= node
->GetChildren();
294 if (child
->GetName() == wxT("style"))
297 GetStyle(attr
, child
, false);
300 child
= child
->GetNext();
303 sheet
->AddParagraphStyle(def
);
305 else if (styleType
== wxT("liststyle"))
307 wxRichTextListStyleDefinition
* def
= new wxRichTextListStyleDefinition(styleName
);
309 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
310 def
->SetNextStyle(nextStyleName
);
311 def
->SetBaseStyle(baseStyleName
);
313 wxXmlNode
* child
= node
->GetChildren();
316 if (child
->GetName() == wxT("style"))
319 GetStyle(attr
, child
, false);
321 wxString styleLevel
= child
->GetAttribute(wxT("level"), wxEmptyString
);
322 if (styleLevel
.IsEmpty())
328 int level
= wxAtoi(styleLevel
);
329 if (level
> 0 && level
<= 10)
331 def
->SetLevelAttributes(level
-1, attr
);
335 child
= child
->GetNext();
338 sheet
->AddListStyle(def
);
344 //-----------------------------------------------------------------------------
345 // xml support routines
346 //-----------------------------------------------------------------------------
348 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
350 return (GetParamNode(node
, param
) != NULL
);
353 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
355 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
357 wxXmlNode
*n
= node
->GetChildren();
361 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
369 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
372 if (n
== NULL
) return wxEmptyString
;
373 n
= n
->GetChildren();
377 if (n
->GetType() == wxXML_TEXT_NODE
||
378 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
379 return n
->GetContent();
382 return wxEmptyString
;
386 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
389 return GetNodeContent(node
);
391 return GetNodeContent(GetParamNode(node
, param
));
394 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
396 wxXmlNode
*parNode
= GetParamNode(node
, param
);
399 wxString
str1(GetNodeContent(parNode
));
403 // For use with earlier versions of wxWidgets
404 #ifndef WXUNUSED_IN_UNICODE
406 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
408 #define WXUNUSED_IN_UNICODE(x) x
412 // write string to output:
413 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
414 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
) = NULL
, wxMBConv
*convFile
= NULL
)
416 if (str
.empty()) return;
420 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
421 stream
.Write((const char*)buf
, strlen((const char*)buf
));
425 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
426 stream
.Write((const char*)buf
, strlen((const char*)buf
));
429 if ( convFile
== NULL
)
430 stream
.Write(str
.mb_str(), str
.Len());
433 wxString
str2(str
.wc_str(*convMem
), *convFile
);
434 stream
.Write(str2
.mb_str(), str2
.Len());
439 // Same as above, but create entities first.
440 // Translates '<' to "<", '>' to ">" and '&' to "&"
441 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
442 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
450 for (i
= 0; i
< len
; i
++)
454 // Original code excluded "&" but we _do_ want to convert
455 // the ampersand beginning & because otherwise when read in,
456 // the original "&" becomes "&".
458 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
459 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
461 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
465 OutputString(stream
, wxT("<"), NULL
, NULL
);
468 OutputString(stream
, wxT(">"), NULL
, NULL
);
471 OutputString(stream
, wxT("&"), NULL
, NULL
);
474 OutputString(stream
, wxT("""), NULL
, NULL
);
480 else if (wxUChar(c
) > 127)
482 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
484 wxString
s(wxT("&#"));
487 OutputString(stream
, s
, NULL
, NULL
);
491 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
494 static wxString
AttributeToXML(const wxString
& str
)
502 for (i
= 0; i
< len
; i
++)
506 // Original code excluded "&" but we _do_ want to convert
507 // the ampersand beginning & because otherwise when read in,
508 // the original "&" becomes "&".
510 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
511 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
513 str1
+= str
.Mid(last
, i
- last
);
523 str1
+= wxT("&");
526 str1
+= wxT(""");
532 else if (wxUChar(c
) > 127)
534 str1
+= str
.Mid(last
, i
- last
);
536 wxString
s(wxT("&#"));
543 str1
+= str
.Mid(last
, i
- last
);
547 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
549 wxString str
= wxT("\n");
550 for (int i
= 0; i
< indent
; i
++)
551 str
<< wxT(' ') << wxT(' ');
552 OutputString(stream
, str
, NULL
, NULL
);
555 // Convert a colour to a 6-digit hex string
556 static wxString
ColourToHexString(const wxColour
& col
)
560 hex
+= wxDecToHex(col
.Red());
561 hex
+= wxDecToHex(col
.Green());
562 hex
+= wxDecToHex(col
.Blue());
567 // Convert 6-digit hex string to a colour
568 static wxColour
HexStringToColour(const wxString
& hex
)
570 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
571 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
572 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
574 return wxColour(r
, g
, b
);
577 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
582 wxString
version(wxT("1.0") ) ;
584 bool deleteConvFile
= false;
585 wxString fileEncoding
;
586 wxMBConv
* convFile
= NULL
;
589 fileEncoding
= wxT("UTF-8");
590 convFile
= & wxConvUTF8
;
592 fileEncoding
= wxT("ISO-8859-1");
593 convFile
= & wxConvISO8859_1
;
596 // If SetEncoding has been called, change the output encoding.
597 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
599 if (m_encoding
== wxT("<System>"))
602 fileEncoding
= wxLocale::GetSystemEncodingName();
603 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
608 fileEncoding
= m_encoding
;
611 // GetSystemEncodingName may not have returned a name
612 if (fileEncoding
.empty())
614 fileEncoding
= wxT("UTF-8");
616 fileEncoding
= wxT("ISO-8859-1");
618 convFile
= new wxCSConv(fileEncoding
);
619 deleteConvFile
= true;
623 wxMBConv
* convMem
= wxConvCurrent
;
625 wxMBConv
* convMem
= NULL
;
629 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
630 version
, fileEncoding
);
631 OutputString(stream
, s
, NULL
, NULL
);
632 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
636 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
638 OutputIndentation(stream
, level
);
639 wxString nameAndDescr
;
640 if (!buffer
->GetStyleSheet()->GetName().IsEmpty())
641 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
642 if (!buffer
->GetStyleSheet()->GetDescription().IsEmpty())
643 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
644 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"), convMem
, convFile
);
648 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
650 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
651 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
654 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
656 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
657 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
660 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
662 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
663 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
666 OutputIndentation(stream
, level
);
667 OutputString(stream
, wxT("</stylesheet>"), convMem
, convFile
);
671 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
673 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
674 OutputString(stream
, wxT("\n"), NULL
, NULL
);
682 /// Recursively export an object
683 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
686 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
687 objectName
= wxT("paragraphlayout");
688 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
689 objectName
= wxT("paragraph");
690 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
691 objectName
= wxT("text");
692 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
693 objectName
= wxT("image");
695 objectName
= wxT("object");
697 bool terminateTag
= true;
699 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
701 wxRichTextPlainText
& textObj
= (wxRichTextPlainText
&) obj
;
703 wxString style
= CreateStyle(obj
.GetAttributes(), false);
707 const wxString
& text
= textObj
.GetText();
708 int len
= (int) text
.Length();
713 OutputIndentation(stream
, indent
);
714 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
715 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
716 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
718 else for (i
= 0; i
< len
; i
++)
720 int c
= (int) text
[i
];
721 if (c
< 32 && c
!= 9 && c
!= 10 && c
!= 13)
725 OutputIndentation(stream
, indent
);
726 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
728 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
730 wxString
fragment(text
.Mid(last
, i
-last
));
731 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
733 OutputString(stream
, wxT("\""), convMem
, convFile
);
734 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
735 OutputString(stream
, wxT("\""), convMem
, convFile
);
738 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
740 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
744 // Output this character as a number in a separate tag, because XML can't cope
745 // with entities below 32 except for 9, 10 and 13
747 OutputIndentation(stream
, indent
);
748 OutputString(stream
, wxT("<symbol"), convMem
, convFile
);
750 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
751 OutputString(stream
, wxString::Format(wxT("%d"), c
), convMem
, convFile
);
753 OutputString(stream
, wxT("</symbol>"), convMem
, convFile
);
761 fragment
= text
.Mid(last
, i
-last
);
765 OutputIndentation(stream
, indent
);
766 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
768 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
770 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
772 OutputString(stream
, wxT("\""), convMem
, convFile
);
773 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
774 OutputString(stream
, wxT("\""), convMem
, convFile
);
777 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
780 terminateTag
= false;
782 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
784 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
786 wxString style
= CreateStyle(obj
.GetAttributes(), false);
788 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
789 imageObj
.MakeBlock();
791 OutputIndentation(stream
, indent
);
792 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
793 if (!imageObj
.GetImageBlock().Ok())
796 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
800 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\"") + style
+ wxT(">"), (int) imageObj
.GetImageBlock().GetImageType()));
803 OutputIndentation(stream
, indent
+1);
804 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
806 imageObj
.GetImageBlock().WriteHex(stream
);
808 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
810 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
812 OutputIndentation(stream
, indent
);
813 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
816 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
819 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
821 if (objectName
== wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox
&) obj
).GetPartialParagraph())
822 style
<< wxT(" partialparagraph=\"true\"");
824 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
826 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
828 for (i
= 0; i
< composite
.GetChildCount(); i
++)
830 wxRichTextObject
* child
= composite
.GetChild(i
);
831 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
835 if (objectName
!= wxT("text"))
836 OutputIndentation(stream
, indent
);
839 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
844 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextStyleDefinition
* def
, int level
)
846 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
847 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
848 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
850 wxString baseStyle
= def
->GetBaseStyle();
851 wxString baseStyleProp
;
852 if (!baseStyle
.IsEmpty())
853 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
855 wxString descr
= def
->GetDescription();
857 if (!descr
.IsEmpty())
858 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
862 OutputIndentation(stream
, level
);
863 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
867 wxString style
= CreateStyle(def
->GetStyle(), false);
869 OutputIndentation(stream
, level
);
870 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
872 OutputIndentation(stream
, level
);
873 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
877 OutputIndentation(stream
, level
);
878 OutputString(stream
, wxT("</characterstyle>"), convMem
, convFile
);
882 OutputIndentation(stream
, level
);
884 if (!listDef
->GetNextStyle().IsEmpty())
885 baseStyleProp
<< wxT(" basestyle=\"") << listDef
->GetNextStyle() << wxT("\"");
887 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
891 wxString style
= CreateStyle(def
->GetStyle(), false);
893 OutputIndentation(stream
, level
);
894 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
896 OutputIndentation(stream
, level
);
897 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
900 for (i
= 0; i
< 10; i
++)
902 wxTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
905 wxString style
= CreateStyle(def
->GetStyle(), false);
906 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
908 OutputIndentation(stream
, level
);
909 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"), convMem
, convFile
);
911 OutputIndentation(stream
, level
);
912 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
918 OutputIndentation(stream
, level
);
919 OutputString(stream
, wxT("</liststyle>"), convMem
, convFile
);
923 OutputIndentation(stream
, level
);
925 if (!paraDef
->GetNextStyle().IsEmpty())
926 baseStyleProp
<< wxT(" basestyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
928 OutputString(stream
, wxT("<paragraphstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
932 wxString style
= CreateStyle(def
->GetStyle(), false);
934 OutputIndentation(stream
, level
);
935 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
937 OutputIndentation(stream
, level
);
938 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
942 OutputIndentation(stream
, level
);
943 OutputString(stream
, wxT("</paragraphstyle>"), convMem
, convFile
);
949 /// Create style parameters
950 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttr
& attr
, bool isPara
)
953 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
955 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
957 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
959 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
962 if (attr
.HasFontSize())
963 str
<< wxT(" fontsize=\"") << attr
.GetFontSize() << wxT("\"");
965 //if (attr.HasFontFamily())
966 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
968 if (attr
.HasFontItalic())
969 str
<< wxT(" fontstyle=\"") << attr
.GetFontStyle() << wxT("\"");
971 if (attr
.HasFontWeight())
972 str
<< wxT(" fontweight=\"") << attr
.GetFontWeight() << wxT("\"");
974 if (attr
.HasFontUnderlined())
975 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFontUnderlined() << wxT("\"");
977 if (attr
.HasFontFaceName())
978 str
<< wxT(" fontface=\"") << attr
.GetFontFaceName() << wxT("\"");
980 if (attr
.HasTextEffects())
982 str
<< wxT(" texteffects=\"");
983 str
<< attr
.GetTextEffects();
986 str
<< wxT(" texteffectflags=\"");
987 str
<< attr
.GetTextEffectFlags();
991 if (!attr
.GetCharacterStyleName().empty())
992 str
<< wxT(" characterstyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
995 str
<< wxT(" url=\"") << AttributeToXML(attr
.GetURL()) << wxT("\"");
999 if (attr
.HasAlignment())
1000 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
1002 if (attr
.HasLeftIndent())
1004 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
1005 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
1008 if (attr
.HasRightIndent())
1009 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
1011 if (attr
.HasParagraphSpacingAfter())
1012 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
1014 if (attr
.HasParagraphSpacingBefore())
1015 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
1017 if (attr
.HasLineSpacing())
1018 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
1020 if (attr
.HasBulletStyle())
1021 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
1023 if (attr
.HasBulletNumber())
1024 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
1026 if (attr
.HasBulletText())
1028 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1029 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1030 if (!attr
.GetBulletText().IsEmpty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1031 str
<< wxT(" bulletsymbol=\"") << (int) (attr
.GetBulletText()[0]) << wxT("\"");
1033 str
<< wxT(" bullettext=\"") << attr
.GetBulletText() << wxT("\"");
1035 str
<< wxT(" bulletfont=\"") << attr
.GetBulletFont() << wxT("\"");
1038 if (attr
.HasBulletName())
1039 str
<< wxT(" bulletname=\"") << attr
.GetBulletName() << wxT("\"");
1041 if (!attr
.GetParagraphStyleName().empty())
1042 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
1044 if (!attr
.GetListStyleName().empty())
1045 str
<< wxT(" liststyle=\"") << wxString(attr
.GetListStyleName()) << wxT("\"");
1049 str
<< wxT(" tabs=\"");
1051 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1055 str
<< attr
.GetTabs()[i
];
1060 if (attr
.HasPageBreak())
1062 str
<< wxT(" pagebreak=\"1\"");
1065 if (attr
.HasOutlineLevel())
1066 str
<< wxT(" outlinelevel=\"") << (int) attr
.GetOutlineLevel() << wxT("\"");
1073 /// Get style parameters
1074 bool wxRichTextXMLHandler::GetStyle(wxTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1076 wxString fontFacename
;
1078 // int fontFamily = wxDEFAULT;
1079 int fontWeight
= wxNORMAL
;
1080 int fontStyle
= wxNORMAL
;
1081 bool fontUnderlined
= false;
1083 // int fontFlags = 0;
1085 fontFacename
= node
->GetAttribute(wxT("fontface"), wxEmptyString
);
1086 if (!fontFacename
.IsEmpty())
1087 attr
.SetFontFaceName(fontFacename
);
1090 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1091 //if (!value.empty())
1092 // fontFamily = wxAtoi(value);
1094 value
= node
->GetAttribute(wxT("fontstyle"), wxEmptyString
);
1097 fontStyle
= wxAtoi(value
);
1098 attr
.SetFontStyle(fontStyle
);
1101 value
= node
->GetAttribute(wxT("fontsize"), wxEmptyString
);
1104 fontSize
= wxAtoi(value
);
1105 attr
.SetFontSize(fontSize
);
1108 value
= node
->GetAttribute(wxT("fontweight"), wxEmptyString
);
1111 fontWeight
= wxAtoi(value
);
1112 attr
.SetFontWeight(fontWeight
);
1115 value
= node
->GetAttribute(wxT("fontunderlined"), wxEmptyString
);
1118 fontUnderlined
= wxAtoi(value
) != 0;
1119 attr
.SetFontUnderlined(fontUnderlined
);
1122 value
= node
->GetAttribute(wxT("textcolor"), wxEmptyString
);
1125 if (value
[0] == wxT('#'))
1126 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1128 attr
.SetTextColour(value
);
1131 value
= node
->GetAttribute(wxT("bgcolor"), wxEmptyString
);
1134 if (value
[0] == wxT('#'))
1135 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1137 attr
.SetBackgroundColour(value
);
1140 value
= node
->GetAttribute(wxT("characterstyle"), wxEmptyString
);
1142 attr
.SetCharacterStyleName(value
);
1144 value
= node
->GetAttribute(wxT("texteffects"), wxEmptyString
);
1145 if (!value
.IsEmpty())
1147 attr
.SetTextEffects(wxAtoi(value
));
1150 value
= node
->GetAttribute(wxT("texteffectflags"), wxEmptyString
);
1151 if (!value
.IsEmpty())
1153 attr
.SetTextEffectFlags(wxAtoi(value
));
1156 value
= node
->GetAttribute(wxT("url"), wxEmptyString
);
1160 // Set paragraph attributes
1163 value
= node
->GetAttribute(wxT("alignment"), wxEmptyString
);
1165 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1167 int leftSubIndent
= 0;
1169 bool hasLeftIndent
= false;
1171 value
= node
->GetAttribute(wxT("leftindent"), wxEmptyString
);
1174 leftIndent
= wxAtoi(value
);
1175 hasLeftIndent
= true;
1178 value
= node
->GetAttribute(wxT("leftsubindent"), wxEmptyString
);
1181 leftSubIndent
= wxAtoi(value
);
1182 hasLeftIndent
= true;
1186 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
1188 value
= node
->GetAttribute(wxT("rightindent"), wxEmptyString
);
1190 attr
.SetRightIndent(wxAtoi(value
));
1192 value
= node
->GetAttribute(wxT("parspacingbefore"), wxEmptyString
);
1194 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1196 value
= node
->GetAttribute(wxT("parspacingafter"), wxEmptyString
);
1198 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1200 value
= node
->GetAttribute(wxT("linespacing"), wxEmptyString
);
1202 attr
.SetLineSpacing(wxAtoi(value
));
1204 value
= node
->GetAttribute(wxT("bulletstyle"), wxEmptyString
);
1206 attr
.SetBulletStyle(wxAtoi(value
));
1208 value
= node
->GetAttribute(wxT("bulletnumber"), wxEmptyString
);
1210 attr
.SetBulletNumber(wxAtoi(value
));
1212 value
= node
->GetAttribute(wxT("bulletsymbol"), wxEmptyString
);
1215 wxChar ch
= wxAtoi(value
);
1218 attr
.SetBulletText(s
);
1221 value
= node
->GetAttribute(wxT("bullettext"), wxEmptyString
);
1223 attr
.SetBulletText(value
);
1225 value
= node
->GetAttribute(wxT("bulletfont"), wxEmptyString
);
1227 attr
.SetBulletFont(value
);
1229 value
= node
->GetAttribute(wxT("bulletname"), wxEmptyString
);
1231 attr
.SetBulletName(value
);
1233 value
= node
->GetAttribute(wxT("parstyle"), wxEmptyString
);
1235 attr
.SetParagraphStyleName(value
);
1237 value
= node
->GetAttribute(wxT("liststyle"), wxEmptyString
);
1239 attr
.SetListStyleName(value
);
1241 value
= node
->GetAttribute(wxT("tabs"), wxEmptyString
);
1245 wxStringTokenizer
tkz(value
, wxT(","));
1246 while (tkz
.HasMoreTokens())
1248 wxString token
= tkz
.GetNextToken();
1249 tabs
.Add(wxAtoi(token
));
1254 value
= node
->GetAttribute(wxT("pagebreak"), wxEmptyString
);
1255 if (!value
.IsEmpty())
1257 attr
.SetPageBreak(wxAtoi(value
) != 0);
1260 value
= node
->GetAttribute(wxT("outlinelevel"), wxEmptyString
);
1261 if (!value
.IsEmpty())
1263 attr
.SetOutlineLevel(wxAtoi(value
));
1274 // wxUSE_RICHTEXT && wxUSE_XML