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/tokenzr.h"
35 #include "wx/xml/xml.h"
37 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler
, wxRichTextFileHandler
)
40 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer
*buffer
, wxInputStream
& stream
)
45 buffer
->ResetAndClearCommands();
48 wxXmlDocument
* xmlDoc
= new wxXmlDocument
;
51 // This is the encoding to convert to (memory encoding rather than file encoding)
52 wxString
encoding(wxT("UTF-8"));
54 #if !wxUSE_UNICODE && wxUSE_INTL
55 encoding
= wxLocale::GetSystemEncodingName();
58 if (!xmlDoc
->Load(stream
, encoding
))
60 buffer
->ResetAndClearCommands();
65 if (xmlDoc
->GetRoot() && xmlDoc
->GetRoot()->GetType() == wxXML_ELEMENT_NODE
&& xmlDoc
->GetRoot()->GetName() == wxT("richtext"))
67 wxXmlNode
* child
= xmlDoc
->GetRoot()->GetChildren();
70 if (child
->GetType() == wxXML_ELEMENT_NODE
)
72 wxString name
= child
->GetName();
73 if (name
== wxT("richtext-version"))
77 ImportXML(buffer
, child
);
80 child
= child
->GetNext();
91 buffer
->UpdateRanges();
96 /// Recursively import an object
97 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer
* buffer
, wxXmlNode
* node
)
99 wxString name
= node
->GetName();
101 bool doneChildren
= false;
103 if (name
== wxT("paragraphlayout"))
105 wxString partial
= node
->GetAttribute(wxT("partialparagraph"), wxEmptyString
);
106 if (partial
== wxT("true"))
107 buffer
->SetPartialParagraph(true);
109 else if (name
== wxT("paragraph"))
111 wxRichTextParagraph
* para
= new wxRichTextParagraph(buffer
);
112 buffer
->AppendChild(para
);
114 GetStyle(para
->GetAttributes(), node
, true);
116 wxXmlNode
* child
= node
->GetChildren();
119 wxString childName
= child
->GetName();
120 if (childName
== wxT("text"))
123 wxXmlNode
* textChild
= child
->GetChildren();
126 if (textChild
->GetType() == wxXML_TEXT_NODE
||
127 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
129 wxString text2
= textChild
->GetContent();
131 // Strip whitespace from end
132 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('\n'))
133 text2
= text2
.Mid(0, text2
.length()-1);
135 if (!text2
.empty() && text2
[0] == wxT('"'))
136 text2
= text2
.Mid(1);
137 if (!text2
.empty() && text2
[text2
.length()-1] == wxT('"'))
138 text2
= text2
.Mid(0, text2
.length() - 1);
142 textChild
= textChild
->GetNext();
145 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(text
, para
);
146 GetStyle(textObject
->GetAttributes(), child
, false);
148 para
->AppendChild(textObject
);
150 else if (childName
== wxT("symbol"))
152 // This is a symbol that XML can't read in the normal way
154 wxXmlNode
* textChild
= child
->GetChildren();
157 if (textChild
->GetType() == wxXML_TEXT_NODE
||
158 textChild
->GetType() == wxXML_CDATA_SECTION_NODE
)
160 wxString text2
= textChild
->GetContent();
163 textChild
= textChild
->GetNext();
167 actualText
<< (wxChar
) wxAtoi(text
);
169 wxRichTextPlainText
* textObject
= new wxRichTextPlainText(actualText
, para
);
170 GetStyle(textObject
->GetAttributes(), child
, false);
172 para
->AppendChild(textObject
);
174 else if (childName
== wxT("image"))
176 wxBitmapType imageType
= wxBITMAP_TYPE_PNG
;
177 wxString value
= child
->GetAttribute(wxT("imagetype"), wxEmptyString
);
180 int type
= wxAtoi(value
);
182 // note: 0 == wxBITMAP_TYPE_INVALID
183 if (type
<= 0 || type
>= wxBITMAP_TYPE_MAX
)
184 wxLogWarning("Invalid bitmap type specified for <image> tag: %d", type
);
186 imageType
= (wxBitmapType
)type
;
191 wxXmlNode
* imageChild
= child
->GetChildren();
194 wxString childName
= imageChild
->GetName();
195 if (childName
== wxT("data"))
197 wxXmlNode
* dataChild
= imageChild
->GetChildren();
200 data
= dataChild
->GetContent();
202 dataChild
= dataChild
->GetNext();
206 imageChild
= imageChild
->GetNext();
211 wxRichTextImage
* imageObj
= new wxRichTextImage(para
);
212 GetStyle(imageObj
->GetAttributes(), child
, false);
213 para
->AppendChild(imageObj
);
215 wxStringInputStream
strStream(data
);
217 imageObj
->GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
220 child
= child
->GetNext();
225 else if (name
== wxT("stylesheet"))
227 if (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
)
229 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
230 wxString sheetName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
231 wxString sheetDescription
= node
->GetAttribute(wxT("description"), wxEmptyString
);
232 sheet
->SetName(sheetName
);
233 sheet
->SetDescription(sheetDescription
);
235 wxXmlNode
* child
= node
->GetChildren();
238 ImportStyleDefinition(sheet
, child
);
240 child
= child
->GetNext();
243 // Notify that styles have changed. If this is vetoed by the app,
244 // the new sheet will be deleted. If it is not vetoed, the
245 // old sheet will be deleted and replaced with the new one.
246 buffer
->SetStyleSheetAndNotify(sheet
);
253 wxXmlNode
* child
= node
->GetChildren();
256 ImportXML(buffer
, child
);
257 child
= child
->GetNext();
264 bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet
* sheet
, wxXmlNode
* node
)
266 wxString styleType
= node
->GetName();
267 wxString styleName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
268 wxString baseStyleName
= node
->GetAttribute(wxT("basestyle"), wxEmptyString
);
270 if (styleName
.IsEmpty())
273 if (styleType
== wxT("characterstyle"))
275 wxRichTextCharacterStyleDefinition
* def
= new wxRichTextCharacterStyleDefinition(styleName
);
276 def
->SetBaseStyle(baseStyleName
);
278 wxXmlNode
* child
= node
->GetChildren();
281 if (child
->GetName() == wxT("style"))
284 GetStyle(attr
, child
, false);
287 child
= child
->GetNext();
290 sheet
->AddCharacterStyle(def
);
292 else if (styleType
== wxT("paragraphstyle"))
294 wxRichTextParagraphStyleDefinition
* def
= new wxRichTextParagraphStyleDefinition(styleName
);
296 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
297 def
->SetNextStyle(nextStyleName
);
298 def
->SetBaseStyle(baseStyleName
);
300 wxXmlNode
* child
= node
->GetChildren();
303 if (child
->GetName() == wxT("style"))
306 GetStyle(attr
, child
, false);
309 child
= child
->GetNext();
312 sheet
->AddParagraphStyle(def
);
314 else if (styleType
== wxT("liststyle"))
316 wxRichTextListStyleDefinition
* def
= new wxRichTextListStyleDefinition(styleName
);
318 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
319 def
->SetNextStyle(nextStyleName
);
320 def
->SetBaseStyle(baseStyleName
);
322 wxXmlNode
* child
= node
->GetChildren();
325 if (child
->GetName() == wxT("style"))
328 GetStyle(attr
, child
, false);
330 wxString styleLevel
= child
->GetAttribute(wxT("level"), wxEmptyString
);
331 if (styleLevel
.IsEmpty())
337 int level
= wxAtoi(styleLevel
);
338 if (level
> 0 && level
<= 10)
340 def
->SetLevelAttributes(level
-1, attr
);
344 child
= child
->GetNext();
347 sheet
->AddListStyle(def
);
353 //-----------------------------------------------------------------------------
354 // xml support routines
355 //-----------------------------------------------------------------------------
357 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
359 return (GetParamNode(node
, param
) != NULL
);
362 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
364 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
366 wxXmlNode
*n
= node
->GetChildren();
370 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
378 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
381 if (n
== NULL
) return wxEmptyString
;
382 n
= n
->GetChildren();
386 if (n
->GetType() == wxXML_TEXT_NODE
||
387 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
388 return n
->GetContent();
391 return wxEmptyString
;
395 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
398 return GetNodeContent(node
);
400 return GetNodeContent(GetParamNode(node
, param
));
403 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
405 wxXmlNode
*parNode
= GetParamNode(node
, param
);
408 wxString
str1(GetNodeContent(parNode
));
412 // For use with earlier versions of wxWidgets
413 #ifndef WXUNUSED_IN_UNICODE
415 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
417 #define WXUNUSED_IN_UNICODE(x) x
421 // write string to output:
422 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
423 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
) = NULL
, wxMBConv
*convFile
= NULL
)
425 if (str
.empty()) return;
429 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
430 stream
.Write((const char*)buf
, strlen((const char*)buf
));
434 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
435 stream
.Write((const char*)buf
, strlen((const char*)buf
));
438 if ( convFile
== NULL
)
439 stream
.Write(str
.mb_str(), str
.Len());
442 wxString
str2(str
.wc_str(*convMem
), *convFile
);
443 stream
.Write(str2
.mb_str(), str2
.Len());
448 // Same as above, but create entities first.
449 // Translates '<' to "<", '>' to ">" and '&' to "&"
450 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
451 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
459 for (i
= 0; i
< len
; i
++)
463 // Original code excluded "&" but we _do_ want to convert
464 // the ampersand beginning & because otherwise when read in,
465 // the original "&" becomes "&".
467 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
468 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
470 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
474 OutputString(stream
, wxT("<"), NULL
, NULL
);
477 OutputString(stream
, wxT(">"), NULL
, NULL
);
480 OutputString(stream
, wxT("&"), NULL
, NULL
);
483 OutputString(stream
, wxT("""), NULL
, NULL
);
489 else if (wxUChar(c
) > 127)
491 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
493 wxString
s(wxT("&#"));
496 OutputString(stream
, s
, NULL
, NULL
);
500 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
503 static wxString
AttributeToXML(const wxString
& str
)
511 for (i
= 0; i
< len
; i
++)
515 // Original code excluded "&" but we _do_ want to convert
516 // the ampersand beginning & because otherwise when read in,
517 // the original "&" becomes "&".
519 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
520 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
522 str1
+= str
.Mid(last
, i
- last
);
532 str1
+= wxT("&");
535 str1
+= wxT(""");
541 else if (wxUChar(c
) > 127)
543 str1
+= str
.Mid(last
, i
- last
);
545 wxString
s(wxT("&#"));
552 str1
+= str
.Mid(last
, i
- last
);
556 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
558 wxString str
= wxT("\n");
559 for (int i
= 0; i
< indent
; i
++)
560 str
<< wxT(' ') << wxT(' ');
561 OutputString(stream
, str
, NULL
, NULL
);
564 // Convert a colour to a 6-digit hex string
565 static wxString
ColourToHexString(const wxColour
& col
)
569 hex
+= wxDecToHex(col
.Red());
570 hex
+= wxDecToHex(col
.Green());
571 hex
+= wxDecToHex(col
.Blue());
576 // Convert 6-digit hex string to a colour
577 static wxColour
HexStringToColour(const wxString
& hex
)
579 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
580 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
581 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
583 return wxColour(r
, g
, b
);
586 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
591 wxString
version(wxT("1.0") ) ;
593 bool deleteConvFile
= false;
594 wxString fileEncoding
;
595 wxMBConv
* convFile
= NULL
;
598 fileEncoding
= wxT("UTF-8");
599 convFile
= & wxConvUTF8
;
601 fileEncoding
= wxT("ISO-8859-1");
602 convFile
= & wxConvISO8859_1
;
605 // If SetEncoding has been called, change the output encoding.
606 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
608 if (m_encoding
== wxT("<System>"))
611 fileEncoding
= wxLocale::GetSystemEncodingName();
612 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
617 fileEncoding
= m_encoding
;
620 // GetSystemEncodingName may not have returned a name
621 if (fileEncoding
.empty())
623 fileEncoding
= wxT("UTF-8");
625 fileEncoding
= wxT("ISO-8859-1");
627 convFile
= new wxCSConv(fileEncoding
);
628 deleteConvFile
= true;
632 wxMBConv
* convMem
= wxConvCurrent
;
634 wxMBConv
* convMem
= NULL
;
638 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
639 version
, fileEncoding
);
640 OutputString(stream
, s
, NULL
, NULL
);
641 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
645 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
647 OutputIndentation(stream
, level
);
648 wxString nameAndDescr
;
649 if (!buffer
->GetStyleSheet()->GetName().IsEmpty())
650 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
651 if (!buffer
->GetStyleSheet()->GetDescription().IsEmpty())
652 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
653 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"), convMem
, convFile
);
657 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
659 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
660 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
663 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
665 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
666 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
669 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
671 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
672 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
675 OutputIndentation(stream
, level
);
676 OutputString(stream
, wxT("</stylesheet>"), convMem
, convFile
);
680 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
682 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
683 OutputString(stream
, wxT("\n"), NULL
, NULL
);
691 /// Recursively export an object
692 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
695 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
696 objectName
= wxT("paragraphlayout");
697 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
698 objectName
= wxT("paragraph");
699 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
700 objectName
= wxT("text");
701 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
702 objectName
= wxT("image");
704 objectName
= wxT("object");
706 bool terminateTag
= true;
708 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
710 wxRichTextPlainText
& textObj
= (wxRichTextPlainText
&) obj
;
712 wxString style
= CreateStyle(obj
.GetAttributes(), false);
716 const wxString
& text
= textObj
.GetText();
717 int len
= (int) text
.Length();
722 OutputIndentation(stream
, indent
);
723 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
724 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
725 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
727 else for (i
= 0; i
< len
; i
++)
729 int c
= (int) text
[i
];
730 if ((c
< 32 || c
== 34) && c
!= 9 && c
!= 10 && c
!= 13)
734 OutputIndentation(stream
, indent
);
735 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
737 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
739 wxString
fragment(text
.Mid(last
, i
-last
));
740 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
742 OutputString(stream
, wxT("\""), convMem
, convFile
);
743 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
744 OutputString(stream
, wxT("\""), convMem
, convFile
);
747 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
749 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
753 // Output this character as a number in a separate tag, because XML can't cope
754 // with entities below 32 except for 9, 10 and 13
756 OutputIndentation(stream
, indent
);
757 OutputString(stream
, wxT("<symbol"), convMem
, convFile
);
759 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
760 OutputString(stream
, wxString::Format(wxT("%d"), c
), convMem
, convFile
);
762 OutputString(stream
, wxT("</symbol>"), convMem
, convFile
);
770 fragment
= text
.Mid(last
, i
-last
);
774 OutputIndentation(stream
, indent
);
775 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
777 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
779 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
781 OutputString(stream
, wxT("\""), convMem
, convFile
);
782 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
783 OutputString(stream
, wxT("\""), convMem
, convFile
);
786 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
789 terminateTag
= false;
791 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
793 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
795 wxString style
= CreateStyle(obj
.GetAttributes(), false);
797 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
798 imageObj
.MakeBlock();
800 OutputIndentation(stream
, indent
);
801 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
802 if (!imageObj
.GetImageBlock().Ok())
805 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
809 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\"") + style
+ wxT(">"), (int) imageObj
.GetImageBlock().GetImageType()));
812 OutputIndentation(stream
, indent
+1);
813 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
815 imageObj
.GetImageBlock().WriteHex(stream
);
817 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
819 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
821 OutputIndentation(stream
, indent
);
822 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
825 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
828 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
830 if (objectName
== wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox
&) obj
).GetPartialParagraph())
831 style
<< wxT(" partialparagraph=\"true\"");
833 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
835 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
837 for (i
= 0; i
< composite
.GetChildCount(); i
++)
839 wxRichTextObject
* child
= composite
.GetChild(i
);
840 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
844 if (objectName
!= wxT("text"))
845 OutputIndentation(stream
, indent
);
848 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
853 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextStyleDefinition
* def
, int level
)
855 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
856 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
857 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
859 wxString baseStyle
= def
->GetBaseStyle();
860 wxString baseStyleProp
;
861 if (!baseStyle
.IsEmpty())
862 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
864 wxString descr
= def
->GetDescription();
866 if (!descr
.IsEmpty())
867 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
871 OutputIndentation(stream
, level
);
872 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
876 wxString style
= CreateStyle(def
->GetStyle(), false);
878 OutputIndentation(stream
, level
);
879 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
881 OutputIndentation(stream
, level
);
882 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
886 OutputIndentation(stream
, level
);
887 OutputString(stream
, wxT("</characterstyle>"), convMem
, convFile
);
891 OutputIndentation(stream
, level
);
893 if (!listDef
->GetNextStyle().IsEmpty())
894 baseStyleProp
<< wxT(" basestyle=\"") << listDef
->GetNextStyle() << wxT("\"");
896 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
900 wxString style
= CreateStyle(def
->GetStyle(), false);
902 OutputIndentation(stream
, level
);
903 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
905 OutputIndentation(stream
, level
);
906 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
909 for (i
= 0; i
< 10; i
++)
911 wxTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
914 wxString style
= CreateStyle(def
->GetStyle(), false);
915 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
917 OutputIndentation(stream
, level
);
918 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"), convMem
, convFile
);
920 OutputIndentation(stream
, level
);
921 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
927 OutputIndentation(stream
, level
);
928 OutputString(stream
, wxT("</liststyle>"), convMem
, convFile
);
932 OutputIndentation(stream
, level
);
934 if (!paraDef
->GetNextStyle().IsEmpty())
935 baseStyleProp
<< wxT(" basestyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
937 OutputString(stream
, wxT("<paragraphstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
941 wxString style
= CreateStyle(def
->GetStyle(), false);
943 OutputIndentation(stream
, level
);
944 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
946 OutputIndentation(stream
, level
);
947 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
951 OutputIndentation(stream
, level
);
952 OutputString(stream
, wxT("</paragraphstyle>"), convMem
, convFile
);
958 /// Create style parameters
959 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttr
& attr
, bool isPara
)
962 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
964 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
966 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
968 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
971 if (attr
.HasFontSize())
972 str
<< wxT(" fontsize=\"") << attr
.GetFontSize() << wxT("\"");
974 //if (attr.HasFontFamily())
975 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
977 if (attr
.HasFontItalic())
978 str
<< wxT(" fontstyle=\"") << attr
.GetFontStyle() << wxT("\"");
980 if (attr
.HasFontWeight())
981 str
<< wxT(" fontweight=\"") << attr
.GetFontWeight() << wxT("\"");
983 if (attr
.HasFontUnderlined())
984 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFontUnderlined() << wxT("\"");
986 if (attr
.HasFontFaceName())
987 str
<< wxT(" fontface=\"") << attr
.GetFontFaceName() << wxT("\"");
989 if (attr
.HasTextEffects())
991 str
<< wxT(" texteffects=\"");
992 str
<< attr
.GetTextEffects();
995 str
<< wxT(" texteffectflags=\"");
996 str
<< attr
.GetTextEffectFlags();
1000 if (!attr
.GetCharacterStyleName().empty())
1001 str
<< wxT(" characterstyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
1004 str
<< wxT(" url=\"") << AttributeToXML(attr
.GetURL()) << wxT("\"");
1008 if (attr
.HasAlignment())
1009 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
1011 if (attr
.HasLeftIndent())
1013 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
1014 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
1017 if (attr
.HasRightIndent())
1018 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
1020 if (attr
.HasParagraphSpacingAfter())
1021 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
1023 if (attr
.HasParagraphSpacingBefore())
1024 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
1026 if (attr
.HasLineSpacing())
1027 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
1029 if (attr
.HasBulletStyle())
1030 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
1032 if (attr
.HasBulletNumber())
1033 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
1035 if (attr
.HasBulletText())
1037 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1038 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1039 if (!attr
.GetBulletText().IsEmpty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
1040 str
<< wxT(" bulletsymbol=\"") << (int) (attr
.GetBulletText()[0]) << wxT("\"");
1042 str
<< wxT(" bullettext=\"") << attr
.GetBulletText() << wxT("\"");
1044 str
<< wxT(" bulletfont=\"") << attr
.GetBulletFont() << wxT("\"");
1047 if (attr
.HasBulletName())
1048 str
<< wxT(" bulletname=\"") << attr
.GetBulletName() << wxT("\"");
1050 if (!attr
.GetParagraphStyleName().empty())
1051 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
1053 if (!attr
.GetListStyleName().empty())
1054 str
<< wxT(" liststyle=\"") << wxString(attr
.GetListStyleName()) << wxT("\"");
1058 str
<< wxT(" tabs=\"");
1060 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
1064 str
<< attr
.GetTabs()[i
];
1069 if (attr
.HasPageBreak())
1071 str
<< wxT(" pagebreak=\"1\"");
1074 if (attr
.HasOutlineLevel())
1075 str
<< wxT(" outlinelevel=\"") << (int) attr
.GetOutlineLevel() << wxT("\"");
1082 /// Replace face name with current name for platform.
1083 /// TODO: introduce a virtual function or settable table to
1084 /// do this comprehensively.
1085 bool wxRichTextFixFaceName(wxString
& facename
)
1087 if (facename
.IsEmpty())
1091 if (facename
== wxT("Times"))
1093 facename
= wxT("Times New Roman");
1096 else if (facename
== wxT("Helvetica"))
1098 facename
= wxT("Arial");
1101 else if (facename
== wxT("Courier"))
1103 facename
= wxT("Courier New");
1109 if (facename
== wxT("Times New Roman"))
1111 facename
= wxT("Times");
1114 else if (facename
== wxT("Arial"))
1116 facename
= wxT("Helvetica");
1119 else if (facename
== wxT("Courier New"))
1121 facename
= wxT("Courier");
1129 /// Get style parameters
1130 bool wxRichTextXMLHandler::GetStyle(wxTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1132 wxString fontFacename
;
1134 // int fontFamily = wxDEFAULT;
1135 int fontWeight
= wxNORMAL
;
1136 int fontStyle
= wxNORMAL
;
1137 bool fontUnderlined
= false;
1139 // int fontFlags = 0;
1141 fontFacename
= node
->GetAttribute(wxT("fontface"), wxEmptyString
);
1142 if (!fontFacename
.IsEmpty())
1144 attr
.SetFontFaceName(fontFacename
);
1145 if (GetFlags() & wxRICHTEXT_HANDLER_CONVERT_FACENAMES
)
1146 wxRichTextFixFaceName(fontFacename
);
1151 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1152 //if (!value.empty())
1153 // fontFamily = wxAtoi(value);
1155 value
= node
->GetAttribute(wxT("fontstyle"), wxEmptyString
);
1158 fontStyle
= wxAtoi(value
);
1159 attr
.SetFontStyle(fontStyle
);
1162 value
= node
->GetAttribute(wxT("fontsize"), wxEmptyString
);
1165 fontSize
= wxAtoi(value
);
1166 attr
.SetFontSize(fontSize
);
1169 value
= node
->GetAttribute(wxT("fontweight"), wxEmptyString
);
1172 fontWeight
= wxAtoi(value
);
1173 attr
.SetFontWeight(fontWeight
);
1176 value
= node
->GetAttribute(wxT("fontunderlined"), wxEmptyString
);
1179 fontUnderlined
= wxAtoi(value
) != 0;
1180 attr
.SetFontUnderlined(fontUnderlined
);
1183 value
= node
->GetAttribute(wxT("textcolor"), wxEmptyString
);
1186 if (value
[0] == wxT('#'))
1187 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1189 attr
.SetTextColour(value
);
1192 value
= node
->GetAttribute(wxT("bgcolor"), wxEmptyString
);
1195 if (value
[0] == wxT('#'))
1196 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1198 attr
.SetBackgroundColour(value
);
1201 value
= node
->GetAttribute(wxT("characterstyle"), wxEmptyString
);
1203 attr
.SetCharacterStyleName(value
);
1205 value
= node
->GetAttribute(wxT("texteffects"), wxEmptyString
);
1206 if (!value
.IsEmpty())
1208 attr
.SetTextEffects(wxAtoi(value
));
1211 value
= node
->GetAttribute(wxT("texteffectflags"), wxEmptyString
);
1212 if (!value
.IsEmpty())
1214 attr
.SetTextEffectFlags(wxAtoi(value
));
1217 value
= node
->GetAttribute(wxT("url"), wxEmptyString
);
1221 // Set paragraph attributes
1224 value
= node
->GetAttribute(wxT("alignment"), wxEmptyString
);
1226 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1228 int leftSubIndent
= 0;
1230 bool hasLeftIndent
= false;
1232 value
= node
->GetAttribute(wxT("leftindent"), wxEmptyString
);
1235 leftIndent
= wxAtoi(value
);
1236 hasLeftIndent
= true;
1239 value
= node
->GetAttribute(wxT("leftsubindent"), wxEmptyString
);
1242 leftSubIndent
= wxAtoi(value
);
1243 hasLeftIndent
= true;
1247 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
1249 value
= node
->GetAttribute(wxT("rightindent"), wxEmptyString
);
1251 attr
.SetRightIndent(wxAtoi(value
));
1253 value
= node
->GetAttribute(wxT("parspacingbefore"), wxEmptyString
);
1255 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1257 value
= node
->GetAttribute(wxT("parspacingafter"), wxEmptyString
);
1259 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1261 value
= node
->GetAttribute(wxT("linespacing"), wxEmptyString
);
1263 attr
.SetLineSpacing(wxAtoi(value
));
1265 value
= node
->GetAttribute(wxT("bulletstyle"), wxEmptyString
);
1267 attr
.SetBulletStyle(wxAtoi(value
));
1269 value
= node
->GetAttribute(wxT("bulletnumber"), wxEmptyString
);
1271 attr
.SetBulletNumber(wxAtoi(value
));
1273 value
= node
->GetAttribute(wxT("bulletsymbol"), wxEmptyString
);
1276 wxChar ch
= wxAtoi(value
);
1279 attr
.SetBulletText(s
);
1282 value
= node
->GetAttribute(wxT("bullettext"), wxEmptyString
);
1284 attr
.SetBulletText(value
);
1286 value
= node
->GetAttribute(wxT("bulletfont"), wxEmptyString
);
1288 attr
.SetBulletFont(value
);
1290 value
= node
->GetAttribute(wxT("bulletname"), wxEmptyString
);
1292 attr
.SetBulletName(value
);
1294 value
= node
->GetAttribute(wxT("parstyle"), wxEmptyString
);
1296 attr
.SetParagraphStyleName(value
);
1298 value
= node
->GetAttribute(wxT("liststyle"), wxEmptyString
);
1300 attr
.SetListStyleName(value
);
1302 value
= node
->GetAttribute(wxT("tabs"), wxEmptyString
);
1306 wxStringTokenizer
tkz(value
, wxT(","));
1307 while (tkz
.HasMoreTokens())
1309 wxString token
= tkz
.GetNextToken();
1310 tabs
.Add(wxAtoi(token
));
1315 value
= node
->GetAttribute(wxT("pagebreak"), wxEmptyString
);
1316 if (!value
.IsEmpty())
1318 attr
.SetPageBreak(wxAtoi(value
) != 0);
1321 value
= node
->GetAttribute(wxT("outlinelevel"), wxEmptyString
);
1322 if (!value
.IsEmpty())
1324 attr
.SetOutlineLevel(wxAtoi(value
));
1335 // wxUSE_RICHTEXT && wxUSE_XML