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 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
496 wxString str
= wxT("\n");
497 for (int i
= 0; i
< indent
; i
++)
498 str
<< wxT(' ') << wxT(' ');
499 OutputString(stream
, str
, NULL
, NULL
);
502 // Convert a colour to a 6-digit hex string
503 static wxString
ColourToHexString(const wxColour
& col
)
507 hex
+= wxDecToHex(col
.Red());
508 hex
+= wxDecToHex(col
.Green());
509 hex
+= wxDecToHex(col
.Blue());
514 // Convert 6-digit hex string to a colour
515 static wxColour
HexStringToColour(const wxString
& hex
)
517 unsigned char r
= (unsigned char)wxHexToDec(hex
.Mid(0, 2));
518 unsigned char g
= (unsigned char)wxHexToDec(hex
.Mid(2, 2));
519 unsigned char b
= (unsigned char)wxHexToDec(hex
.Mid(4, 2));
521 return wxColour(r
, g
, b
);
524 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer
*buffer
, wxOutputStream
& stream
)
529 wxString
version(wxT("1.0") ) ;
531 bool deleteConvFile
= false;
532 wxString fileEncoding
;
533 wxMBConv
* convFile
= NULL
;
536 fileEncoding
= wxT("UTF-8");
537 convFile
= & wxConvUTF8
;
539 fileEncoding
= wxT("ISO-8859-1");
540 convFile
= & wxConvISO8859_1
;
543 // If SetEncoding has been called, change the output encoding.
544 if (!m_encoding
.empty() && m_encoding
.Lower() != fileEncoding
.Lower())
546 if (m_encoding
== wxT("<System>"))
549 fileEncoding
= wxLocale::GetSystemEncodingName();
550 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
555 fileEncoding
= m_encoding
;
558 // GetSystemEncodingName may not have returned a name
559 if (fileEncoding
.empty())
561 fileEncoding
= wxT("UTF-8");
563 fileEncoding
= wxT("ISO-8859-1");
565 convFile
= new wxCSConv(fileEncoding
);
566 deleteConvFile
= true;
570 wxMBConv
* convMem
= wxConvCurrent
;
572 wxMBConv
* convMem
= NULL
;
576 s
.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
577 version
, fileEncoding
);
578 OutputString(stream
, s
, NULL
, NULL
);
579 OutputString(stream
, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL
, NULL
);
583 if (buffer
->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET
))
585 OutputIndentation(stream
, level
);
586 wxString nameAndDescr
;
587 if (!buffer
->GetStyleSheet()->GetName().IsEmpty())
588 nameAndDescr
<< wxT(" name=\"") << buffer
->GetStyleSheet()->GetName() << wxT("\"");
589 if (!buffer
->GetStyleSheet()->GetDescription().IsEmpty())
590 nameAndDescr
<< wxT(" description=\"") << buffer
->GetStyleSheet()->GetDescription() << wxT("\"");
591 OutputString(stream
, wxString(wxT("<stylesheet")) + nameAndDescr
+ wxT(">"), convMem
, convFile
);
595 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetCharacterStyleCount(); i
++)
597 wxRichTextCharacterStyleDefinition
* def
= buffer
->GetStyleSheet()->GetCharacterStyle(i
);
598 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
601 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetParagraphStyleCount(); i
++)
603 wxRichTextParagraphStyleDefinition
* def
= buffer
->GetStyleSheet()->GetParagraphStyle(i
);
604 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
607 for (i
= 0; i
< (int) buffer
->GetStyleSheet()->GetListStyleCount(); i
++)
609 wxRichTextListStyleDefinition
* def
= buffer
->GetStyleSheet()->GetListStyle(i
);
610 ExportStyleDefinition(stream
, convMem
, convFile
, def
, level
+ 1);
613 OutputIndentation(stream
, level
);
614 OutputString(stream
, wxT("</stylesheet>"), convMem
, convFile
);
618 bool success
= ExportXML(stream
, convMem
, convFile
, *buffer
, level
);
620 OutputString(stream
, wxT("\n</richtext>") , NULL
, NULL
);
621 OutputString(stream
, wxT("\n"), NULL
, NULL
);
629 /// Recursively export an object
630 bool wxRichTextXMLHandler::ExportXML(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextObject
& obj
, int indent
)
633 if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox
)))
634 objectName
= wxT("paragraphlayout");
635 else if (obj
.IsKindOf(CLASSINFO(wxRichTextParagraph
)))
636 objectName
= wxT("paragraph");
637 else if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
638 objectName
= wxT("text");
639 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
640 objectName
= wxT("image");
642 objectName
= wxT("object");
644 bool terminateTag
= true;
646 if (obj
.IsKindOf(CLASSINFO(wxRichTextPlainText
)))
648 wxRichTextPlainText
& textObj
= (wxRichTextPlainText
&) obj
;
650 wxString style
= CreateStyle(obj
.GetAttributes(), false);
654 const wxString
& text
= textObj
.GetText();
655 int len
= (int) text
.Length();
656 for (i
= 0; i
< len
; i
++)
658 int c
= (int) text
[i
];
659 if (c
< 32 && c
!= 9 && c
!= 10 && c
!= 13)
663 OutputIndentation(stream
, indent
);
664 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
666 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
668 wxString
fragment(text
.Mid(last
, i
-last
));
669 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
671 OutputString(stream
, wxT("\""), convMem
, convFile
);
672 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
673 OutputString(stream
, wxT("\""), convMem
, convFile
);
676 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
678 OutputString(stream
, wxT("</text>"), convMem
, convFile
);
682 // Output this character as a number in a separate tag, because XML can't cope
683 // with entities below 32 except for 9, 10 and 13
685 OutputIndentation(stream
, indent
);
686 OutputString(stream
, wxT("<symbol"), convMem
, convFile
);
688 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
689 OutputString(stream
, wxString::Format(wxT("%d"), c
), convMem
, convFile
);
691 OutputString(stream
, wxT("</symbol>"), convMem
, convFile
);
699 fragment
= text
.Mid(last
, i
-last
);
703 OutputIndentation(stream
, indent
);
704 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
706 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
708 if (!fragment
.empty() && (fragment
[0] == wxT(' ') || fragment
[fragment
.length()-1] == wxT(' ')))
710 OutputString(stream
, wxT("\""), convMem
, convFile
);
711 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
712 OutputString(stream
, wxT("\""), convMem
, convFile
);
715 OutputStringEnt(stream
, fragment
, convMem
, convFile
);
718 terminateTag
= false;
720 else if (obj
.IsKindOf(CLASSINFO(wxRichTextImage
)))
722 wxRichTextImage
& imageObj
= (wxRichTextImage
&) obj
;
724 wxString style
= CreateStyle(obj
.GetAttributes(), false);
726 if (imageObj
.GetImage().Ok() && !imageObj
.GetImageBlock().Ok())
727 imageObj
.MakeBlock();
729 OutputIndentation(stream
, indent
);
730 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
731 if (!imageObj
.GetImageBlock().Ok())
734 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
738 OutputString(stream
, wxString::Format(wxT(" imagetype=\"%d\"") + style
+ wxT(">"), (int) imageObj
.GetImageBlock().GetImageType()));
741 OutputIndentation(stream
, indent
+1);
742 OutputString(stream
, wxT("<data>"), convMem
, convFile
);
744 imageObj
.GetImageBlock().WriteHex(stream
);
746 OutputString(stream
, wxT("</data>"), convMem
, convFile
);
748 else if (obj
.IsKindOf(CLASSINFO(wxRichTextCompositeObject
)))
750 OutputIndentation(stream
, indent
);
751 OutputString(stream
, wxT("<") + objectName
, convMem
, convFile
);
754 if (objectName
== wxT("paragraph") || objectName
== wxT("paragraphlayout"))
757 wxString style
= CreateStyle(obj
.GetAttributes(), isPara
);
759 if (objectName
== wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox
&) obj
).GetPartialParagraph())
760 style
<< wxT(" partialparagraph=\"true\"");
762 OutputString(stream
, style
+ wxT(">"), convMem
, convFile
);
764 wxRichTextCompositeObject
& composite
= (wxRichTextCompositeObject
&) obj
;
766 for (i
= 0; i
< composite
.GetChildCount(); i
++)
768 wxRichTextObject
* child
= composite
.GetChild(i
);
769 ExportXML(stream
, convMem
, convFile
, *child
, indent
+1);
773 if (objectName
!= wxT("text"))
774 OutputIndentation(stream
, indent
);
777 OutputString(stream
, wxT("</") + objectName
+ wxT(">"), convMem
, convFile
);
782 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream
& stream
, wxMBConv
* convMem
, wxMBConv
* convFile
, wxRichTextStyleDefinition
* def
, int level
)
784 wxRichTextCharacterStyleDefinition
* charDef
= wxDynamicCast(def
, wxRichTextCharacterStyleDefinition
);
785 wxRichTextParagraphStyleDefinition
* paraDef
= wxDynamicCast(def
, wxRichTextParagraphStyleDefinition
);
786 wxRichTextListStyleDefinition
* listDef
= wxDynamicCast(def
, wxRichTextListStyleDefinition
);
788 wxString baseStyle
= def
->GetBaseStyle();
789 wxString baseStyleProp
;
790 if (!baseStyle
.IsEmpty())
791 baseStyleProp
= wxT(" basestyle=\"") + baseStyle
+ wxT("\"");
793 wxString descr
= def
->GetDescription();
795 if (!descr
.IsEmpty())
796 descrProp
= wxT(" description=\"") + descr
+ wxT("\"");
800 OutputIndentation(stream
, level
);
801 OutputString(stream
, wxT("<characterstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
805 wxString style
= CreateStyle(def
->GetStyle(), false);
807 OutputIndentation(stream
, level
);
808 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
810 OutputIndentation(stream
, level
);
811 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
815 OutputIndentation(stream
, level
);
816 OutputString(stream
, wxT("</characterstyle>"), convMem
, convFile
);
820 OutputIndentation(stream
, level
);
822 if (!listDef
->GetNextStyle().IsEmpty())
823 baseStyleProp
<< wxT(" basestyle=\"") << listDef
->GetNextStyle() << wxT("\"");
825 OutputString(stream
, wxT("<liststyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
829 wxString style
= CreateStyle(def
->GetStyle(), false);
831 OutputIndentation(stream
, level
);
832 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
834 OutputIndentation(stream
, level
);
835 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
838 for (i
= 0; i
< 10; i
++)
840 wxTextAttr
* levelAttr
= listDef
->GetLevelAttributes(i
);
843 wxString style
= CreateStyle(def
->GetStyle(), false);
844 wxString levelStr
= wxString::Format(wxT(" level=\"%d\" "), (i
+1));
846 OutputIndentation(stream
, level
);
847 OutputString(stream
, wxT("<style ") + levelStr
+ style
+ wxT(">"), convMem
, convFile
);
849 OutputIndentation(stream
, level
);
850 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
856 OutputIndentation(stream
, level
);
857 OutputString(stream
, wxT("</liststyle>"), convMem
, convFile
);
861 OutputIndentation(stream
, level
);
863 if (!paraDef
->GetNextStyle().IsEmpty())
864 baseStyleProp
<< wxT(" basestyle=\"") << paraDef
->GetNextStyle() << wxT("\"");
866 OutputString(stream
, wxT("<paragraphstyle") + baseStyleProp
+ descrProp
+ wxT(">"), convMem
, convFile
);
870 wxString style
= CreateStyle(def
->GetStyle(), false);
872 OutputIndentation(stream
, level
);
873 OutputString(stream
, wxT("<style ") + style
+ wxT(">"), convMem
, convFile
);
875 OutputIndentation(stream
, level
);
876 OutputString(stream
, wxT("</style>"), convMem
, convFile
);
880 OutputIndentation(stream
, level
);
881 OutputString(stream
, wxT("</paragraphstyle>"), convMem
, convFile
);
887 /// Create style parameters
888 wxString
wxRichTextXMLHandler::CreateStyle(const wxTextAttr
& attr
, bool isPara
)
891 if (attr
.HasTextColour() && attr
.GetTextColour().Ok())
893 str
<< wxT(" textcolor=\"#") << ColourToHexString(attr
.GetTextColour()) << wxT("\"");
895 if (attr
.HasBackgroundColour() && attr
.GetBackgroundColour().Ok())
897 str
<< wxT(" bgcolor=\"#") << ColourToHexString(attr
.GetBackgroundColour()) << wxT("\"");
900 if (attr
.HasFontSize())
901 str
<< wxT(" fontsize=\"") << attr
.GetFontSize() << wxT("\"");
903 //if (attr.HasFontFamily())
904 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
906 if (attr
.HasFontItalic())
907 str
<< wxT(" fontstyle=\"") << attr
.GetFontStyle() << wxT("\"");
909 if (attr
.HasFontWeight())
910 str
<< wxT(" fontweight=\"") << attr
.GetFontWeight() << wxT("\"");
912 if (attr
.HasFontUnderlined())
913 str
<< wxT(" fontunderlined=\"") << (int) attr
.GetFontUnderlined() << wxT("\"");
915 if (attr
.HasFontFaceName())
916 str
<< wxT(" fontface=\"") << attr
.GetFontFaceName() << wxT("\"");
918 if (attr
.HasTextEffects())
920 str
<< wxT(" texteffects=\"");
921 str
<< attr
.GetTextEffects();
924 str
<< wxT(" texteffectflags=\"");
925 str
<< attr
.GetTextEffectFlags();
929 if (!attr
.GetCharacterStyleName().empty())
930 str
<< wxT(" characterstyle=\"") << wxString(attr
.GetCharacterStyleName()) << wxT("\"");
933 str
<< wxT(" url=\"") << attr
.GetURL() << wxT("\"");
937 if (attr
.HasAlignment())
938 str
<< wxT(" alignment=\"") << (int) attr
.GetAlignment() << wxT("\"");
940 if (attr
.HasLeftIndent())
942 str
<< wxT(" leftindent=\"") << (int) attr
.GetLeftIndent() << wxT("\"");
943 str
<< wxT(" leftsubindent=\"") << (int) attr
.GetLeftSubIndent() << wxT("\"");
946 if (attr
.HasRightIndent())
947 str
<< wxT(" rightindent=\"") << (int) attr
.GetRightIndent() << wxT("\"");
949 if (attr
.HasParagraphSpacingAfter())
950 str
<< wxT(" parspacingafter=\"") << (int) attr
.GetParagraphSpacingAfter() << wxT("\"");
952 if (attr
.HasParagraphSpacingBefore())
953 str
<< wxT(" parspacingbefore=\"") << (int) attr
.GetParagraphSpacingBefore() << wxT("\"");
955 if (attr
.HasLineSpacing())
956 str
<< wxT(" linespacing=\"") << (int) attr
.GetLineSpacing() << wxT("\"");
958 if (attr
.HasBulletStyle())
959 str
<< wxT(" bulletstyle=\"") << (int) attr
.GetBulletStyle() << wxT("\"");
961 if (attr
.HasBulletNumber())
962 str
<< wxT(" bulletnumber=\"") << (int) attr
.GetBulletNumber() << wxT("\"");
964 if (attr
.HasBulletText())
966 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
967 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
968 if (!attr
.GetBulletText().IsEmpty() && (attr
.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL
))
969 str
<< wxT(" bulletsymbol=\"") << (int) (attr
.GetBulletText()[0]) << wxT("\"");
971 str
<< wxT(" bullettext=\"") << attr
.GetBulletText() << wxT("\"");
973 str
<< wxT(" bulletfont=\"") << attr
.GetBulletFont() << wxT("\"");
976 if (attr
.HasBulletName())
977 str
<< wxT(" bulletname=\"") << attr
.GetBulletName() << wxT("\"");
979 if (!attr
.GetParagraphStyleName().empty())
980 str
<< wxT(" parstyle=\"") << wxString(attr
.GetParagraphStyleName()) << wxT("\"");
982 if (!attr
.GetListStyleName().empty())
983 str
<< wxT(" liststyle=\"") << wxString(attr
.GetListStyleName()) << wxT("\"");
987 str
<< wxT(" tabs=\"");
989 for (i
= 0; i
< attr
.GetTabs().GetCount(); i
++)
993 str
<< attr
.GetTabs()[i
];
998 if (attr
.HasPageBreak())
1000 str
<< wxT(" pagebreak=\"1\"");
1003 if (attr
.HasOutlineLevel())
1004 str
<< wxT(" outlinelevel=\"") << (int) attr
.GetOutlineLevel() << wxT("\"");
1011 /// Get style parameters
1012 bool wxRichTextXMLHandler::GetStyle(wxTextAttr
& attr
, wxXmlNode
* node
, bool isPara
)
1014 wxString fontFacename
;
1016 // int fontFamily = wxDEFAULT;
1017 int fontWeight
= wxNORMAL
;
1018 int fontStyle
= wxNORMAL
;
1019 bool fontUnderlined
= false;
1021 // int fontFlags = 0;
1023 fontFacename
= node
->GetAttribute(wxT("fontface"), wxEmptyString
);
1024 if (!fontFacename
.IsEmpty())
1025 attr
.SetFontFaceName(fontFacename
);
1028 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1029 //if (!value.empty())
1030 // fontFamily = wxAtoi(value);
1032 value
= node
->GetAttribute(wxT("fontstyle"), wxEmptyString
);
1035 fontStyle
= wxAtoi(value
);
1036 attr
.SetFontStyle(fontStyle
);
1039 value
= node
->GetAttribute(wxT("fontsize"), wxEmptyString
);
1042 fontSize
= wxAtoi(value
);
1043 attr
.SetFontSize(fontSize
);
1046 value
= node
->GetAttribute(wxT("fontweight"), wxEmptyString
);
1049 fontWeight
= wxAtoi(value
);
1050 attr
.SetFontWeight(fontWeight
);
1053 value
= node
->GetAttribute(wxT("fontunderlined"), wxEmptyString
);
1056 fontUnderlined
= wxAtoi(value
) != 0;
1057 attr
.SetFontUnderlined(fontUnderlined
);
1060 value
= node
->GetAttribute(wxT("textcolor"), wxEmptyString
);
1063 if (value
[0] == wxT('#'))
1064 attr
.SetTextColour(HexStringToColour(value
.Mid(1)));
1066 attr
.SetTextColour(value
);
1069 value
= node
->GetAttribute(wxT("bgcolor"), wxEmptyString
);
1072 if (value
[0] == wxT('#'))
1073 attr
.SetBackgroundColour(HexStringToColour(value
.Mid(1)));
1075 attr
.SetBackgroundColour(value
);
1078 value
= node
->GetAttribute(wxT("characterstyle"), wxEmptyString
);
1080 attr
.SetCharacterStyleName(value
);
1082 value
= node
->GetAttribute(wxT("texteffects"), wxEmptyString
);
1083 if (!value
.IsEmpty())
1085 attr
.SetTextEffects(wxAtoi(value
));
1088 value
= node
->GetAttribute(wxT("texteffectflags"), wxEmptyString
);
1089 if (!value
.IsEmpty())
1091 attr
.SetTextEffectFlags(wxAtoi(value
));
1094 value
= node
->GetAttribute(wxT("url"), wxEmptyString
);
1098 // Set paragraph attributes
1101 value
= node
->GetAttribute(wxT("alignment"), wxEmptyString
);
1103 attr
.SetAlignment((wxTextAttrAlignment
) wxAtoi(value
));
1105 int leftSubIndent
= 0;
1107 bool hasLeftIndent
= false;
1109 value
= node
->GetAttribute(wxT("leftindent"), wxEmptyString
);
1112 leftIndent
= wxAtoi(value
);
1113 hasLeftIndent
= true;
1116 value
= node
->GetAttribute(wxT("leftsubindent"), wxEmptyString
);
1119 leftSubIndent
= wxAtoi(value
);
1120 hasLeftIndent
= true;
1124 attr
.SetLeftIndent(leftIndent
, leftSubIndent
);
1126 value
= node
->GetAttribute(wxT("rightindent"), wxEmptyString
);
1128 attr
.SetRightIndent(wxAtoi(value
));
1130 value
= node
->GetAttribute(wxT("parspacingbefore"), wxEmptyString
);
1132 attr
.SetParagraphSpacingBefore(wxAtoi(value
));
1134 value
= node
->GetAttribute(wxT("parspacingafter"), wxEmptyString
);
1136 attr
.SetParagraphSpacingAfter(wxAtoi(value
));
1138 value
= node
->GetAttribute(wxT("linespacing"), wxEmptyString
);
1140 attr
.SetLineSpacing(wxAtoi(value
));
1142 value
= node
->GetAttribute(wxT("bulletstyle"), wxEmptyString
);
1144 attr
.SetBulletStyle(wxAtoi(value
));
1146 value
= node
->GetAttribute(wxT("bulletnumber"), wxEmptyString
);
1148 attr
.SetBulletNumber(wxAtoi(value
));
1150 value
= node
->GetAttribute(wxT("bulletsymbol"), wxEmptyString
);
1153 wxChar ch
= wxAtoi(value
);
1156 attr
.SetBulletText(s
);
1159 value
= node
->GetAttribute(wxT("bullettext"), wxEmptyString
);
1161 attr
.SetBulletText(value
);
1163 value
= node
->GetAttribute(wxT("bulletfont"), wxEmptyString
);
1165 attr
.SetBulletFont(value
);
1167 value
= node
->GetAttribute(wxT("bulletname"), wxEmptyString
);
1169 attr
.SetBulletName(value
);
1171 value
= node
->GetAttribute(wxT("parstyle"), wxEmptyString
);
1173 attr
.SetParagraphStyleName(value
);
1175 value
= node
->GetAttribute(wxT("liststyle"), wxEmptyString
);
1177 attr
.SetListStyleName(value
);
1179 value
= node
->GetAttribute(wxT("tabs"), wxEmptyString
);
1183 wxStringTokenizer
tkz(value
, wxT(","));
1184 while (tkz
.HasMoreTokens())
1186 wxString token
= tkz
.GetNextToken();
1187 tabs
.Add(wxAtoi(token
));
1192 value
= node
->GetAttribute(wxT("pagebreak"), wxEmptyString
);
1193 if (!value
.IsEmpty())
1195 attr
.SetPageBreak(wxAtoi(value
) != 0);
1198 value
= node
->GetAttribute(wxT("outlinelevel"), wxEmptyString
);
1199 if (!value
.IsEmpty())
1201 attr
.SetOutlineLevel(wxAtoi(value
));
1212 // wxUSE_RICHTEXT && wxUSE_XML