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 para
->AppendChild(imageObj
);
205 wxStringInputStream
strStream(data
);
207 imageObj
->GetImageBlock().ReadHex(strStream
, data
.length(), imageType
);
210 child
= child
->GetNext();
215 else if (name
== wxT("stylesheet"))
217 if (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
)
219 wxRichTextStyleSheet
* sheet
= new wxRichTextStyleSheet
;
220 wxString sheetName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
221 wxString sheetDescription
= node
->GetAttribute(wxT("description"), wxEmptyString
);
222 sheet
->SetName(sheetName
);
223 sheet
->SetDescription(sheetDescription
);
225 wxXmlNode
* child
= node
->GetChildren();
228 ImportStyleDefinition(sheet
, child
);
230 child
= child
->GetNext();
233 // Notify that styles have changed. If this is vetoed by the app,
234 // the new sheet will be deleted. If it is not vetoed, the
235 // old sheet will be deleted and replaced with the new one.
236 buffer
->SetStyleSheetAndNotify(sheet
);
243 wxXmlNode
* child
= node
->GetChildren();
246 ImportXML(buffer
, child
);
247 child
= child
->GetNext();
254 bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet
* sheet
, wxXmlNode
* node
)
256 wxString styleType
= node
->GetName();
257 wxString styleName
= node
->GetAttribute(wxT("name"), wxEmptyString
);
258 wxString baseStyleName
= node
->GetAttribute(wxT("basestyle"), wxEmptyString
);
260 if (styleName
.IsEmpty())
263 if (styleType
== wxT("characterstyle"))
265 wxRichTextCharacterStyleDefinition
* def
= new wxRichTextCharacterStyleDefinition(styleName
);
266 def
->SetBaseStyle(baseStyleName
);
268 wxXmlNode
* child
= node
->GetChildren();
271 if (child
->GetName() == wxT("style"))
274 GetStyle(attr
, child
, false);
277 child
= child
->GetNext();
280 sheet
->AddCharacterStyle(def
);
282 else if (styleType
== wxT("paragraphstyle"))
284 wxRichTextParagraphStyleDefinition
* def
= new wxRichTextParagraphStyleDefinition(styleName
);
286 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
287 def
->SetNextStyle(nextStyleName
);
288 def
->SetBaseStyle(baseStyleName
);
290 wxXmlNode
* child
= node
->GetChildren();
293 if (child
->GetName() == wxT("style"))
296 GetStyle(attr
, child
, false);
299 child
= child
->GetNext();
302 sheet
->AddParagraphStyle(def
);
304 else if (styleType
== wxT("liststyle"))
306 wxRichTextListStyleDefinition
* def
= new wxRichTextListStyleDefinition(styleName
);
308 wxString nextStyleName
= node
->GetAttribute(wxT("nextstyle"), wxEmptyString
);
309 def
->SetNextStyle(nextStyleName
);
310 def
->SetBaseStyle(baseStyleName
);
312 wxXmlNode
* child
= node
->GetChildren();
315 if (child
->GetName() == wxT("style"))
318 GetStyle(attr
, child
, false);
320 wxString styleLevel
= child
->GetAttribute(wxT("level"), wxEmptyString
);
321 if (styleLevel
.IsEmpty())
327 int level
= wxAtoi(styleLevel
);
328 if (level
> 0 && level
<= 10)
330 def
->SetLevelAttributes(level
-1, attr
);
334 child
= child
->GetNext();
337 sheet
->AddListStyle(def
);
343 //-----------------------------------------------------------------------------
344 // xml support routines
345 //-----------------------------------------------------------------------------
347 bool wxRichTextXMLHandler::HasParam(wxXmlNode
* node
, const wxString
& param
)
349 return (GetParamNode(node
, param
) != NULL
);
352 wxXmlNode
*wxRichTextXMLHandler::GetParamNode(wxXmlNode
* node
, const wxString
& param
)
354 wxCHECK_MSG(node
, NULL
, wxT("You can't access node data before it was initialized!"));
356 wxXmlNode
*n
= node
->GetChildren();
360 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
368 wxString
wxRichTextXMLHandler::GetNodeContent(wxXmlNode
*node
)
371 if (n
== NULL
) return wxEmptyString
;
372 n
= n
->GetChildren();
376 if (n
->GetType() == wxXML_TEXT_NODE
||
377 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
378 return n
->GetContent();
381 return wxEmptyString
;
385 wxString
wxRichTextXMLHandler::GetParamValue(wxXmlNode
*node
, const wxString
& param
)
388 return GetNodeContent(node
);
390 return GetNodeContent(GetParamNode(node
, param
));
393 wxString
wxRichTextXMLHandler::GetText(wxXmlNode
*node
, const wxString
& param
, bool WXUNUSED(translate
))
395 wxXmlNode
*parNode
= GetParamNode(node
, param
);
398 wxString
str1(GetNodeContent(parNode
));
402 // For use with earlier versions of wxWidgets
403 #ifndef WXUNUSED_IN_UNICODE
405 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
407 #define WXUNUSED_IN_UNICODE(x) x
411 // write string to output:
412 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
,
413 wxMBConv
*WXUNUSED_IN_UNICODE(convMem
) = NULL
, wxMBConv
*convFile
= NULL
)
415 if (str
.empty()) return;
419 const wxWX2MBbuf
buf(str
.mb_str(*convFile
));
420 stream
.Write((const char*)buf
, strlen((const char*)buf
));
424 const wxWX2MBbuf
buf(str
.mb_str(wxConvUTF8
));
425 stream
.Write((const char*)buf
, strlen((const char*)buf
));
428 if ( convFile
== NULL
)
429 stream
.Write(str
.mb_str(), str
.Len());
432 wxString
str2(str
.wc_str(*convMem
), *convFile
);
433 stream
.Write(str2
.mb_str(), str2
.Len());
438 // Same as above, but create entities first.
439 // Translates '<' to "<", '>' to ">" and '&' to "&"
440 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
,
441 wxMBConv
*convMem
= NULL
, wxMBConv
*convFile
= NULL
)
449 for (i
= 0; i
< len
; i
++)
453 // Original code excluded "&" but we _do_ want to convert
454 // the ampersand beginning & because otherwise when read in,
455 // the original "&" becomes "&".
457 if (c
== wxT('<') || c
== wxT('>') || c
== wxT('"') ||
458 (c
== wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
460 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
464 OutputString(stream
, wxT("<"), NULL
, NULL
);
467 OutputString(stream
, wxT(">"), NULL
, NULL
);
470 OutputString(stream
, wxT("&"), NULL
, NULL
);
473 OutputString(stream
, wxT("""), NULL
, NULL
);
479 else if (wxUChar(c
) > 127)
481 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
483 wxString
s(wxT("&#"));
486 OutputString(stream
, s
, NULL
, NULL
);
490 OutputString(stream
, str
.Mid(last
, i
- last
), convMem
, convFile
);
493 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
495 wxString str
= wxT("\n");
496 for (int i
= 0; i
< indent
; i
++)
497 str
<< wxT(' ') << wxT(' ');
498 OutputString(stream
, str
, NULL
, NULL
);
501 // Convert a colour to a 6-digit hex string
502 static wxString
ColourToHexString(const wxColour
& col
)
506 hex
+= wxDecToHex(col
.Red());
507 hex
+= wxDecToHex(col
.Green());
508 hex
+= wxDecToHex(col
.Blue());
513 // Convert 6-digit hex string to a colour
514 static wxColour
HexStringToColour(const wxString
& hex
)
516 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
517 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
518 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
520 return wxColour(r
, g
, b
);
523 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
528 wxString
version(wxT("1.0") ) ;
530 bool deleteConvFile
= false;
531 wxString fileEncoding
;
532 wxMBConv
* convFile
= NULL
;
535 fileEncoding
= wxT("UTF-8");
536 convFile
= & wxConvUTF8
;
538 fileEncoding
= wxT("ISO-8859-1");
539 convFile
= & wxConvISO8859_1
;
542 // If SetEncoding has been called, change the output encoding.
543 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
545 if (m_encoding
== wxT("<System>"))
548 fileEncoding
= wxLocale::GetSystemEncodingName();
549 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
554 fileEncoding
= m_encoding
;
557 // GetSystemEncodingName may not have returned a name
558 if (fileEncoding
.empty())
560 fileEncoding
= wxT("UTF-8");
562 fileEncoding
= wxT("ISO-8859-1");
564 convFile
= new wxCSConv(fileEncoding
);
565 deleteConvFile
= true;
569 wxMBConv
* convMem
= wxConvCurrent
;
571 wxMBConv
* convMem
= NULL
;
575 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
576 version
, fileEncoding
);
577 OutputString(stream
, s
, NULL
, NULL
);
578 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
582 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
584 OutputIndentation(stream
, level
);
585 wxString nameAndDescr
;
586 if (!buffer
->GetStyleSheet()->GetName().IsEmpty())
587 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
588 if (!buffer
->GetStyleSheet()->GetDescription().IsEmpty())
589 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
590 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"), convMem
, convFile
);
594 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
596 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
597 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
600 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
602 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
603 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
606 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
608 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
609 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
612 OutputIndentation(stream
, level
);
613 OutputString(stream
, wxT("</stylesheet>"), convMem
, convFile
);
617 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
619 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
620 OutputString(stream
, wxT("\n"), NULL
, NULL
);
628 /// Recursively export an object
629 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
632 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
633 objectName
= wxT("paragraphlayout");
634 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
635 objectName
= wxT("paragraph");
636 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
637 objectName
= wxT("text");
638 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
639 objectName
= wxT("image");
641 objectName
= wxT("object");
643 bool terminateTag
= true;
645 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
647 wxRichTextPlainText
& textObj
= (wxRichTextPlainText
&) obj
;
649 wxString style
= CreateStyle(obj
.GetAttributes(), false);
653 const wxString
& text
= textObj
.GetText();
654 int len
= (int) text
.Length();
655 for (i
= 0; i
< len
; i
++)
657 int c
= (int) text
[i
];
658 if (c
< 32 && c
!= 9 && c
!= 10 && c
!= 13)
662 OutputIndentation(stream
, indent
);
663 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
665 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
667 wxString
fragment(text
.Mid(last
, i
-last
));
668 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
670 OutputString(stream
, wxT("\""), convMem
, convFile
);
671 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
672 OutputString(stream
, wxT("\""), convMem
, convFile
);
675 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
677 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
681 // Output this character as a number in a separate tag, because XML can't cope
682 // with entities below 32 except for 9, 10 and 13
684 OutputIndentation(stream
, indent
);
685 OutputString(stream
, wxT("<symbol"), convMem
, convFile
);
687 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
688 OutputString(stream
, wxString::Format(wxT("%d"), c
), convMem
, convFile
);
690 OutputString(stream
, wxT("</symbol>"), convMem
, convFile
);
698 fragment
= text
.Mid(last
, i
-last
);
702 OutputIndentation(stream
, indent
);
703 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
705 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
707 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
709 OutputString(stream
, wxT("\""), convMem
, convFile
);
710 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
711 OutputString(stream
, wxT("\""), convMem
, convFile
);
714 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
717 terminateTag
= false;
719 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
721 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
723 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
724 imageObj
.MakeBlock();
726 OutputIndentation(stream
, indent
);
727 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
728 if (!imageObj
.GetImageBlock().Ok())
731 OutputString(stream
, wxT(">"), convMem
, convFile
);
735 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\">"), (int) imageObj
.GetImageBlock().GetImageType()));
738 OutputIndentation(stream
, indent
+1);
739 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
741 imageObj
.GetImageBlock().WriteHex(stream
);
743 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
745 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
747 OutputIndentation(stream
, indent
);
748 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
751 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
754 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
756 if (objectName
== wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox
&) obj
).GetPartialParagraph())
757 style
<< wxT(" partialparagraph=\"true\"");
759 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
761 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
763 for (i
= 0; i
< composite
.GetChildCount(); i
++)
765 wxRichTextObject
* child
= composite
.GetChild(i
);
766 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
770 if (objectName
!= wxT("text"))
771 OutputIndentation(stream
, indent
);
774 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
779 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextStyleDefinition
* def
, int level
)
781 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
782 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
783 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
785 wxString baseStyle
= def
->GetBaseStyle();
786 wxString baseStyleProp
;
787 if (!baseStyle
.IsEmpty())
788 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
790 wxString descr
= def
->GetDescription();
792 if (!descr
.IsEmpty())
793 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
797 OutputIndentation(stream
, level
);
798 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
802 wxString style
= CreateStyle(def
->GetStyle(), false);
804 OutputIndentation(stream
, level
);
805 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
807 OutputIndentation(stream
, level
);
808 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
812 OutputIndentation(stream
, level
);
813 OutputString(stream
, wxT("</characterstyle>"), convMem
, convFile
);
817 OutputIndentation(stream
, level
);
819 if (!listDef
->GetNextStyle().IsEmpty())
820 baseStyleProp
<< wxT(" basestyle=\"") << listDef
->GetNextStyle() << wxT("\"");
822 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
826 wxString style
= CreateStyle(def
->GetStyle(), false);
828 OutputIndentation(stream
, level
);
829 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
831 OutputIndentation(stream
, level
);
832 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
835 for (i
= 0; i
< 10; i
++)
837 wxTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
840 wxString style
= CreateStyle(def
->GetStyle(), false);
841 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
843 OutputIndentation(stream
, level
);
844 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"), convMem
, convFile
);
846 OutputIndentation(stream
, level
);
847 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
853 OutputIndentation(stream
, level
);
854 OutputString(stream
, wxT("</liststyle>"), convMem
, convFile
);
858 OutputIndentation(stream
, level
);
860 if (!paraDef
->GetNextStyle().IsEmpty())
861 baseStyleProp
<< wxT(" basestyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
863 OutputString(stream
, wxT("<paragraphstyle") + 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("</paragraphstyle>"), convMem
, convFile
);
884 /// Create style parameters
885 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttr
& attr
, bool isPara
)
888 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
890 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
892 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
894 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
897 if (attr
.HasFontSize())
898 str
<< wxT(" fontsize=\"") << attr
.GetFontSize() << wxT("\"");
900 //if (attr.HasFontFamily())
901 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
903 if (attr
.HasFontItalic())
904 str
<< wxT(" fontstyle=\"") << attr
.GetFontStyle() << wxT("\"");
906 if (attr
.HasFontWeight())
907 str
<< wxT(" fontweight=\"") << attr
.GetFontWeight() << wxT("\"");
909 if (attr
.HasFontUnderlined())
910 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFontUnderlined() << wxT("\"");
912 if (attr
.HasFontFaceName())
913 str
<< wxT(" fontface=\"") << attr
.GetFontFaceName() << wxT("\"");
915 if (attr
.HasTextEffects())
917 str
<< wxT(" texteffects=\"");
918 str
<< attr
.GetTextEffects();
921 str
<< wxT(" texteffectflags=\"");
922 str
<< attr
.GetTextEffectFlags();
926 if (!attr
.GetCharacterStyleName().empty())
927 str
<< wxT(" characterstyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
930 str
<< wxT(" url=\"") << attr
.GetURL() << wxT("\"");
934 if (attr
.HasAlignment())
935 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
937 if (attr
.HasLeftIndent())
939 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
940 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
943 if (attr
.HasRightIndent())
944 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
946 if (attr
.HasParagraphSpacingAfter())
947 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
949 if (attr
.HasParagraphSpacingBefore())
950 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
952 if (attr
.HasLineSpacing())
953 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
955 if (attr
.HasBulletStyle())
956 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
958 if (attr
.HasBulletNumber())
959 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
961 if (attr
.HasBulletText())
963 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
964 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
965 if (!attr
.GetBulletText().IsEmpty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
966 str
<< wxT(" bulletsymbol=\"") << (int) (attr
.GetBulletText()[0]) << wxT("\"");
968 str
<< wxT(" bullettext=\"") << attr
.GetBulletText() << wxT("\"");
970 str
<< wxT(" bulletfont=\"") << attr
.GetBulletFont() << wxT("\"");
973 if (attr
.HasBulletName())
974 str
<< wxT(" bulletname=\"") << attr
.GetBulletName() << wxT("\"");
976 if (!attr
.GetParagraphStyleName().empty())
977 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
979 if (!attr
.GetListStyleName().empty())
980 str
<< wxT(" liststyle=\"") << wxString(attr
.GetListStyleName()) << wxT("\"");
984 str
<< wxT(" tabs=\"");
986 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
990 str
<< attr
.GetTabs()[i
];
995 if (attr
.HasPageBreak())
997 str
<< wxT(" pagebreak=\"1\"");
1000 if (attr
.HasOutlineLevel())
1001 str
<< wxT(" outlinelevel=\"") << (int) attr
.GetOutlineLevel() << wxT("\"");
1008 /// Get style parameters
1009 bool wxRichTextXMLHandler::GetStyle(wxTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1011 wxString fontFacename
;
1013 // int fontFamily = wxDEFAULT;
1014 int fontWeight
= wxNORMAL
;
1015 int fontStyle
= wxNORMAL
;
1016 bool fontUnderlined
= false;
1018 // int fontFlags = 0;
1020 fontFacename
= node
->GetAttribute(wxT("fontface"), wxEmptyString
);
1021 if (!fontFacename
.IsEmpty())
1022 attr
.SetFontFaceName(fontFacename
);
1025 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1026 //if (!value.empty())
1027 // fontFamily = wxAtoi(value);
1029 value
= node
->GetAttribute(wxT("fontstyle"), wxEmptyString
);
1032 fontStyle
= wxAtoi(value
);
1033 attr
.SetFontStyle(fontStyle
);
1036 value
= node
->GetAttribute(wxT("fontsize"), wxEmptyString
);
1039 fontSize
= wxAtoi(value
);
1040 attr
.SetFontSize(fontSize
);
1043 value
= node
->GetAttribute(wxT("fontweight"), wxEmptyString
);
1046 fontWeight
= wxAtoi(value
);
1047 attr
.SetFontWeight(fontWeight
);
1050 value
= node
->GetAttribute(wxT("fontunderlined"), wxEmptyString
);
1053 fontUnderlined
= wxAtoi(value
) != 0;
1054 attr
.SetFontUnderlined(fontUnderlined
);
1057 value
= node
->GetAttribute(wxT("textcolor"), wxEmptyString
);
1060 if (value
[0] == wxT('#'))
1061 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1063 attr
.SetTextColour(value
);
1066 value
= node
->GetAttribute(wxT("backgroundcolor"), wxEmptyString
);
1069 if (value
[0] == wxT('#'))
1070 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1072 attr
.SetBackgroundColour(value
);
1075 value
= node
->GetAttribute(wxT("characterstyle"), wxEmptyString
);
1077 attr
.SetCharacterStyleName(value
);
1079 value
= node
->GetAttribute(wxT("texteffects"), wxEmptyString
);
1080 if (!value
.IsEmpty())
1082 attr
.SetTextEffects(wxAtoi(value
));
1085 value
= node
->GetAttribute(wxT("texteffectflags"), wxEmptyString
);
1086 if (!value
.IsEmpty())
1088 attr
.SetTextEffectFlags(wxAtoi(value
));
1091 value
= node
->GetAttribute(wxT("url"), wxEmptyString
);
1095 // Set paragraph attributes
1098 value
= node
->GetAttribute(wxT("alignment"), wxEmptyString
);
1100 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1102 int leftSubIndent
= 0;
1104 bool hasLeftIndent
= false;
1106 value
= node
->GetAttribute(wxT("leftindent"), wxEmptyString
);
1109 leftIndent
= wxAtoi(value
);
1110 hasLeftIndent
= true;
1113 value
= node
->GetAttribute(wxT("leftsubindent"), wxEmptyString
);
1116 leftSubIndent
= wxAtoi(value
);
1117 hasLeftIndent
= true;
1121 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
1123 value
= node
->GetAttribute(wxT("rightindent"), wxEmptyString
);
1125 attr
.SetRightIndent(wxAtoi(value
));
1127 value
= node
->GetAttribute(wxT("parspacingbefore"), wxEmptyString
);
1129 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1131 value
= node
->GetAttribute(wxT("parspacingafter"), wxEmptyString
);
1133 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1135 value
= node
->GetAttribute(wxT("linespacing"), wxEmptyString
);
1137 attr
.SetLineSpacing(wxAtoi(value
));
1139 value
= node
->GetAttribute(wxT("bulletstyle"), wxEmptyString
);
1141 attr
.SetBulletStyle(wxAtoi(value
));
1143 value
= node
->GetAttribute(wxT("bulletnumber"), wxEmptyString
);
1145 attr
.SetBulletNumber(wxAtoi(value
));
1147 value
= node
->GetAttribute(wxT("bulletsymbol"), wxEmptyString
);
1150 wxChar ch
= wxAtoi(value
);
1153 attr
.SetBulletText(s
);
1156 value
= node
->GetAttribute(wxT("bullettext"), wxEmptyString
);
1158 attr
.SetBulletText(value
);
1160 value
= node
->GetAttribute(wxT("bulletfont"), wxEmptyString
);
1162 attr
.SetBulletFont(value
);
1164 value
= node
->GetAttribute(wxT("bulletname"), wxEmptyString
);
1166 attr
.SetBulletName(value
);
1168 value
= node
->GetAttribute(wxT("parstyle"), wxEmptyString
);
1170 attr
.SetParagraphStyleName(value
);
1172 value
= node
->GetAttribute(wxT("liststyle"), wxEmptyString
);
1174 attr
.SetListStyleName(value
);
1176 value
= node
->GetAttribute(wxT("tabs"), wxEmptyString
);
1180 wxStringTokenizer
tkz(value
, wxT(","));
1181 while (tkz
.HasMoreTokens())
1183 wxString token
= tkz
.GetNextToken();
1184 tabs
.Add(wxAtoi(token
));
1189 value
= node
->GetAttribute(wxT("pagebreak"), wxEmptyString
);
1190 if (!value
.IsEmpty())
1192 attr
.SetPageBreak(wxAtoi(value
) != 0);
1195 value
= node
->GetAttribute(wxT("outlinelevel"), wxEmptyString
);
1196 if (!value
.IsEmpty())
1198 attr
.SetOutlineLevel(wxAtoi(value
));
1209 // wxUSE_RICHTEXT && wxUSE_XML